From 01ff093dfa174a774d67273835d4c2b1dca7a069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Thu, 10 Aug 2023 13:40:30 +0200 Subject: [PATCH] Modify transaction struct itself when calling sign() on it Instead of returning a signed copy. I think this API is more intuitive. I also don't want to do both, modify self and return a signed copy (because it's not possible to just return self, that is first-of-all a compiler error, and secondly would create a second instance in jS-land anyway), as that would transparently create a copy of the variable, which is unexpected behavior for users of the library and will lead only to errors. --- web-client/example/module.js | 15 ++++++++++----- web-client/src/transaction.rs | 7 ++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/web-client/example/module.js b/web-client/example/module.js index 010c31580b..c7ac2b3b41 100644 --- a/web-client/example/module.js +++ b/web-client/example/module.js @@ -68,7 +68,8 @@ init().then(async () => { BigInt(fee), await client.getHeadHeight(), await client.getNetworkId(), - ).sign(keyPair); + ); + transaction.sign(keyPair); } else { transaction = Nimiq.TransactionBuilder.newBasic( keyPair.toAddress(), @@ -77,7 +78,8 @@ init().then(async () => { BigInt(fee), await client.getHeadHeight(), await client.getNetworkId(), - ).sign(keyPair); + ); + transaction.sign(keyPair); } return client.sendTransaction(transaction.toHex()); @@ -104,7 +106,8 @@ init().then(async () => { BigInt(fee), await client.getHeadHeight(), await client.getNetworkId(), - ).sign(keyPair); + ); + transaction.sign(keyPair); return client.sendTransaction(transaction.toHex()); } @@ -128,7 +131,8 @@ init().then(async () => { BigInt(fee), await client.getHeadHeight(), await client.getNetworkId(), - ).sign(keyPair); + ); + transaction.sign(keyPair); return client.sendTransaction(transaction.toHex()); } @@ -152,7 +156,8 @@ init().then(async () => { BigInt(fee), await client.getHeadHeight(), await client.getNetworkId(), - ).sign(keyPair); + ); + transaction.sign(keyPair); return client.sendTransaction(transaction.toHex()); } diff --git a/web-client/src/transaction.rs b/web-client/src/transaction.rs index 448a4d7c1c..ec763a6330 100644 --- a/web-client/src/transaction.rs +++ b/web-client/src/transaction.rs @@ -140,12 +140,11 @@ impl Transaction { /// /// ### Limitations /// - HTLC redemption is not supported and will throw. - /// - Validator deletion transactions are not and cannot be supported. /// - For transaction to the staking contract, both signatures are made with the same keypair, /// so it is not possible to interact with a staker that is different from the sender address /// or using a different cold or signing key for validator transactions. #[cfg(feature = "primitives")] - pub fn sign(&self, key_pair: &KeyPair) -> Result { + pub fn sign(&mut self, key_pair: &KeyPair) -> Result<(), JsError> { let proof_builder = TransactionProofBuilder::new(self.native_ref().clone()); let signed_transaction = match proof_builder { TransactionProofBuilder::Basic(mut builder) => { @@ -196,7 +195,9 @@ impl Transaction { } }; - Ok(Transaction::from_native(signed_transaction)) + self.set_proof(signed_transaction.proof); + + Ok(()) } /// Computes the transaction's hash, which is used as its unique identifier on the blockchain.