def redeem_key_to_address(public_redeem_key): pk = base64.urlsafe_b64decode(public_redeem_key) addr = [2, [2, pk], {}] addr_hash = blake2b(sha3_256(cbor.dumps(addr, sort_keys=True)).digest(), digest_size=28).digest() tag = cbor.dumps([addr_hash, {}, 2], sort_keys=True) address = cbor.dumps([ cbor.Tag(24, tag), binascii.crc32(tag) ]) return base58.b58encode(address).decode()
def __call__(self, elements): ret = [bytes([0x9f])] for e in elements: ret.append(cbor.dumps(e)) ret.append(bytes([0xff])) return ret
def convert_raw_tx_to_obj(tx: list, extraData: dict): tx_inputs, tx_outputs, tx_witnesses = tx[0][0], tx[0][1], tx[1] tx_id, tx_body = pack_raw_txid_and_body(tx) inputs, outputs, witnesses = [], [], [] for inp in tx_inputs: types, tagged = inp input_tx_id, idx = cbor.loads(tagged.value) inputs.append({'type': types, 'txId': input_tx_id.hex(), 'idx': idx}) for out in tx_outputs: address, value = out outputs.append({'address': base58.b58encode(cbor.dumps(address)), 'value': value}) for wit in tx_witnesses: types, tagged = wit witnesses.append({'type': types, 'sign': cbor.loads(tagged.value)}) ret = { 'id': tx_id, 'inputs': inputs, 'outputs': outputs, 'witnesses': witnesses, 'txBody': tx_body } ret.update(extraData) return ret
def test_sortkeys(self): obytes = [] xbytes = [] for n in _range(2, 27): ob = {u'{:02x}'.format(x): x for x in _range(n)} obytes.append(dumps(ob, sort_keys=True)) xbytes.append(dumps(ob, sort_keys=False)) allOGood = True someXMiss = False for i, g in enumerate(_GOLDEN_SORTED_KEYS_BYTES): if g != obytes[i]: print('bad sorted result, wanted %r got %r', g, obytes[i]) allOGood = False if g != xbytes[i]: someXMiss = True assert allOGood assert someXMiss
def _oso_bytearray(self, ob): ser = dumps(ob) try: o2 = loads(bytearray(ser)) assert ob == o2, '%r != %r from %s' % (ob, o2, ubinascii.hexlify(ser)) except Exception as e: print('failure on buf len={0} {1!r} ob={2!r} {3!r}; {4}\n'.format( len(ser), hexstr(ser), ob, ser, e)) raise
def pack_raw_txid_and_body(decoded_tx_body): if not decoded_tx_body: raise Exception('can not decode empty tx!') try: inputs, outputs, attributes = decoded_tx_to_base(decoded_tx_body) cbor_indef_array = CborIndefiniteLengthArray() enc = cbor.dumps([ cbor_indef_array(inputs), cbor_indef_array(outputs), attributes, ]) tx_id = blake2b(enc, digest_size=32).hexdigest() tx_body = enc.hex() return [tx_id, tx_body] except Exception as e: raise Exception(f'fail to convert raw tx to ID! {str(e)}')
def test_tuple(self): l = [1, 2, 3] t = tuple(l) ser = dumps(t) o2 = loads(ser) assert l == o2
def _osos(self, ob): obs = dumps(ob) o2 = loads(obs) o2s = dumps(o2) assert obs == o2s
def test_datetime(self): # right now we're just testing that it's possible to dumps() # Tag(0,...) because there was a bug around that. dumps(Tag(0, '1984-01-24T23:22:21'))
def serilize_Cbor(data: Any) -> bytes: return cbor.dumps(data)
def header_to_id(header, tx_type: int): header_data = cbor.dumps([tx_type, header]) return blake2b(header_data, digest_size=32).hexdigest()