def test_verifyAuthority(self): stm = self.bts stm.wallet.unlock("123") tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1.300 BBD", dpay_instance=stm), "memo": "Foobar"})) account = Account("dpaycli", dpay_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_broadcast(self): nodelist = NodeList() stm = DPay(node=nodelist.get_testnet(), keys=[self.active_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, expiration=10, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""})) tx.appendSigner("dpaycli", "active") tx.sign() tx.broadcast()
def test_transactionbuilder(self, node_param): if node_param == "instance": set_shared_dpay_instance(self.bts) o = TransactionBuilder() self.assertIn(o.dpay.rpc.url, self.urls) with self.assertRaises(RPCConnection): o = TransactionBuilder(dpay_instance=DPay( node="https://abc.d", autoconnect=False, num_retries=1)) o.dpay.get_config() else: set_shared_dpay_instance( DPay(node="https://abc.d", autoconnect=False, num_retries=1)) stm = self.bts o = TransactionBuilder(dpay_instance=stm) self.assertIn(o.dpay.rpc.url, self.urls) with self.assertRaises(RPCConnection): o = TransactionBuilder() o.dpay.get_config()
def test_transfer_1of1(self): dpay = self.bts dpay.nobroadcast = False tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli', "to": 'dpaycli1', "amount": Amount("0.01 BEX", dpay_instance=dpay), "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() dpay.nobroadcast = True
def test_transfer_2of2_wallet(self): # Send a 2 of 2 transaction from dpaycli5 which needs dpaycli4's cosign to send # priv key of dpaycli5 and dpaycli4 are stored in the wallet # appendSigner fetches both keys and signs automatically with both keys. dpay = self.bts dpay.nobroadcast = False dpay.wallet.unlock("123") tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli5', "to": 'dpaycli1', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '2 of 2 serialized/deserialized transaction'})) tx.appendSigner("dpaycli5", "active") tx.sign() self.assertEqual(len(tx['signatures']), 2) tx.broadcast() dpay.nobroadcast = True
def test_appendSigner(self): nodelist = NodeList() stm = DPay(node=nodelist.get_testnet(), keys=[self.active_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""})) account = Account("dpaycli", dpay_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)
from dpayclibase.transactions import getBlockParams log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) # example wif wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3" if __name__ == "__main__": stm_online = DPay() 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 = DPay(offline=True) op = operations.Transfer({ 'from': 'dpayclibot', 'to': 'holger80', 'amount': "0.001 BBD", 'memo': "" }) tb = TransactionBuilder(dpay_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(dpay_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": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""}) tx1 = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx1.appendOps(opTransfer) tx = TransactionBuilder(tx1, dpay_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("dpaycli", dpay_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 = DPay(node=nodelist.get_testnet(), keys=[self.posting_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""})) account = Account("dpaycli2", dpay_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 = DPay(node=nodelist.get_testnet(), nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_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 dpaycli4'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 dpaycli4's key. dpay = DPay( node=nodelist.get_testnet(), num_retries=10, keys=[self.active_private_key_of_dpaycli5], expiration=360, ) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli5', "to": 'dpaycli', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '2 of 2 serialized/deserialized transaction'})) tx.appendSigner("dpaycli5", "active") tx.addSigningInformation("dpaycli5", "active") tx.sign() tx.clearWifs() self.assertEqual(len(tx['signatures']), 1) tx_json = tx.json() del dpay del tx dpay = DPay( node=nodelist.get_testnet(), num_retries=10, keys=[self.active_private_key_of_dpaycli4], expiration=360, ) new_tx = TransactionBuilder(tx=tx_json, dpay_instance=dpay) 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 dpaycli5 which needs dpaycli4's cosign to send # funds but sign the transaction with dpaycli5's key and then serialize the transaction # and deserialize the transaction. After that, sign with dpaycli4's key. dpay = self.bts dpay.nobroadcast = False dpay.wallet.unlock("123") # dpay.wallet.removeAccount("dpaycli4") dpay.wallet.removePrivateKeyFromPublicKey(str(PublicKey(self.active_private_key_of_dpaycli4, prefix=core_unit))) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli5', "to": 'dpaycli', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '2 of 2 serialized/deserialized transaction'})) tx.appendSigner("dpaycli5", "active") tx.addSigningInformation("dpaycli5", "active") tx.sign() tx.clearWifs() self.assertEqual(len(tx['signatures']), 1) # dpay.wallet.removeAccount("dpaycli5") dpay.wallet.removePrivateKeyFromPublicKey(str(PublicKey(self.active_private_key_of_dpaycli5, prefix=core_unit))) dpay.wallet.addPrivateKey(self.active_private_key_of_dpaycli4) tx.appendMissingSignatures() tx.sign(reconstruct_tx=False) self.assertEqual(len(tx['signatures']), 2) tx.broadcast() dpay.nobroadcast = True dpay.wallet.addPrivateKey(self.active_private_key_of_dpaycli5)
def test_transfer_2of2_simple(self): # Send a 2 of 2 transaction from elf which needs dpaycli4's cosign to send funds dpay = self.bts dpay.nobroadcast = False tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli5', "to": 'dpaycli1', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '2 of 2 simple transaction'})) tx.appendWif(self.active_private_key_of_dpaycli5) tx.sign() tx.clearWifs() tx.appendWif(self.active_private_key_of_dpaycli4) tx.sign(reconstruct_tx=False) self.assertEqual(len(tx['signatures']), 2) tx.broadcast() dpay.nobroadcast = True