質問編集履歴
3
元に戻しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,9 +1,173 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
+
BitcoinまたはLitecoinの送金をPay-to-PubkeyHashで行いたいのですが。
|
3
|
+
1つのUTXOからの送金を
|
4
|
+
https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash
|
5
|
+
https://github.com/shirriff/bitcoin-code/blob/master/txnUtils.py
|
6
|
+
を参考に送金を行うことができました。
|
7
|
+
しかし
|
8
|
+
2つ3つの同じアドレスのUTXOから送金をしたいと思っています。
|
2
9
|
|
3
10
|
### 発生している問題・エラーメッセージ
|
11
|
+
https://github.com/shirriff/bitcoin-code/blob/master/txnUtils.py
|
12
|
+
のソースコードを元に以下のソースコードを作成実行したところAPI側から
|
13
|
+
Non-canonical signature: S value is unnecessarily high
|
14
|
+
とエラーが発生してしまいます。
|
4
15
|
|
5
16
|
### 該当のソースコード
|
17
|
+
```python
|
18
|
+
class BitcoinTransaction():
|
19
|
+
def __init__(self,type="insight",APIURL="https://insight.litecore.io",txversion="01000000",iVersion=48,iWIFVersion=176):
|
20
|
+
if type == "insight":
|
21
|
+
self.APIRapper = insight(APIURL)
|
22
|
+
elif type == "dogechain":
|
23
|
+
self.APIRapper = dogechain()
|
24
|
+
|
25
|
+
self.txversion = txversion
|
26
|
+
|
27
|
+
self.util = BitcoinUtil(iVersion,iWIFVersion)
|
28
|
+
|
29
|
+
|
30
|
+
def varstr(self,s):
|
31
|
+
return self.varint(len(s)) + s
|
32
|
+
|
33
|
+
|
34
|
+
def varint(self,n):
|
35
|
+
if n < 0xfd:
|
36
|
+
return struct.pack('<B', n)
|
37
|
+
elif n < 0xffff:
|
38
|
+
return struct.pack('<cH', '\xfd', n)
|
39
|
+
elif n < 0xffffffff:
|
40
|
+
return struct.pack('<cL', '\xfe', n)
|
41
|
+
else:
|
42
|
+
return struct.pack('<cQ', '\xff', n)
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
def GetScriptPubKeyFromAddress(self,address):
|
47
|
+
pubkeyhash = self.util.GetPubkeyhashFromAddress(address)
|
48
|
+
return '76a914' + pubkeyhash + '88ac'
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
def CreateRawInputs(self,address):
|
53
|
+
utxos = self.APIRapper.utxo(address)
|
54
|
+
|
55
|
+
inputs_text = ""
|
56
|
+
inputs_text = inputs_text + "%02x" % len(utxos)
|
57
|
+
|
58
|
+
for index,utxo in enumerate(utxos):
|
59
|
+
utxoTx = binascii.unhexlify(utxo["txid"])
|
60
|
+
utxoTx = utxoTx[::-1]
|
61
|
+
utxoTx = binascii.hexlify(utxoTx)
|
62
|
+
utxoTx = utxoTx.decode('utf-8')
|
63
|
+
|
64
|
+
utxoIndex = utxo["vout"]
|
65
|
+
utxoIndex = struct.pack('<L', utxoIndex)
|
66
|
+
utxoIndex = binascii.hexlify(utxoIndex)
|
67
|
+
utxoIndex = utxoIndex.decode('utf-8')
|
68
|
+
|
69
|
+
scriptPubKey = utxo["scriptPubKey"]
|
70
|
+
scriptPubKey_size = len(binascii.unhexlify(scriptPubKey))
|
71
|
+
scriptPubKey_size = "%02x" % scriptPubKey_size
|
72
|
+
|
73
|
+
sequence = "ffffffff"
|
74
|
+
|
75
|
+
inputs_text = inputs_text + (utxoTx + utxoIndex + scriptPubKey_size + scriptPubKey + sequence)
|
76
|
+
|
77
|
+
return inputs_text
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def CreateSignInputs(self,privkey,datas):
|
82
|
+
address = self.util.GetKeysFromPrivkey(privkey)["address"]
|
83
|
+
utxos = self.APIRapper.utxo(address)
|
84
|
+
|
85
|
+
sk = SigningKey.from_string(binascii.unhexlify(privkey), curve=SECP256k1)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
inputs_text = ""
|
90
|
+
inputs_text = inputs_text + "%02x" % len(utxos)
|
91
|
+
|
92
|
+
for index,utxo in enumerate(utxos):
|
93
|
+
utxoTx = binascii.unhexlify(utxo["txid"])
|
94
|
+
utxoTx = utxoTx[::-1]
|
95
|
+
utxoTx = binascii.hexlify(utxoTx)
|
96
|
+
utxoTx = utxoTx.decode('utf-8')
|
97
|
+
|
98
|
+
utxoIndex = utxo["vout"]
|
99
|
+
utxoIndex = struct.pack('<L', utxoIndex)
|
100
|
+
utxoIndex = binascii.hexlify(utxoIndex)
|
101
|
+
utxoIndex = utxoIndex.decode('utf-8')
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
RawTransaction = self.CreateRawTransaction(address,datas)
|
106
|
+
txhash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(RawTransaction)).digest()).digest()
|
107
|
+
|
108
|
+
sig = sk.sign_digest(txhash, sigencode=ecdsa.util.sigencode_der) + binascii.unhexlify('01') # 01 is hashtype
|
109
|
+
pubKey = binascii.unhexlify(self.util.GetPubkeyFromPrivkey(privkey))
|
110
|
+
scriptSig = binascii.hexlify(self.varstr(sig)).decode('utf-8') + binascii.hexlify(self.varstr(pubKey)).decode('utf-8')
|
111
|
+
|
112
|
+
scriptSig_size = len(binascii.unhexlify(scriptSig))
|
113
|
+
scriptSig_size = "%02x" % scriptSig_size
|
114
|
+
|
115
|
+
|
116
|
+
sequence = "ffffffff"
|
117
|
+
|
118
|
+
inputs_text = inputs_text + (utxoTx + utxoIndex + scriptSig_size + scriptSig + sequence)
|
119
|
+
|
120
|
+
return inputs_text
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def CreateOutputs(self,datas):
|
126
|
+
outputs_text = ""
|
127
|
+
outputs_text = outputs_text + "%02x" % len(datas)
|
128
|
+
for data in datas:
|
129
|
+
redemptionSatoshis, address = data
|
130
|
+
|
131
|
+
rspack = struct.pack("<Q", redemptionSatoshis)
|
132
|
+
rspack = binascii.hexlify(rspack).decode('utf-8')
|
133
|
+
|
134
|
+
outputScript = self.GetScriptPubKeyFromAddress(address)
|
135
|
+
outputScript_size = len(binascii.unhexlify(outputScript))
|
136
|
+
outputScript_size = "%02x" % outputScript_size
|
137
|
+
|
138
|
+
outputs_text = outputs_text + (rspack + outputScript_size + outputScript)
|
139
|
+
|
140
|
+
outputs_text = outputs_text + "00000000"
|
141
|
+
|
142
|
+
return outputs_text
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
def CreateRawTransaction(self,inputaddress,datas):
|
147
|
+
RawTransaction = ""
|
148
|
+
RawTransaction = RawTransaction + self.txversion
|
149
|
+
RawTransaction = RawTransaction + self.CreateRawInputs(inputaddress)
|
150
|
+
RawTransaction = RawTransaction + self.CreateOutputs(datas)
|
151
|
+
|
152
|
+
RawTransaction = RawTransaction + "01000000"
|
153
|
+
|
154
|
+
return RawTransaction
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
def CreateSignTransaction(self,privkey,datas):
|
159
|
+
SignTransaction = ""
|
160
|
+
SignTransaction = SignTransaction + self.txversion
|
161
|
+
SignTransaction = SignTransaction + self.CreateSignInputs(privkey,datas)
|
162
|
+
SignTransaction = SignTransaction + self.CreateOutputs(datas)
|
163
|
+
|
164
|
+
return SignTransaction
|
165
|
+
```
|
6
166
|
|
7
167
|
### 試したこと
|
168
|
+
```python
|
169
|
+
scriptSig = binascii.hexlify(self.varstr(sig)).decode('utf-8') + binascii.hexlify(self.varstr(pubKey)).decode('utf-8')
|
170
|
+
```
|
171
|
+
sigの元のtxhashの元のrawTransactionの中を変更したりしました。
|
8
172
|
|
9
173
|
### 補足情報(FW/ツールのバージョンなど)
|
2
編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,23 +1,9 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
BitcoinまたはLitecoinの送金をPay-to-PubkeyHashで行いたいのですが。
|
4
|
-
1つのUTXOからの送金を
|
5
|
-
https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash
|
6
|
-
https://github.com/shirriff/bitcoin-code/blob/master/txnUtils.py
|
7
|
-
を参考に送金を行うことができました。
|
8
|
-
しかし
|
9
|
-
2つ3つの同じアドレスのUTXOから送金をしたいと思っています。
|
10
|
-
|
11
3
|
### 発生している問題・エラーメッセージ
|
12
|
-
https://github.com/shirriff/bitcoin-code/blob/master/txnUtils.py
|
13
|
-
のソースコードを元に以下のソースコードを作成実行したところAPI側から
|
14
|
-
Non-canonical signature: S value is unnecessarily high
|
15
|
-
とエラーが発生してしまいます。
|
16
4
|
|
17
|
-
|
18
5
|
### 該当のソースコード
|
19
6
|
|
20
7
|
### 試したこと
|
21
8
|
|
22
|
-
|
23
9
|
### 補足情報(FW/ツールのバージョンなど)
|
1
編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,162 +17,7 @@
|
|
17
17
|
|
18
18
|
### 該当のソースコード
|
19
19
|
|
20
|
-
```python
|
21
|
-
class BitcoinTransaction():
|
22
|
-
def __init__(self,type="insight",APIURL="https://insight.litecore.io",txversion="01000000",iVersion=48,iWIFVersion=176):
|
23
|
-
if type == "insight":
|
24
|
-
self.APIRapper = insight(APIURL)
|
25
|
-
elif type == "dogechain":
|
26
|
-
self.APIRapper = dogechain()
|
27
|
-
|
28
|
-
self.txversion = txversion
|
29
|
-
|
30
|
-
self.util = BitcoinUtil(iVersion,iWIFVersion)
|
31
|
-
|
32
|
-
|
33
|
-
def varstr(self,s):
|
34
|
-
return self.varint(len(s)) + s
|
35
|
-
|
36
|
-
|
37
|
-
def varint(self,n):
|
38
|
-
if n < 0xfd:
|
39
|
-
return struct.pack('<B', n)
|
40
|
-
elif n < 0xffff:
|
41
|
-
return struct.pack('<cH', '\xfd', n)
|
42
|
-
elif n < 0xffffffff:
|
43
|
-
return struct.pack('<cL', '\xfe', n)
|
44
|
-
else:
|
45
|
-
return struct.pack('<cQ', '\xff', n)
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
def GetScriptPubKeyFromAddress(self,address):
|
50
|
-
pubkeyhash = self.util.GetPubkeyhashFromAddress(address)
|
51
|
-
return '76a914' + pubkeyhash + '88ac'
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
def CreateRawInputs(self,address):
|
56
|
-
utxos = self.APIRapper.utxo(address)
|
57
|
-
|
58
|
-
inputs_text = ""
|
59
|
-
inputs_text = inputs_text + "%02x" % len(utxos)
|
60
|
-
|
61
|
-
for index,utxo in enumerate(utxos):
|
62
|
-
utxoTx = binascii.unhexlify(utxo["txid"])
|
63
|
-
utxoTx = utxoTx[::-1]
|
64
|
-
utxoTx = binascii.hexlify(utxoTx)
|
65
|
-
utxoTx = utxoTx.decode('utf-8')
|
66
|
-
|
67
|
-
utxoIndex = utxo["vout"]
|
68
|
-
utxoIndex = struct.pack('<L', utxoIndex)
|
69
|
-
utxoIndex = binascii.hexlify(utxoIndex)
|
70
|
-
utxoIndex = utxoIndex.decode('utf-8')
|
71
|
-
|
72
|
-
scriptPubKey = utxo["scriptPubKey"]
|
73
|
-
scriptPubKey_size = len(binascii.unhexlify(scriptPubKey))
|
74
|
-
scriptPubKey_size = "%02x" % scriptPubKey_size
|
75
|
-
|
76
|
-
sequence = "ffffffff"
|
77
|
-
|
78
|
-
inputs_text = inputs_text + (utxoTx + utxoIndex + scriptPubKey_size + scriptPubKey + sequence)
|
79
|
-
|
80
|
-
return inputs_text
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
def CreateSignInputs(self,privkey,datas):
|
85
|
-
address = self.util.GetKeysFromPrivkey(privkey)["address"]
|
86
|
-
utxos = self.APIRapper.utxo(address)
|
87
|
-
|
88
|
-
sk = SigningKey.from_string(binascii.unhexlify(privkey), curve=SECP256k1)
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
inputs_text = ""
|
93
|
-
inputs_text = inputs_text + "%02x" % len(utxos)
|
94
|
-
|
95
|
-
for index,utxo in enumerate(utxos):
|
96
|
-
utxoTx = binascii.unhexlify(utxo["txid"])
|
97
|
-
utxoTx = utxoTx[::-1]
|
98
|
-
utxoTx = binascii.hexlify(utxoTx)
|
99
|
-
utxoTx = utxoTx.decode('utf-8')
|
100
|
-
|
101
|
-
utxoIndex = utxo["vout"]
|
102
|
-
utxoIndex = struct.pack('<L', utxoIndex)
|
103
|
-
utxoIndex = binascii.hexlify(utxoIndex)
|
104
|
-
utxoIndex = utxoIndex.decode('utf-8')
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
RawTransaction = self.CreateRawTransaction(address,datas)
|
109
|
-
txhash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(RawTransaction)).digest()).digest()
|
110
|
-
|
111
|
-
sig = sk.sign_digest(txhash, sigencode=ecdsa.util.sigencode_der) + binascii.unhexlify('01') # 01 is hashtype
|
112
|
-
pubKey = binascii.unhexlify(self.util.GetPubkeyFromPrivkey(privkey))
|
113
|
-
scriptSig = binascii.hexlify(self.varstr(sig)).decode('utf-8') + binascii.hexlify(self.varstr(pubKey)).decode('utf-8')
|
114
|
-
|
115
|
-
scriptSig_size = len(binascii.unhexlify(scriptSig))
|
116
|
-
scriptSig_size = "%02x" % scriptSig_size
|
117
|
-
|
118
|
-
|
119
|
-
sequence = "ffffffff"
|
120
|
-
|
121
|
-
inputs_text = inputs_text + (utxoTx + utxoIndex + scriptSig_size + scriptSig + sequence)
|
122
|
-
|
123
|
-
return inputs_text
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
def CreateOutputs(self,datas):
|
129
|
-
outputs_text = ""
|
130
|
-
outputs_text = outputs_text + "%02x" % len(datas)
|
131
|
-
for data in datas:
|
132
|
-
redemptionSatoshis, address = data
|
133
|
-
|
134
|
-
rspack = struct.pack("<Q", redemptionSatoshis)
|
135
|
-
rspack = binascii.hexlify(rspack).decode('utf-8')
|
136
|
-
|
137
|
-
outputScript = self.GetScriptPubKeyFromAddress(address)
|
138
|
-
outputScript_size = len(binascii.unhexlify(outputScript))
|
139
|
-
outputScript_size = "%02x" % outputScript_size
|
140
|
-
|
141
|
-
outputs_text = outputs_text + (rspack + outputScript_size + outputScript)
|
142
|
-
|
143
|
-
outputs_text = outputs_text + "00000000"
|
144
|
-
|
145
|
-
return outputs_text
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
def CreateRawTransaction(self,inputaddress,datas):
|
150
|
-
RawTransaction = ""
|
151
|
-
RawTransaction = RawTransaction + self.txversion
|
152
|
-
RawTransaction = RawTransaction + self.CreateRawInputs(inputaddress)
|
153
|
-
RawTransaction = RawTransaction + self.CreateOutputs(datas)
|
154
|
-
|
155
|
-
RawTransaction = RawTransaction + "01000000"
|
156
|
-
|
157
|
-
return RawTransaction
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def CreateSignTransaction(self,privkey,datas):
|
162
|
-
SignTransaction = ""
|
163
|
-
SignTransaction = SignTransaction + self.txversion
|
164
|
-
SignTransaction = SignTransaction + self.CreateSignInputs(privkey,datas)
|
165
|
-
SignTransaction = SignTransaction + self.CreateOutputs(datas)
|
166
|
-
|
167
|
-
return SignTransaction
|
168
|
-
```
|
169
|
-
|
170
20
|
### 試したこと
|
171
|
-
```python
|
172
|
-
scriptSig = binascii.hexlify(self.varstr(sig)).decode('utf-8') + binascii.hexlify(self.varstr(pubKey)).decode('utf-8')
|
173
|
-
```
|
174
|
-
をscriptSig = sig + pubkey + pubkeyにしたり
|
175
|
-
sigの元のtxhashの元のrawTransactionの中を変更したりしました。
|
176
21
|
|
177
22
|
|
178
23
|
### 補足情報(FW/ツールのバージョンなど)
|