From c46da2844b94a088d6e8c443254710b2e78117cc Mon Sep 17 00:00:00 2001 From: Seulgi Kim Date: Wed, 26 Jun 2019 14:37:10 +0900 Subject: [PATCH] Implement chain_getTransactionSigner --- rpc/src/v1/impls/chain.rs | 8 ++++++++ rpc/src/v1/traits/chain.rs | 4 ++++ spec/JSON-RPC.md | 31 +++++++++++++++++++++++++++++++ test/src/e2e/chain.test.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/rpc/src/v1/impls/chain.rs b/rpc/src/v1/impls/chain.rs index 3c17ca423f..155b1c2af5 100644 --- a/rpc/src/v1/impls/chain.rs +++ b/rpc/src/v1/impls/chain.rs @@ -77,6 +77,14 @@ where Ok(self.client.transaction(&id).map(From::from)) } + fn get_transaction_signer(&self, transaction_hash: H256) -> Result> { + let id = transaction_hash.into(); + Ok(self.client.transaction(&id).map(|mut tx| { + let address = public_to_address(&tx.signer()); + PlatformAddress::new_v1(tx.network_id, address) + })) + } + fn contains_transaction(&self, transaction_hash: H256) -> Result { Ok(self.client.transaction_block(&transaction_hash.into()).is_some()) } diff --git a/rpc/src/v1/traits/chain.rs b/rpc/src/v1/traits/chain.rs index ac39a62c8a..e0ce0e1c06 100644 --- a/rpc/src/v1/traits/chain.rs +++ b/rpc/src/v1/traits/chain.rs @@ -29,6 +29,10 @@ build_rpc_trait! { # [rpc(name = "chain_getTransaction")] fn get_transaction(&self, H256) -> Result>; + /// Gets the signer of transaction with given hash. + # [rpc(name = "chain_getTransactionSigner")] + fn get_transaction_signer(&self, H256) -> Result>; + /// Query whether the chain has the transaction with given transaction hash. # [rpc(name = "chain_containsTransaction")] fn contains_transaction(&self, H256) -> Result; diff --git a/spec/JSON-RPC.md b/spec/JSON-RPC.md index a6d2729477..6e5df7136c 100644 --- a/spec/JSON-RPC.md +++ b/spec/JSON-RPC.md @@ -281,6 +281,7 @@ When `Transaction` is included in any response, there will be an additional fiel * [chain_getBlockByHash](#chain_getblockbyhash) * [chain_getBlockTransactionCountByHash](#chain_getblocktransactioncountbyhash) * [chain_getTransaction](#chain_gettransaction) + * [chain_getTrnsactionSigner](#chain_gettrnsactionsigner) * [chain_containsTransaction](#chain_containstransaction) * [chain_getTransactionByTracker](#chain_gettransactionbytracker) * [chain_getAssetSchemeByTracker](#chain_getassetschemebytracker) @@ -721,6 +722,36 @@ Errors: `Invalid Params` [Back to **List of methods**](#list-of-methods) +## chain_getTrnsactionSigner +Returns the signer of the given transaction hash. + +It returns `null` if the transaction hash doesn't exist in the chain. + +### Params +1. tx hash: `H256` + +### Returns +`null` | `PlatformAddress` + +### Request Example +``` + curl \ + -H 'Content-Type: application/json' \ + -d '{"jsonrpc": "2.0", "method": "chain_getTrnsactionSigner", "params": ["0xdb7c705d02e8961880783b4cb3dc051c41e551ade244bed5521901d8de190fc6"], "id": "who-is-authors"}' \ + localhost:8080 +``` + +### Response Example +``` +{ + "jsonrpc":"2.0", + "result": "tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f", + "id": "who-is-authors" +} +``` + +[Back to **List of methods**](#list-of-methods) + ## chain_containsTransaction Returns true if the transaction with the given hash is in the chain. diff --git a/test/src/e2e/chain.test.ts b/test/src/e2e/chain.test.ts index 781f0dd98c..97bb3b8635 100644 --- a/test/src/e2e/chain.test.ts +++ b/test/src/e2e/chain.test.ts @@ -158,6 +158,36 @@ describe("chain", function() { expect(signed!.unsigned).to.deep.equal(tx); }); + it("sendPayTx, getTransactionSigner", async function() { + const tx = node.sdk.core.createPayTransaction({ + recipient: "tccqxv9y4cw0jwphhu65tn4605wadyd2sxu5yezqghw", + quantity: 0 + }); + const seq = await node.sdk.rpc.chain.getSeq(faucetAddress); + const hash = await node.sdk.rpc.chain.sendSignedTransaction( + tx.sign({ + secret: faucetSecret, + fee: 10, + seq + }) + ); + expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.true; + const signer = await node.sdk.rpc.sendRpcRequest( + "chain_getTransactionSigner", + [hash] + ); + expect(signer).equal(faucetAddress.toString()); + const signed = await node.sdk.rpc.chain.getTransaction(hash); + expect(signed).not.null; + expect(signed!.unsigned).to.deep.equal(tx); + expect( + node.sdk.core.classes.PlatformAddress.fromPublic( + signed!.getSignerPublic(), + { networkId: "tc" } + ).toString() + ).equal(signer); + }); + it("getRegularKey, getRegularKeyOwner", async function() { const key = node.sdk.util.getPublicFromPrivate( node.sdk.util.generatePrivateKey()