mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
implement script_num_to_hex
This commit is contained in:
parent
376a815458
commit
4b89b1e270
2 changed files with 40 additions and 1 deletions
|
@ -152,6 +152,29 @@ def int_to_hex(i, length=1):
|
||||||
s = "0"*(2*length - len(s)) + s
|
s = "0"*(2*length - len(s)) + s
|
||||||
return rev_hex(s)
|
return rev_hex(s)
|
||||||
|
|
||||||
|
def script_num_to_hex(i):
|
||||||
|
"""See CScriptNum in Bitcoin Core.
|
||||||
|
Encodes an integer as hex, to be used in script.
|
||||||
|
|
||||||
|
ported from https://github.com/bitcoin/bitcoin/blob/8cbc5c4be4be22aca228074f087a374a7ec38be8/src/script/script.h#L326
|
||||||
|
"""
|
||||||
|
if i == 0:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
result = bytearray()
|
||||||
|
neg = i < 0
|
||||||
|
absvalue = abs(i)
|
||||||
|
while absvalue > 0:
|
||||||
|
result.append(absvalue & 0xff)
|
||||||
|
absvalue >>= 8
|
||||||
|
|
||||||
|
if result[-1] & 0x80:
|
||||||
|
result.append(0x80 if neg else 0x00)
|
||||||
|
elif neg:
|
||||||
|
result[-1] |= 0x80
|
||||||
|
|
||||||
|
return bh2u(result)
|
||||||
|
|
||||||
|
|
||||||
def var_int(i):
|
def var_int(i):
|
||||||
# https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
|
# https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
|
||||||
|
|
|
@ -11,7 +11,8 @@ from lib.bitcoin import (
|
||||||
var_int, op_push, address_to_script, regenerate_key,
|
var_int, op_push, address_to_script, regenerate_key,
|
||||||
verify_message, deserialize_privkey, serialize_privkey, is_segwit_address,
|
verify_message, deserialize_privkey, serialize_privkey, is_segwit_address,
|
||||||
is_b58_address, address_to_scripthash, is_minikey, is_compressed, is_xpub,
|
is_b58_address, address_to_scripthash, is_minikey, is_compressed, is_xpub,
|
||||||
xpub_type, is_xprv, is_bip32_derivation, seed_type, EncodeBase58Check)
|
xpub_type, is_xprv, is_bip32_derivation, seed_type, EncodeBase58Check,
|
||||||
|
script_num_to_hex)
|
||||||
from lib.util import bfh
|
from lib.util import bfh
|
||||||
from lib import constants
|
from lib import constants
|
||||||
|
|
||||||
|
@ -150,6 +151,21 @@ class Test_bitcoin(unittest.TestCase):
|
||||||
self.assertEqual(op_push(0x10000), '4e00000100')
|
self.assertEqual(op_push(0x10000), '4e00000100')
|
||||||
self.assertEqual(op_push(0x12345678), '4e78563412')
|
self.assertEqual(op_push(0x12345678), '4e78563412')
|
||||||
|
|
||||||
|
def test_script_num_to_hex(self):
|
||||||
|
# test vectors from https://github.com/btcsuite/btcd/blob/fdc2bc867bda6b351191b5872d2da8270df00d13/txscript/scriptnum.go#L77
|
||||||
|
self.assertEqual(script_num_to_hex(127), '7f')
|
||||||
|
self.assertEqual(script_num_to_hex(-127), 'ff')
|
||||||
|
self.assertEqual(script_num_to_hex(128), '8000')
|
||||||
|
self.assertEqual(script_num_to_hex(-128), '8080')
|
||||||
|
self.assertEqual(script_num_to_hex(129), '8100')
|
||||||
|
self.assertEqual(script_num_to_hex(-129), '8180')
|
||||||
|
self.assertEqual(script_num_to_hex(256), '0001')
|
||||||
|
self.assertEqual(script_num_to_hex(-256), '0081')
|
||||||
|
self.assertEqual(script_num_to_hex(32767), 'ff7f')
|
||||||
|
self.assertEqual(script_num_to_hex(-32767), 'ffff')
|
||||||
|
self.assertEqual(script_num_to_hex(32768), '008000')
|
||||||
|
self.assertEqual(script_num_to_hex(-32768), '008080')
|
||||||
|
|
||||||
def test_address_to_script(self):
|
def test_address_to_script(self):
|
||||||
# bech32 native segwit
|
# bech32 native segwit
|
||||||
# test vectors from BIP-0173
|
# test vectors from BIP-0173
|
||||||
|
|
Loading…
Add table
Reference in a new issue