def test_transactionbuilder(self, node_param): if node_param == "instance": set_shared_crea_instance(self.bts) o = TransactionBuilder() self.assertIn(o.crea.rpc.url, self.urls) with self.assertRaises(RPCConnection): o = TransactionBuilder(crea_instance=Crea( node="https://abc.d", autoconnect=False, num_retries=1)) o.crea.get_config() else: set_shared_crea_instance( Crea(node="https://abc.d", autoconnect=False, num_retries=1)) stm = self.bts o = TransactionBuilder(crea_instance=stm) self.assertIn(o.crea.rpc.url, self.urls) with self.assertRaises(RPCConnection): o = TransactionBuilder() o.crea.get_config()
def test_verifyAuthority(self): stm = self.bts stm.wallet.unlock("123") tx = TransactionBuilder(use_condenser_api=True, crea_instance=stm) tx.appendOps( Transfer( **{ "from": "crea", "to": "crea1", "amount": Amount("1.300 CBD", crea_instance=stm), "memo": "Foobar" })) account = Account("crea", crea_instance=stm) tx.appendSigner(account, "active") self.assertTrue(len(tx.wifs) > 0) tx.sign() tx.verify_authority() self.assertTrue(len(tx["signatures"]) > 0)
def test_transfer_1of1(self): crea = self.bts crea.nobroadcast = False tx = TransactionBuilder(use_condenser_api=True, crea_instance=crea) tx.appendOps( Transfer( **{ "from": 'crea', "to": 'crea1', "amount": Amount("0.01 CREA", crea_instance=crea), "memo": '1 of 1 transaction' })) self.assertEqual(tx["operations"][0]["type"], "transfer_operation") tx.appendWif(self.active_key) tx.sign() tx.sign() self.assertEqual(len(tx['signatures']), 1) tx.broadcast() crea.nobroadcast = True
def test_Transfer_broadcast(self): nodelist = NodeList() stm = Crea(node=self.nodes, keys=[self.active_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, expiration=10, crea_instance=stm) tx.appendOps( Transfer( **{ "from": "crea", "to": "crea1", "amount": Amount("1 CREA", crea_instance=stm), "memo": "" })) tx.appendSigner("crea", "active") tx.sign() tx.broadcast()
def test_appendSigner(self): nodelist = NodeList() stm = Crea(node=self.nodes, keys=[self.active_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, crea_instance=stm) tx.appendOps( Transfer( **{ "from": "crea", "to": "crea1", "amount": Amount("1 CREA", crea_instance=stm), "memo": "" })) account = Account("crea", crea_instance=stm) with self.assertRaises(AssertionError): tx.appendSigner(account, "abcdefg") tx.appendSigner(account, "active") self.assertTrue(len(tx.wifs) > 0) tx.sign() self.assertTrue(len(tx["signatures"]) > 0)
def test_transfer_2of2_wallet(self): # Send a 2 of 2 transaction from crea5 which needs crea4's cosign to send # priv key of crea5 and crea4 are stored in the wallet # appendSigner fetches both keys and signs automatically with both keys. crea = self.bts crea.nobroadcast = False crea.wallet.unlock("123") tx = TransactionBuilder(use_condenser_api=True, crea_instance=crea) tx.appendOps( Transfer( **{ "from": 'crea5', "to": 'crea1', "amount": Amount("0.01 CREA", crea_instance=crea), "memo": '2 of 2 serialized/deserialized transaction' })) tx.appendSigner("crea5", "active") tx.sign() self.assertEqual(len(tx['signatures']), 2) tx.broadcast() crea.nobroadcast = True
from crea.nodelist import NodeList from creabase.transactions import getBlockParams log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) # example wif wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3" if __name__ == "__main__": stm_online = Crea() ref_block_num, ref_block_prefix = getBlockParams(stm_online) print("ref_block_num %d - ref_block_prefix %d" % (ref_block_num, ref_block_prefix)) stm = Crea(offline=True) op = operations.Transfer({ 'from': 'creabot', 'to': 'holger80', 'amount': "0.001 CBD", 'memo': "" }) tb = TransactionBuilder(crea_instance=stm) tb.appendOps([op]) tb.appendWif(wif) tb.constructTx(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix) tx = tb.sign(reconstruct_tx=False) print(tx.json())
def test_emptyTransaction(self): stm = self.stm tx = TransactionBuilder(crea_instance=stm) self.assertTrue(tx.is_empty()) self.assertTrue(tx["ref_block_num"] is not None)
def test_TransactionConstructor(self): stm = self.bts opTransfer = Transfer( **{ "from": "crea", "to": "crea1", "amount": Amount("1 CREA", crea_instance=stm), "memo": "" }) tx1 = TransactionBuilder(use_condenser_api=True, crea_instance=stm) tx1.appendOps(opTransfer) tx = TransactionBuilder(tx1, crea_instance=stm) self.assertFalse(tx.is_empty()) self.assertTrue(len(tx.list_operations()) == 1) self.assertTrue(repr(tx) is not None) self.assertTrue(str(tx) is not None) account = Account("crea", crea_instance=stm) tx.appendSigner(account, "active") self.assertTrue(len(tx.wifs) > 0) tx.sign() self.assertTrue(len(tx["signatures"]) > 0)
def test_verifyAuthorityException(self): nodelist = NodeList() stm = Crea(node=self.nodes, keys=[self.posting_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, crea_instance=stm) tx.appendOps( Transfer( **{ "from": "crea", "to": "crea1", "amount": Amount("1 CREA", crea_instance=stm), "memo": "" })) account = Account("crea2", crea_instance=stm) tx.appendSigner(account, "active") tx.appendWif(self.posting_key) self.assertTrue(len(tx.wifs) > 0) tx.sign() with self.assertRaises(exceptions.MissingRequiredActiveAuthority): tx.verify_authority() self.assertTrue(len(tx["signatures"]) > 0)
def test_appendWif(self): nodelist = NodeList() stm = Crea(node=self.nodes, nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, crea_instance=stm) tx.appendOps( Transfer( **{ "from": "crea", "to": "crea1", "amount": Amount("1 CREA", crea_instance=stm), "memo": "" })) with self.assertRaises(MissingKeyError): tx.sign() with self.assertRaises(InvalidWifError): tx.appendWif("abcdefg") tx.appendWif(self.active_key) tx.sign() self.assertTrue(len(tx["signatures"]) > 0)
def test_transfer_2of2_wif(self): nodelist = NodeList() # Send a 2 of 2 transaction from elf which needs crea4's cosign to send # funds but sign the transaction with elf's key and then serialize the transaction # and deserialize the transaction. After that, sign with crea4's key. crea = Crea( node=self.nodes, num_retries=10, keys=[self.active_private_key_of_crea5], expiration=360, ) tx = TransactionBuilder(use_condenser_api=True, crea_instance=crea) tx.appendOps( Transfer( **{ "from": 'crea5', "to": 'crea', "amount": Amount("0.01 CREA", crea_instance=crea), "memo": '2 of 2 serialized/deserialized transaction' })) tx.appendSigner("crea5", "active") tx.addSigningInformation("crea5", "active") tx.sign() tx.clearWifs() self.assertEqual(len(tx['signatures']), 1) tx_json = tx.json() del crea del tx crea = Crea( node=self.nodes, num_retries=10, keys=[self.active_private_key_of_crea4], expiration=360, ) new_tx = TransactionBuilder(tx=tx_json, crea_instance=crea) new_tx.appendMissingSignatures() new_tx.sign(reconstruct_tx=False) self.assertEqual(len(new_tx['signatures']), 2) new_tx.broadcast()
def test_transfer_2of2_offline(self): # Send a 2 of 2 transaction from crea5 which needs crea4's cosign to send # funds but sign the transaction with crea5's key and then serialize the transaction # and deserialize the transaction. After that, sign with crea4's key. crea = self.bts crea.nobroadcast = False crea.wallet.unlock("123") # crea.wallet.removeAccount("crea4") crea.wallet.removePrivateKeyFromPublicKey( str(PublicKey(self.active_private_key_of_crea4, prefix=core_unit))) tx = TransactionBuilder(use_condenser_api=True, crea_instance=crea) tx.appendOps( Transfer( **{ "from": 'crea5', "to": 'crea', "amount": Amount("0.01 CREA", crea_instance=crea), "memo": '2 of 2 serialized/deserialized transaction' })) tx.appendSigner("crea5", "active") tx.addSigningInformation("crea5", "active") tx.sign() tx.clearWifs() self.assertEqual(len(tx['signatures']), 1) # crea.wallet.removeAccount("crea5") crea.wallet.removePrivateKeyFromPublicKey( str(PublicKey(self.active_private_key_of_crea5, prefix=core_unit))) crea.wallet.addPrivateKey(self.active_private_key_of_crea4) tx.appendMissingSignatures() tx.sign(reconstruct_tx=False) self.assertEqual(len(tx['signatures']), 2) tx.broadcast() crea.nobroadcast = True crea.wallet.addPrivateKey(self.active_private_key_of_crea5)
def test_transfer_2of2_simple(self): # Send a 2 of 2 transaction from elf which needs crea4's cosign to send funds crea = self.bts crea.nobroadcast = False tx = TransactionBuilder(use_condenser_api=True, crea_instance=crea) tx.appendOps( Transfer( **{ "from": 'crea5', "to": 'crea1', "amount": Amount("0.01 CREA", crea_instance=crea), "memo": '2 of 2 simple transaction' })) tx.appendWif(self.active_private_key_of_crea5) tx.sign() tx.clearWifs() tx.appendWif(self.active_private_key_of_crea4) tx.sign(reconstruct_tx=False) self.assertEqual(len(tx['signatures']), 2) tx.broadcast() crea.nobroadcast = True