mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-01 17:55:20 +00:00
basic functions and tests for multisig transactions and bip 32
This commit is contained in:
parent
515b3412b0
commit
e1504ba80b
2 changed files with 234 additions and 50 deletions
264
lib/bitcoin.py
264
lib/bitcoin.py
|
@ -29,6 +29,7 @@ def int_to_hex(i, length=1):
|
||||||
return rev_hex(s)
|
return rev_hex(s)
|
||||||
|
|
||||||
def var_int(i):
|
def var_int(i):
|
||||||
|
# https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
|
||||||
if i<0xfd:
|
if i<0xfd:
|
||||||
return int_to_hex(i)
|
return int_to_hex(i)
|
||||||
elif i<=0xffff:
|
elif i<=0xffff:
|
||||||
|
@ -69,22 +70,22 @@ def i2d_ECPrivateKey(pkey, compressed=False):
|
||||||
'%064x' % _r + \
|
'%064x' % _r + \
|
||||||
'020101a144034200'
|
'020101a144034200'
|
||||||
|
|
||||||
return key.decode('hex') + i2o_ECPublicKey(pkey, compressed)
|
return key.decode('hex') + i2o_ECPublicKey(pkey.pubkey, compressed)
|
||||||
|
|
||||||
def i2o_ECPublicKey(pkey, compressed=False):
|
def i2o_ECPublicKey(pubkey, compressed=False):
|
||||||
# public keys are 65 bytes long (520 bits)
|
# public keys are 65 bytes long (520 bits)
|
||||||
# 0x04 + 32-byte X-coordinate + 32-byte Y-coordinate
|
# 0x04 + 32-byte X-coordinate + 32-byte Y-coordinate
|
||||||
# 0x00 = point at infinity, 0x02 and 0x03 = compressed, 0x04 = uncompressed
|
# 0x00 = point at infinity, 0x02 and 0x03 = compressed, 0x04 = uncompressed
|
||||||
# compressed keys: <sign> <x> where <sign> is 0x02 if y is even and 0x03 if y is odd
|
# compressed keys: <sign> <x> where <sign> is 0x02 if y is even and 0x03 if y is odd
|
||||||
if compressed:
|
if compressed:
|
||||||
if pkey.pubkey.point.y() & 1:
|
if pubkey.point.y() & 1:
|
||||||
key = '03' + '%064x' % pkey.pubkey.point.x()
|
key = '03' + '%064x' % pubkey.point.x()
|
||||||
else:
|
else:
|
||||||
key = '02' + '%064x' % pkey.pubkey.point.x()
|
key = '02' + '%064x' % pubkey.point.x()
|
||||||
else:
|
else:
|
||||||
key = '04' + \
|
key = '04' + \
|
||||||
'%064x' % pkey.pubkey.point.x() + \
|
'%064x' % pubkey.point.x() + \
|
||||||
'%064x' % pkey.pubkey.point.y()
|
'%064x' % pubkey.point.y()
|
||||||
|
|
||||||
return key.decode('hex')
|
return key.decode('hex')
|
||||||
|
|
||||||
|
@ -94,8 +95,6 @@ def i2o_ECPublicKey(pkey, compressed=False):
|
||||||
|
|
||||||
############ functions from pywallet #####################
|
############ functions from pywallet #####################
|
||||||
|
|
||||||
addrtype = 0
|
|
||||||
|
|
||||||
def hash_160(public_key):
|
def hash_160(public_key):
|
||||||
try:
|
try:
|
||||||
md = hashlib.new('ripemd160')
|
md = hashlib.new('ripemd160')
|
||||||
|
@ -111,7 +110,7 @@ def public_key_to_bc_address(public_key):
|
||||||
h160 = hash_160(public_key)
|
h160 = hash_160(public_key)
|
||||||
return hash_160_to_bc_address(h160)
|
return hash_160_to_bc_address(h160)
|
||||||
|
|
||||||
def hash_160_to_bc_address(h160):
|
def hash_160_to_bc_address(h160, addrtype = 0):
|
||||||
vh160 = chr(addrtype) + h160
|
vh160 = chr(addrtype) + h160
|
||||||
h = Hash(vh160)
|
h = Hash(vh160)
|
||||||
addr = vh160 + h[0:4]
|
addr = vh160 + h[0:4]
|
||||||
|
@ -119,7 +118,7 @@ def hash_160_to_bc_address(h160):
|
||||||
|
|
||||||
def bc_address_to_hash_160(addr):
|
def bc_address_to_hash_160(addr):
|
||||||
bytes = b58decode(addr, 25)
|
bytes = b58decode(addr, 25)
|
||||||
return bytes[1:21]
|
return ord(bytes[0]), bytes[1:21]
|
||||||
|
|
||||||
def encode_point(pubkey, compressed=False):
|
def encode_point(pubkey, compressed=False):
|
||||||
order = generator_secp256k1.order()
|
order = generator_secp256k1.order()
|
||||||
|
@ -200,12 +199,12 @@ def DecodeBase58Check(psz):
|
||||||
def PrivKeyToSecret(privkey):
|
def PrivKeyToSecret(privkey):
|
||||||
return privkey[9:9+32]
|
return privkey[9:9+32]
|
||||||
|
|
||||||
def SecretToASecret(secret, compressed=False):
|
def SecretToASecret(secret, compressed=False, addrtype=0):
|
||||||
vchIn = chr((addrtype+128)&255) + secret
|
vchIn = chr((addrtype+128)&255) + secret
|
||||||
if compressed: vchIn += '\01'
|
if compressed: vchIn += '\01'
|
||||||
return EncodeBase58Check(vchIn)
|
return EncodeBase58Check(vchIn)
|
||||||
|
|
||||||
def ASecretToSecret(key):
|
def ASecretToSecret(key, addrtype=0):
|
||||||
vch = DecodeBase58Check(key)
|
vch = DecodeBase58Check(key)
|
||||||
if vch and vch[0] == chr((addrtype+128)&255):
|
if vch and vch[0] == chr((addrtype+128)&255):
|
||||||
return vch[1:]
|
return vch[1:]
|
||||||
|
@ -220,8 +219,8 @@ def regenerate_key(sec):
|
||||||
secret = int('0x' + b.encode('hex'), 16)
|
secret = int('0x' + b.encode('hex'), 16)
|
||||||
return EC_KEY(secret)
|
return EC_KEY(secret)
|
||||||
|
|
||||||
def GetPubKey(pkey, compressed=False):
|
def GetPubKey(pubkey, compressed=False):
|
||||||
return i2o_ECPublicKey(pkey, compressed)
|
return i2o_ECPublicKey(pubkey, compressed)
|
||||||
|
|
||||||
def GetPrivKey(pkey, compressed=False):
|
def GetPrivKey(pkey, compressed=False):
|
||||||
return i2d_ECPrivateKey(pkey, compressed)
|
return i2d_ECPrivateKey(pkey, compressed)
|
||||||
|
@ -254,46 +253,231 @@ class EC_KEY(object):
|
||||||
self.secret = secret
|
self.secret = secret
|
||||||
|
|
||||||
|
|
||||||
|
###################################### BIP32 ##############################
|
||||||
|
|
||||||
def filter(s):
|
def bip32_init(seed):
|
||||||
|
import hmac
|
||||||
|
|
||||||
|
I = hmac.new("Bitcoin seed", seed, hashlib.sha512).digest()
|
||||||
|
|
||||||
|
print "seed", seed.encode('hex')
|
||||||
|
master_secret = I[0:32]
|
||||||
|
master_chain = I[32:]
|
||||||
|
|
||||||
|
# public key
|
||||||
|
curve = SECP256k1
|
||||||
|
master_private_key = ecdsa.SigningKey.from_string( master_secret, curve = SECP256k1 )
|
||||||
|
master_public_key = master_private_key.get_verifying_key()
|
||||||
|
K = master_public_key.to_string()
|
||||||
|
K_compressed = GetPubKey(master_public_key.pubkey,True)
|
||||||
|
return master_secret, master_chain, K, K_compressed
|
||||||
|
|
||||||
|
|
||||||
|
def CKD(k, c, n):
|
||||||
|
import hmac
|
||||||
|
from ecdsa.util import string_to_number, number_to_string
|
||||||
|
order = generator_secp256k1.order()
|
||||||
|
keypair = EC_KEY(string_to_number(k))
|
||||||
|
K = GetPubKey(keypair.pubkey,True)
|
||||||
|
I = hmac.new(c, K + rev_hex(int_to_hex(n,4)).decode('hex'), hashlib.sha512).digest()
|
||||||
|
k_n = number_to_string( (string_to_number(I[0:32]) * string_to_number(k)) % order , order )
|
||||||
|
c_n = I[32:]
|
||||||
|
return k_n, c_n
|
||||||
|
|
||||||
|
|
||||||
|
def CKD_prime(K, c, n):
|
||||||
|
import hmac
|
||||||
|
from ecdsa.util import string_to_number, number_to_string
|
||||||
|
order = generator_secp256k1.order()
|
||||||
|
|
||||||
|
K_public_key = ecdsa.VerifyingKey.from_string( K, curve = SECP256k1 )
|
||||||
|
K_compressed = GetPubKey(K_public_key.pubkey,True)
|
||||||
|
|
||||||
|
I = hmac.new(c, K_compressed + rev_hex(int_to_hex(n,4)).decode('hex'), hashlib.sha512).digest()
|
||||||
|
|
||||||
|
#pubkey = ecdsa.ecdsa.Public_key( generator_secp256k1, string_to_number(I[0:32]) * K_public_key.pubkey.point )
|
||||||
|
public_key = ecdsa.VerifyingKey.from_public_point( string_to_number(I[0:32]) * K_public_key.pubkey.point, curve = SECP256k1 )
|
||||||
|
K_n = public_key.to_string()
|
||||||
|
K_n_compressed = GetPubKey(public_key.pubkey,True)
|
||||||
|
c_n = I[32:]
|
||||||
|
|
||||||
|
return K_n, K_n_compressed, c_n
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
################################## transactions
|
||||||
|
|
||||||
|
|
||||||
|
def tx_filter(s):
|
||||||
out = re.sub('( [^\n]*|)\n','',s)
|
out = re.sub('( [^\n]*|)\n','',s)
|
||||||
out = out.replace(' ','')
|
out = out.replace(' ','')
|
||||||
out = out.replace('\n','')
|
out = out.replace('\n','')
|
||||||
return out
|
return out
|
||||||
|
|
||||||
# https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
|
|
||||||
def raw_tx( inputs, outputs, for_sig = None ):
|
def raw_tx( inputs, outputs, for_sig = None ):
|
||||||
s = int_to_hex(1,4) + ' version\n'
|
s = int_to_hex(1,4) # version
|
||||||
s += var_int( len(inputs) ) + ' number of inputs\n'
|
s += var_int( len(inputs) ) # number of inputs
|
||||||
for i in range(len(inputs)):
|
for i in range(len(inputs)):
|
||||||
_, _, p_hash, p_index, p_script, pubkey, sig = inputs[i]
|
_, _, p_hash, p_index, p_script, pubkeysig = inputs[i]
|
||||||
s += p_hash.decode('hex')[::-1].encode('hex') + ' prev hash\n'
|
s += p_hash.decode('hex')[::-1].encode('hex') # prev hash
|
||||||
s += int_to_hex(p_index,4) + ' prev index\n'
|
s += int_to_hex(p_index,4) # prev index
|
||||||
|
|
||||||
if for_sig is None:
|
if for_sig is None:
|
||||||
sig = sig + chr(1) # hashtype
|
if len(pubkeysig) == 1:
|
||||||
script = int_to_hex( len(sig)) + ' push %d bytes\n'%len(sig)
|
pubkey, sig = pubkeysig[0]
|
||||||
script += sig.encode('hex') + ' sig\n'
|
sig = sig + chr(1) # hashtype
|
||||||
script += int_to_hex( len(pubkey)) + ' push %d bytes\n'%len(pubkey)
|
script = int_to_hex( len(sig))
|
||||||
script += pubkey.encode('hex') + ' pubkey\n'
|
script += sig.encode('hex')
|
||||||
|
script += int_to_hex( len(pubkey))
|
||||||
|
script += pubkey.encode('hex')
|
||||||
|
else:
|
||||||
|
pubkey0, sig0 = pubkeysig[0]
|
||||||
|
pubkey1, sig1 = pubkeysig[1]
|
||||||
|
sig0 = sig0 + chr(1)
|
||||||
|
sig1 = sig1 + chr(1)
|
||||||
|
inner_script = multisig_script([pubkey0, pubkey1])
|
||||||
|
script = '00' # op_0
|
||||||
|
script += int_to_hex(len(sig0))
|
||||||
|
script += sig0.encode('hex')
|
||||||
|
script += int_to_hex(len(sig1))
|
||||||
|
script += sig1.encode('hex')
|
||||||
|
script += var_int(len(inner_script)/2)
|
||||||
|
script += inner_script
|
||||||
|
|
||||||
elif for_sig==i:
|
elif for_sig==i:
|
||||||
script = p_script + ' scriptsig \n'
|
if len(pubkeysig) > 1:
|
||||||
|
script = multisig_script(pubkeysig) # p2sh uses the inner script
|
||||||
|
else:
|
||||||
|
script = p_script # scriptsig
|
||||||
else:
|
else:
|
||||||
script=''
|
script=''
|
||||||
s += var_int( len(filter(script))/2 ) + ' script length \n'
|
s += var_int( len(tx_filter(script))/2 ) # script length
|
||||||
s += script
|
s += script
|
||||||
s += "ffffffff" + ' sequence\n'
|
s += "ffffffff" # sequence
|
||||||
s += var_int( len(outputs) ) + ' number of outputs\n'
|
|
||||||
|
s += var_int( len(outputs) ) # number of outputs
|
||||||
for output in outputs:
|
for output in outputs:
|
||||||
addr, amount = output
|
addr, amount = output
|
||||||
s += int_to_hex( amount, 8) + ' amount: %d\n'%amount
|
s += int_to_hex( amount, 8) # amount
|
||||||
script = '76a9' # op_dup, op_hash_160
|
addrtype, hash_160 = bc_address_to_hash_160(addr)
|
||||||
script += '14' # push 0x14 bytes
|
if addrtype == 0:
|
||||||
script += bc_address_to_hash_160(addr).encode('hex')
|
script = '76a9' # op_dup, op_hash_160
|
||||||
script += '88ac' # op_equalverify, op_checksig
|
script += '14' # push 0x14 bytes
|
||||||
s += var_int( len(filter(script))/2 ) + ' script length \n'
|
script += hash_160.encode('hex')
|
||||||
s += script + ' script \n'
|
script += '88ac' # op_equalverify, op_checksig
|
||||||
s += int_to_hex(0,4) # lock time
|
elif addrtype == 5:
|
||||||
if for_sig is not None: s += int_to_hex(1, 4) # hash type
|
script = 'a9' # op_hash_160
|
||||||
|
script += '14' # push 0x14 bytes
|
||||||
|
script += hash_160.encode('hex')
|
||||||
|
script += '87' # op_equal
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
s += var_int( len(tx_filter(script))/2 ) # script length
|
||||||
|
s += script # script
|
||||||
|
s += int_to_hex(0,4) # lock time
|
||||||
|
if for_sig is not None: s += int_to_hex(1, 4) # hash type
|
||||||
|
return tx_filter(s)
|
||||||
|
|
||||||
|
|
||||||
|
def multisig_script(public_keys):
|
||||||
|
# supports only "2 of 2", and "2 of 3" transactions
|
||||||
|
n = len(public_keys)
|
||||||
|
s = '52'
|
||||||
|
for k in public_keys:
|
||||||
|
s += var_int(len(k)/2)
|
||||||
|
s += k
|
||||||
|
if n==2:
|
||||||
|
s += '52'
|
||||||
|
elif n==3:
|
||||||
|
s += '53'
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
s += 'ae'
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_bip32():
|
||||||
|
seed = "ff000000000000000000000000000000".decode('hex')
|
||||||
|
master_secret, master_chain, master_public_key, master_public_key_compressed = bip32_init(seed)
|
||||||
|
|
||||||
|
print "secret key", master_secret.encode('hex')
|
||||||
|
print "chain code", master_chain.encode('hex')
|
||||||
|
|
||||||
|
key_id = hash_160(master_public_key_compressed)
|
||||||
|
print "keyid", key_id.encode('hex')
|
||||||
|
print "base58"
|
||||||
|
print "address", hash_160_to_bc_address(key_id)
|
||||||
|
print "secret key", SecretToASecret(master_secret, True)
|
||||||
|
|
||||||
|
print "-- m/0 --"
|
||||||
|
k0, c0 = CKD(master_secret, master_chain, 0)
|
||||||
|
print "secret", k0.encode('hex')
|
||||||
|
print "chain", c0.encode('hex')
|
||||||
|
print "secret key", SecretToASecret(k0, True)
|
||||||
|
|
||||||
|
K0, K0_compressed, c0 = CKD_prime(master_public_key, master_chain, 0)
|
||||||
|
print "address", hash_160_to_bc_address(hash_160(K0_compressed))
|
||||||
|
|
||||||
|
print "-- m/0/1 --"
|
||||||
|
K01, K01_compressed, c01 = CKD_prime(K0, c0, 1)
|
||||||
|
print "address", hash_160_to_bc_address(hash_160(K01_compressed))
|
||||||
|
|
||||||
|
print "-- m/0/1/3 --"
|
||||||
|
K013, K013_compressed, c013 = CKD_prime(K01, c01, 3)
|
||||||
|
print "address", hash_160_to_bc_address(hash_160(K013_compressed))
|
||||||
|
|
||||||
|
print "-- m/0/1/3/7 --"
|
||||||
|
K0137, K0137_compressed, c0137 = CKD_prime(K013, c013, 7)
|
||||||
|
print "address", hash_160_to_bc_address(hash_160(K0137_compressed))
|
||||||
|
|
||||||
|
|
||||||
|
def test_p2sh():
|
||||||
|
|
||||||
|
print "2 of 2"
|
||||||
|
pubkeys = ["04e89a79651522201d756f14b1874ae49139cc984e5782afeca30ffe84e5e6b2cfadcfe9875c490c8a1a05a4debd715dd57471af8886ab5dfbb3959d97f087f77a",
|
||||||
|
"0455cf4a3ab68a011b18cb0a86aae2b8e9cad6c6355476de05247c57a9632d127084ac7630ad89893b43c486c5a9f7ec6158fb0feb708fa9255d5c4d44bc0858f8"]
|
||||||
|
s = multisig_script(pubkeys)
|
||||||
|
print "address", hash_160_to_bc_address(hash_160(s.decode('hex')), 5)
|
||||||
|
|
||||||
|
|
||||||
|
print "Gavin's tutorial: redeem p2sh: http://blockchain.info/tx-index/30888901"
|
||||||
|
pubkey1 = "0491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f86"
|
||||||
|
pubkey2 = "04865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec6874"
|
||||||
|
pubkey3 = "048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d46213"
|
||||||
|
pubkeys = [pubkey1, pubkey2, pubkey3]
|
||||||
|
|
||||||
|
tx_for_sig = raw_tx( [(None, None, '3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac', 0, 'a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087', pubkeys)],
|
||||||
|
[('1GtpSrGhRGY5kkrNz4RykoqRQoJuG2L6DS',1000000)], for_sig = 0)
|
||||||
|
|
||||||
|
print "tx for sig", tx_for_sig
|
||||||
|
|
||||||
|
signature1 = "304502200187af928e9d155c4b1ac9c1c9118153239aba76774f775d7c1f9c3e106ff33c0221008822b0f658edec22274d0b6ae9de10ebf2da06b1bbdaaba4e50eb078f39e3d78"
|
||||||
|
signature2 = "30440220795f0f4f5941a77ae032ecb9e33753788d7eb5cb0c78d805575d6b00a1d9bfed02203e1f4ad9332d1416ae01e27038e945bc9db59c732728a383a6f1ed2fb99da7a4"
|
||||||
|
|
||||||
|
for pubkey in pubkeys:
|
||||||
|
import traceback, sys
|
||||||
|
|
||||||
|
public_key = ecdsa.VerifyingKey.from_string(pubkey[2:].decode('hex'), curve = SECP256k1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
public_key.verify_digest( signature1.decode('hex'), Hash( tx_for_sig.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
|
||||||
|
print True
|
||||||
|
except ecdsa.keys.BadSignatureError:
|
||||||
|
#traceback.print_exc(file=sys.stdout)
|
||||||
|
print False
|
||||||
|
|
||||||
|
try:
|
||||||
|
public_key.verify_digest( signature2.decode('hex'), Hash( tx_for_sig.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
|
||||||
|
print True
|
||||||
|
except ecdsa.keys.BadSignatureError:
|
||||||
|
#traceback.print_exc(file=sys.stdout)
|
||||||
|
print False
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
#test_bip32()
|
||||||
|
test_p2sh()
|
||||||
|
|
||||||
|
|
|
@ -130,7 +130,7 @@ class Wallet:
|
||||||
|
|
||||||
# rebuild private and public key from regenerated secret
|
# rebuild private and public key from regenerated secret
|
||||||
private_key = GetPrivKey(pkey, compressed)
|
private_key = GetPrivKey(pkey, compressed)
|
||||||
public_key = GetPubKey(pkey, compressed)
|
public_key = GetPubKey(pkey.pubkey, compressed)
|
||||||
address = public_key_to_bc_address(public_key)
|
address = public_key_to_bc_address(public_key)
|
||||||
|
|
||||||
if address in self.all_addresses():
|
if address in self.all_addresses():
|
||||||
|
@ -168,10 +168,10 @@ class Wallet:
|
||||||
ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z')
|
ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z')
|
||||||
if not ADDRESS_RE.match(addr): return False
|
if not ADDRESS_RE.match(addr): return False
|
||||||
try:
|
try:
|
||||||
h = bc_address_to_hash_160(addr)
|
addrtype, h = bc_address_to_hash_160(addr)
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
return addr == hash_160_to_bc_address(h)
|
return addr == hash_160_to_bc_address(h, addrtype)
|
||||||
|
|
||||||
def stretch_key(self,seed):
|
def stretch_key(self,seed):
|
||||||
oldseed = seed
|
oldseed = seed
|
||||||
|
@ -216,7 +216,7 @@ class Wallet:
|
||||||
compressed = False
|
compressed = False
|
||||||
pkey = EC_KEY(secexp)
|
pkey = EC_KEY(secexp)
|
||||||
|
|
||||||
public_key = GetPubKey(pkey, compressed)
|
public_key = GetPubKey(pkey.pubkey, compressed)
|
||||||
addr = public_key_to_bc_address(public_key)
|
addr = public_key_to_bc_address(public_key)
|
||||||
if addr != address:
|
if addr != address:
|
||||||
print_error('Invalid password with correct decoding')
|
print_error('Invalid password with correct decoding')
|
||||||
|
@ -606,7 +606,7 @@ class Wallet:
|
||||||
addr = item.get('address')
|
addr = item.get('address')
|
||||||
v = item.get('value')
|
v = item.get('value')
|
||||||
total += v
|
total += v
|
||||||
inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], None, None) )
|
inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], [(None,None)] ))
|
||||||
fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
|
fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
|
||||||
if total >= amount + fee: break
|
if total >= amount + fee: break
|
||||||
else:
|
else:
|
||||||
|
@ -628,18 +628,18 @@ class Wallet:
|
||||||
def sign_inputs( self, inputs, outputs, password ):
|
def sign_inputs( self, inputs, outputs, password ):
|
||||||
s_inputs = []
|
s_inputs = []
|
||||||
for i in range(len(inputs)):
|
for i in range(len(inputs)):
|
||||||
addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
|
addr, v, p_hash, p_pos, p_scriptPubKey, _ = inputs[i]
|
||||||
secexp, compressed = self.get_private_key(addr, password)
|
secexp, compressed = self.get_private_key(addr, password)
|
||||||
private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
|
private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
|
||||||
public_key = private_key.get_verifying_key()
|
public_key = private_key.get_verifying_key()
|
||||||
|
|
||||||
pkey = EC_KEY(secexp)
|
pkey = EC_KEY(secexp)
|
||||||
pubkey = GetPubKey(pkey, compressed)
|
pubkey = GetPubKey(pkey.pubkey, compressed)
|
||||||
|
|
||||||
tx = filter( raw_tx( inputs, outputs, for_sig = i ) )
|
tx = raw_tx( inputs, outputs, for_sig = i )
|
||||||
sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
|
sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
|
||||||
assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
|
assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
|
||||||
s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig) )
|
s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, [(pubkey, sig)] ) )
|
||||||
return s_inputs
|
return s_inputs
|
||||||
|
|
||||||
def pw_encode(self, s, password):
|
def pw_encode(self, s, password):
|
||||||
|
@ -841,7 +841,7 @@ class Wallet:
|
||||||
|
|
||||||
def signed_tx(self, inputs, outputs, password):
|
def signed_tx(self, inputs, outputs, password):
|
||||||
s_inputs = self.sign_inputs( inputs, outputs, password )
|
s_inputs = self.sign_inputs( inputs, outputs, password )
|
||||||
tx = filter( raw_tx( s_inputs, outputs ) )
|
tx = raw_tx( s_inputs, outputs )
|
||||||
return tx
|
return tx
|
||||||
|
|
||||||
def sendtx(self, tx):
|
def sendtx(self, tx):
|
||||||
|
|
Loading…
Add table
Reference in a new issue