def test_simple_transfer(self): EXPECTED_DIGEST = "7fff24ca77fbb23b9b3c460104ab1a74011bafe3965e15dc6ea6f25a4ac44392" EXPECTED_PAYLOAD = \ "a1440000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d44235130ac5aab442e39f" \ "9aa27118956695229212dd2f1ab5b714e9f6bd581511c1010000000000000000000000000418c2a33af8bd2cba7f" \ "a714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758b" \ "cc4961bbdc75a0251c" # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_transfer(IDENTITIES[1], 256) payload.add_signer(IDENTITIES[0]) # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)
def action(self, contract_digest: Address, contract_address: Address, action: str, fee: int, from_address: Address, signers: EntityList, *args, shard_mask: BitVector = None): # Default to wildcard shard mask if none supplied if not shard_mask: logging.warning( "Defaulting to wildcard shard mask as none supplied") shard_mask = BitVector() # build up the basic transaction information tx = self._create_skeleton_tx(fee) tx.from_address = Address(from_address) tx.target_contract(contract_digest, contract_address, shard_mask) tx.action = str(action) tx.data = self._encode_msgpack_payload(*args) for signer in signers: tx.add_signer(signer) encoded_tx = encode_transaction(tx, signers) return self._post_tx_json(encoded_tx, None)
def wealth(self, entity: Entity, amount: int): """ Creates wealth for specified account :param entity: The entity object to create wealth for :param amount: The amount of wealth to be generated :return: The digest of the submitted transaction :raises: ApiError on any failures """ ENDPOINT = 'wealth' # format the data to be closed by the transaction # wildcard for the moment shard_mask = BitVector() # build up the basic transaction information tx = self._create_skeleton_tx(1) tx.from_address = Address(entity) tx.target_chain_code(self.API_PREFIX, shard_mask) tx.action = 'wealth' tx.add_signer(entity) # format the transaction payload tx.data = self._encode_json({ 'address': entity.public_key, 'amount': amount }) # encode and sign the transaction encoded_tx = encode_transaction(tx, [entity]) # submit the transaction return self._post_tx_json(encoded_tx, ENDPOINT)
def test_multiple_transfers(self): EXPECTED_DIGEST = "7e6a95a8c773755d349209d8f3bb60ec2a5f3c683075540f953863f124eb1250" EXPECTED_PAYLOAD = \ "a1460000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d4014235130ac5aab442e3" \ "9f9aa27118956695229212dd2f1ab5b714e9f6bd581511c1010020f478c7f74b50c187bf9a8836f382bd62977bae" \ "eaf19625608e7e912aa60098c10200da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af" \ "6dc2000186a000000000000000000000000418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe" \ "08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_transfer(IDENTITIES[1], 256) payload.add_transfer(IDENTITIES[2], 512) payload.add_transfer(IDENTITIES[3], 100000) payload.add_signer(IDENTITIES[0]) # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)
def collect_stake(self, entity: Entity, fee: int): """ Collect all stakes that have reached the end of the cooldown period :param entity: The entity object that desires to collect :return: The digest of the submitted transaction :raises: ApiError on any failures """ ENDPOINT = 'collectStake' # format the data to be closed by the transaction # wildcard for the moment shard_mask = BitVector() # build up the basic transaction information tx = self._create_skeleton_tx(fee) tx.from_address = Address(entity) tx.target_chain_code(self.API_PREFIX, shard_mask) tx.action = 'collectStake' tx.add_signer(entity) # encode and sign the transaction encoded_tx = encode_transaction(tx, [entity]) # submit the transaction return self._post_tx_json(encoded_tx, ENDPOINT)
def de_stake(self, entity: Entity, amount: int, fee: int): """ Destakes a specific amount of tokens from a staking miner. This will put the tokens in a cool down period :param entity: The entity object that desires to destake :param amount: The amount of tokens to destake :return: The digest of the submitted transaction :raises: ApiError on any failures """ ENDPOINT = 'deStake' # format the data to be closed by the transaction # wildcard for the moment shard_mask = BitVector() # build up the basic transaction information tx = self._create_skeleton_tx(fee) tx.from_address = Address(entity) tx.target_chain_code(self.API_PREFIX, shard_mask) tx.action = 'deStake' tx.add_signer(entity) # format the transaction payload tx.data = self._encode_json({ 'address': entity.public_key, 'amount': amount }) # encode and sign the transaction encoded_tx = encode_transaction(tx, [entity]) # submit the transaction return self._post_tx_json(encoded_tx, ENDPOINT)
def test_contract_with_4bit_shard_mask(self): EXPECTED_PAYLOAD = "a12180532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d464c0c8c103e8c2000f42401c0b666f6f2e6261722e62617a066c61756e6368000418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" mask = BitVector(4) mask.set(3, 1) mask.set(2, 1) # build the payload bytes for the transaction payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.valid_from = 100 payload.valid_until = 200 payload.target_chain_code('foo.bar.baz', mask) payload.action = 'launch' # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx)
def transfer(self, entity: Entity, to: AddressLike, amount: int, fee: int): """ Transfers wealth from one account to another account :param private_key_bin: The bytes of the private key of the source address :param to_address: The bytes of the targeted address to send funds to :param amount: The amount of funds being transfered :param fee: The fee associated with the transfer :return: The digest of the submitted transaction :raises: ApiError on any failures """ ENDPOINT = 'transfer' # format the data to be closed by the transaction # wildcard for the moment shard_mask = BitVector() # build up the basic transaction information tx = Transaction() tx.from_address = Address(entity) tx.valid_until = 10000 tx.charge_rate = 1 tx.charge_limit = fee tx.add_transfer(to, amount) tx.add_signer(entity) # encode and sign the transaction encoded_tx = encode_transaction(tx, [entity]) # submit the transaction return self._post_tx_json(encoded_tx, ENDPOINT)
def test_validity_ranges(self): EXPECTED_PAYLOAD = "a12700532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d4024235130ac5aab442e39f9aa27118956695229212dd2f1ab5b714e9f6bd581511c103e820f478c7f74b50c187bf9a8836f382bd62977baeeaf19625608e7e912aa60098c103e8da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af6dc103e8e6672a9d98da667e5dc25b2bca8acf9644a7ac0797f01cb5968abf39de011df2c103e864c0c8c103e8c2000f42400418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_transfer(IDENTITIES[1], 1000) payload.add_transfer(IDENTITIES[2], 1000) payload.add_transfer(IDENTITIES[3], 1000) payload.add_transfer(IDENTITIES[4], 1000) payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.valid_from = 100 payload.valid_until = 200 # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx)
def create(self, owner: Entity, contract: 'Contract', fee: int): ENDPOINT = 'create' # format the data to be closed by the transaction # wildcard for the moment shard_mask = BitVector() # build up the basic transaction information tx = self._create_skeleton_tx(fee) tx.from_address = Address(owner) tx.target_chain_code(self.API_PREFIX, shard_mask) tx.action = ENDPOINT tx.data = self._encode_json({ 'text': contract.encoded_source, 'digest': contract.digest.to_hex() }) tx.add_signer(owner) # encode and sign the transaction encoded_tx = encode_transaction(tx, [owner]) # update the contracts owner contract.owner = owner # submit the transaction return self._post_tx_json(encoded_tx, ENDPOINT)
def test_synergetic_data_submission(self): EXPECTED_PAYLOAD = "a120c0532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d4c1271001c3000000e8d4a5100080da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af6de6672a9d98da667e5dc25b2bca8acf9644a7ac0797f01cb5968abf39de011df204646174610f7b2276616c7565223a20313233347d0418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction payload = Transaction() payload.from_address = IDENTITIES[0] payload.valid_until = 10000 payload.target_contract(IDENTITIES[3], IDENTITIES[4], BitVector()) payload.charge_rate = 1 payload.charge_limit = 1000000000000 payload.action = 'data' payload.synergetic_data_submission = True payload.data = json.dumps({'value': 1234}).encode('ascii') payload.add_signer(IDENTITIES[0]) # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx)
def create(self, owner: Entity, contract: 'Contract', fee: int, shard_mask: BitVector = None): ENDPOINT = 'create' logging.debug('Deploying contract', contract.address) # Default to wildcard shard mask if none supplied if not shard_mask: logging.warning( "Defaulting to wildcard shard mask as none supplied") shard_mask = BitVector() # build up the basic transaction information tx = self._create_skeleton_tx(fee) tx.from_address = Address(owner) tx.target_chain_code(self.API_PREFIX, shard_mask) tx.action = ENDPOINT tx.data = self._encode_json({ 'nonce': contract.nonce, 'text': contract.encoded_source, 'digest': contract.digest.to_hex() }) tx.add_signer(owner) # encode and sign the transaction encoded_tx = encode_transaction(tx, [owner]) # update the contracts owner contract.owner = owner # submit the transaction return self._post_tx_json(encoded_tx, ENDPOINT)
def test_contract_with_large_shard_mask(self): EXPECTED_DIGEST = "a4eff45d0374d29f259aba25fb06dd67394149b636927d199062102d91c0f7bf" EXPECTED_PAYLOAD = \ "a1418000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d464c0c8c103e8c2000f42" \ "4041eaab0b666f6f2e6261722e62617a066c61756e63680000000000000000000418c2a33af8bd2cba7fa714a840" \ "a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bb" \ "dc75a0251c" mask = BitVector(16) mask.set(15, 1) mask.set(14, 1) mask.set(13, 1) mask.set(11, 1) mask.set(9, 1) mask.set(7, 1) mask.set(5, 1) mask.set(3, 1) mask.set(1, 1) mask.set(0, 1) # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.valid_from = 100 payload.valid_until = 200 payload.target_chain_code('foo.bar.baz', mask) payload.action = 'launch' # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)
def action(self, contract_digest: Address, contract_owner: Address, action: str, fee: int, signers: EntityList, *args): shard_mask = BitVector() # build up the basic transaction information tx = self._create_skeleton_tx(fee) tx.from_address = Address(contract_owner) tx.target_contract(contract_digest, contract_owner, shard_mask) tx.action = str(action) tx.data = self._encode_msgpack_payload(*args) for signer in signers: tx.add_signer(signer) encoded_tx = encode_transaction(tx, signers) return self._post_tx_json(encoded_tx, None)
def submit_data(self, entity: Entity, digest: Address, **kwargs): # build up the basic transaction information tx = Transaction() tx.from_address = Address(entity) tx.valid_until = 10000 tx.target_contract(digest, Address(entity), BitVector()) tx.charge_rate = 1 tx.charge_limit = 1000000000000 tx.action = 'data' tx.synergetic_data_submission = True tx.data = self._encode_json(dict(**kwargs)) tx.add_signer(entity) # encode the transaction encoded_tx = encode_transaction(tx, [entity]) # submit the transaction to the catch-all endpoint return self._post_tx_json(encoded_tx, None)
def test_simple_transfer(self): EXPECTED_PAYLOAD = "a12400532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d44235130ac5aab442e39f9aa27118956695229212dd2f1ab5b714e9f6bd581511c101000000000418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_transfer(IDENTITIES[1], 256) payload.add_signer(IDENTITIES[0]) # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx)
def test_contract_with_4bit_shard_mask(self): EXPECTED_DIGEST = "7915d6393fb07dbb4ff6896ef0f57025e5153b744d3a652b0f4815f129a9033c" EXPECTED_PAYLOAD = \ "a1418000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d464c0c8c103e8c2000f42" \ "401c0b666f6f2e6261722e62617a066c61756e63680000000000000000000418c2a33af8bd2cba7fa714a840a308" \ "a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75" \ "a0251c" mask = BitVector(4) mask.set(3, 1) mask.set(2, 1) # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.valid_from = 100 payload.valid_until = 200 payload.target_chain_code('foo.bar.baz', mask) payload.action = 'launch' # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)
def test_validity_ranges(self): EXPECTED_DIGEST = "98f10e8aa0bf4507db9eca66f0ff0b6a3eff35fe4def9ed86150c7ce72e71e80" EXPECTED_PAYLOAD = \ "a1470000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d4024235130ac5aab442e3" \ "9f9aa27118956695229212dd2f1ab5b714e9f6bd581511c103e820f478c7f74b50c187bf9a8836f382bd62977bae" \ "eaf19625608e7e912aa60098c103e8da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af" \ "6dc103e8e6672a9d98da667e5dc25b2bca8acf9644a7ac0797f01cb5968abf39de011df2c103e864c0c8c103e8c2" \ "000f424000000000000000000418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f" \ "4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_transfer(IDENTITIES[1], 1000) payload.add_transfer(IDENTITIES[2], 1000) payload.add_transfer(IDENTITIES[3], 1000) payload.add_transfer(IDENTITIES[4], 1000) payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.valid_from = 100 payload.valid_until = 200 # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)
def test_multiple_transfers(self): EXPECTED_PAYLOAD = "a12600532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d4014235130ac5aab442e39f9aa27118956695229212dd2f1ab5b714e9f6bd581511c1010020f478c7f74b50c187bf9a8836f382bd62977baeeaf19625608e7e912aa60098c10200da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af6dc2000186a00000000418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_transfer(IDENTITIES[1], 256) payload.add_transfer(IDENTITIES[2], 512) payload.add_transfer(IDENTITIES[3], 100000) payload.add_signer(IDENTITIES[0]) # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx)
def test_synergetic_data_submission(self): EXPECTED_DIGEST = "9397fd490b60a394ea0af5526435608a1e853e2cb6b09bc7cafec8f6a0aa2cf6" EXPECTED_PAYLOAD = \ "a140c000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d4c1271001c3000000e8d4" \ "a5100080da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af6de6672a9d98da667e5dc2" \ "5b2bca8acf9644a7ac0797f01cb5968abf39de011df204646174610f7b2276616c7565223a20313233347d000000" \ "00000000000418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c" \ "258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.valid_until = 10000 payload.target_contract(IDENTITIES[3], IDENTITIES[4], BitVector()) payload.charge_rate = 1 payload.charge_limit = 1000000000000 payload.action = 'data' payload.synergetic_data_submission = True payload.data = json.dumps({'value': 1234}).encode('ascii') payload.add_signer(IDENTITIES[0]) # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)
def wealth(self, entity: Entity, amount: int): """ Creates wealth for specified account :param private_key_bin: The bytes of the private key of the targeted address :param amount: The amount of wealth to be generated :param fee: The fee value to be used for the transaction :return: The digest of the submitted transaction :raises: ApiError on any failures """ ENDPOINT = 'wealth' # format the data to be closed by the transaction # wildcard for the moment shard_mask = BitVector() # build up the basic transaction information tx = Transaction() tx.from_address = Address(entity) tx.valid_until = 10000 tx.charge_rate = 1 tx.charge_limit = 10 tx.target_chain_code(self.API_PREFIX, shard_mask) tx.action = 'wealth' tx.add_signer(entity) # format the transaction payload tx.data = self._encode_json({ 'address': entity.public_key, 'amount': amount }) # encode and sign the transaction encoded_tx = encode_transaction(tx, [entity]) # submit the transaction return self._post_tx_json(encoded_tx, ENDPOINT)
def test_smart_contract(self): EXPECTED_DIGEST = "032a72029ae2ac5cdbf9e07cf57d9ecab97a6de34ed5cdf785d1d98037cd5dcd" EXPECTED_PAYLOAD = \ "a1404000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d400c103e8c2000f424080" \ "da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af6de6672a9d98da667e5dc25b2bca8a" \ "cf9644a7ac0797f01cb5968abf39de011df2066c61756e636802676f00000000000000000418c2a33af8bd2cba7f" \ "a714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758b" \ "cc4961bbdc75a0251c" # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.target_contract(IDENTITIES[3], IDENTITIES[4], BitVector()) payload.action = 'launch' payload.data = 'go'.encode('ascii') # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)
def test_smart_contract(self): EXPECTED_PAYLOAD = "a12040532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d400c103e8c2000f424080da2e9c3191e3768d1c59ea43f6318367ed9b21e6974f46a60d0dd8976740af6de6672a9d98da667e5dc25b2bca8acf9644a7ac0797f01cb5968abf39de011df2066c61756e636802676f0418c2a33af8bd2cba7fa714a840a308a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75a0251c" # build the payload bytes for the transaction payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.target_contract(IDENTITIES[3], IDENTITIES[4], BitVector()) payload.action = 'launch' payload.data = 'go'.encode('ascii') # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx)
def test_chain_code(self): EXPECTED_DIGEST = "7032dd625b0a93aec85fa03696d0ecbc9de19d834ce3fd1d1ed9cf8a56d9f62e" EXPECTED_PAYLOAD = \ "a1408000532398dd883d1990f7dad3fde6a53a53347afc2680a04748f7f15ad03cadc4d400c103e8c2000f424080" \ "0b666f6f2e6261722e62617a066c61756e636802676f00000000000000000418c2a33af8bd2cba7fa714a840a308" \ "a217aa4483880b1ef14b4fdffe08ab956e3f4b921cec33be7c258cfd7025a2b9a942770e5b17758bcc4961bbdc75" \ "a0251c" # build the payload bytes for the transaction with mock.patch('random.getrandbits') as mock_counter: mock_counter.side_effect = [0] payload = Transaction() payload.from_address = IDENTITIES[0] payload.add_signer(IDENTITIES[0]) payload.charge_rate = 1000 payload.charge_limit = 1000000 payload.target_chain_code('foo.bar.baz', BitVector()) payload.action = 'launch' payload.data = 'go'.encode('ascii') # sign the final transaction transaction_bytes = encode_transaction(payload, [ENTITIES[0]]) self.assertIsExpectedTx(payload, transaction_bytes, EXPECTED_PAYLOAD) # attempt to decode a transaction from the generated bytes buffer = io.BytesIO(transaction_bytes) success, tx = decode_transaction(buffer) self.assertTrue(success) self.assertTxAreEqual(payload, tx) # Check payload digest buffer = io.BytesIO() encode_payload(buffer, payload) self.assertEqual(sha256_hash(buffer.getvalue()), EXPECTED_DIGEST)