Python

OP_RETURN 使用 python

  • July 1, 2015

我正在嘗試製作一個可以可靠地上傳 OP_RETURN 事務的簡單程序。我在網上找到了一些例子,但到目前為止,它們並不能始終如一地工作。經過的那些似乎是隨機的。以下是經過的交易

<https://blockchain.info/address/1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y>

每次我嘗試時都會受到打擊或錯過。我完全無法使用腳本上傳任何交易,但是偶爾如果我使用他們通過的區塊鏈上傳原始交易。有任何想法嗎?這是錯誤消息。

Traceback (most recent call last):
 File "/Users/JC/Documents/DevelopmentStuff/programming /Python/bit/op-return_test2.py", line 51, in &lt;module&gt;
   print(pushtx(tx))
 File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 237, in pushtx
   return f(*args)
 File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 192, in bci_pushtx
   return make_request('https://blockchain.info/pushtx', 'tx='+tx)
 File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 23, in make_request
   raise Exception(p)
Exception: None Standard Script Output OP_RETURN 48656c6c6f

這是程式碼。我真的很感激任何幫助。謝謝

# coding: utf-8

from bitcoin import *
import binascii
from test import *



priv = "Private key"

pub = "1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y"

addr= "1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y"
inputs = unspent(addr)

message = "Hello"
FullLen = format(len(message)+2,'x').rjust(2,'0') 
MessageLen = format(len(message),'x').rjust(2,'0')
ID = binascii.hexlify(str(message))
#snd = "6a4c28"+ID
snd = "6a"+MessageLen+ID
print hexlify(chr(len(message)))
print MessageLen
print snd
#snd = "6a4c"+MessageLen+ID
#print snd
newScriptPubKey = "6a" + hexlify(chr(len(message)))+ hexlify(message)
#print newScriptPubKey

outputs = [{'value': 50000, 'address': addr}, {'value': 0, 'script': snd}]

fee = 10000

tx = mksend(inputs, outputs, addr, fee)

dt = deserialize(tx)
ins = dt['ins']

#print addr
#print ins

print tx
for ind, elm in enumerate(ins):
   print elm
   tx = sign(tx, ind, priv)

print tx



print(pushtx(tx)) # Error is raised on this line

e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62

deserialize(str(blockr_fetchtx("e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62")))

{'ins': [{'outpoint': {'hash': 'a5221ad4b031d9487dfb2c334b3d84a3c33d908e5f1089141a72b2f4c98baf55',
   'index': 0},
  'script': '483045022100e684df2de15562b09f267d6ec25a1f8043e0162fb7426e40ee87a1a0b51a4ab8022009069aced5a4a33ac720dbbafad37b36e1
087ea9578b1ec9cd14ea4367debc0f01410466c719272fda4368a44cde914dd210b3dcf1913b700b1fdd2948f6d44acb404c250d172705bd297d9a3fcc4ddb9e
0d3fe2c52b870e31eb035f650921bd68d73c',
  'sequence': 4294967295L},
 {'outpoint': {'hash': 'a5221ad4b031d9487dfb2c334b3d84a3c33d908e5f1089141a72b2f4c98baf55',
   'index': 2},
  'script': '483045022008e0edd19548f7098db140d703fd60e00b184c7d4746da57fdb36f5ff573bc080221008203612b6bf02e9e15bde7e412085323e4
d561761ddc2632f5f68a0dc057aebd01410466c719272fda4368a44cde914dd210b3dcf1913b700b1fdd2948f6d44acb404c250d172705bd297d9a3fcc4ddb9e
0d3fe2c52b870e31eb035f650921bd68d73c',
  'sequence': 4294967295L}],
'locktime': 0,
'outs': [{'script': '76a91497734226bd4c9c7764771a077e775e8581e030e988ac',
  'value': 50000},
 {'script': '6a4c0848656c6c6f526179', 'value': 0},
 {'script': '76a91497734226bd4c9c7764771a077e775e8581e030e988ac',
  'value': 210477}],
'version': 1}

{'script': '6a4c0848656c6c6f526179', 'value': 0}

OP_RETURN 是非標準的,這是 BCI 拋出的錯誤消息。問題出在4c前面6a:如果不需要推送數據程式碼,則 Tx 是非標準的。它應該只是讀取6a48656c6c6f526179,或者在序列化的 Tx 中它應該是096a48656c6c6f526179而不是0b6a4c0848656c6c6f526179.

您可能會發現我們的 python-OP_RETURN 庫很有幫助,可以開箱即用,也可以深入了解我們如何建構 OP_RETURN 事務。

<https://github.com/coinspark/python-OP_RETURN>

它還具有一個簡潔的功能,可以使用帶有 OP_RETURN 的多個鍊式交易將任意大小的數據儲存在區塊鏈中,並使用單個 12 位參考號檢索該數據。

引用自:https://bitcoin.stackexchange.com/questions/37158