Skip to content

Commit 79f1b3b

Browse files
Seulgi Kimsgkim126
authored andcommitted
Implement chain_getTransactionSigner
1 parent 9ab133a commit 79f1b3b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

rpc/src/v1/impls/chain.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ where
7777
Ok(self.client.transaction(&id).map(From::from))
7878
}
7979

80+
fn get_transaction_signer(&self, transaction_hash: H256) -> Result<Option<PlatformAddress>> {
81+
let id = transaction_hash.into();
82+
Ok(self.client.transaction(&id).map(|mut tx| {
83+
let address = public_to_address(&tx.signer());
84+
PlatformAddress::new_v1(tx.network_id, address)
85+
}))
86+
}
87+
8088
fn contains_transaction(&self, transaction_hash: H256) -> Result<bool> {
8189
Ok(self.client.transaction_block(&transaction_hash.into()).is_some())
8290
}

rpc/src/v1/traits/chain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ build_rpc_trait! {
2929
# [rpc(name = "chain_getTransaction")]
3030
fn get_transaction(&self, H256) -> Result<Option<Transaction>>;
3131

32+
/// Gets the signer of transaction with given hash.
33+
# [rpc(name = "chain_getTransactionSigner")]
34+
fn get_transaction_signer(&self, H256) -> Result<Option<PlatformAddress>>;
35+
3236
/// Query whether the chain has the transaction with given transaction hash.
3337
# [rpc(name = "chain_containsTransaction")]
3438
fn contains_transaction(&self, H256) -> Result<bool>;

spec/JSON-RPC.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ When `Transaction` is included in any response, there will be an additional fiel
281281
* [chain_getBlockByHash](#chain_getblockbyhash)
282282
* [chain_getBlockTransactionCountByHash](#chain_getblocktransactioncountbyhash)
283283
* [chain_getTransaction](#chain_gettransaction)
284+
* [chain_getTrnsactionSigner](#chain_gettrnsactionsigner)
284285
* [chain_containsTransaction](#chain_containstransaction)
285286
* [chain_getTransactionByTracker](#chain_gettransactionbytracker)
286287
* [chain_getAssetSchemeByTracker](#chain_getassetschemebytracker)
@@ -721,6 +722,36 @@ Errors: `Invalid Params`
721722

722723
[Back to **List of methods**](#list-of-methods)
723724

725+
## chain_getTrnsactionSigner
726+
Returns the signer of the given transaction hash.
727+
728+
It returns `null` if the transaction hash doesn't exist in the chain.
729+
730+
### Params
731+
1. tx hash: `H256`
732+
733+
### Returns
734+
`null` | `PlatformAddress`
735+
736+
### Request Example
737+
```
738+
curl \
739+
-H 'Content-Type: application/json' \
740+
-d '{"jsonrpc": "2.0", "method": "chain_getTrnsactionSigner", "params": ["0xdb7c705d02e8961880783b4cb3dc051c41e551ade244bed5521901d8de190fc6"], "id": "who-is-authors"}' \
741+
localhost:8080
742+
```
743+
744+
### Response Example
745+
```
746+
{
747+
"jsonrpc":"2.0",
748+
"result": "tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f",
749+
"id": "who-is-authors"
750+
}
751+
```
752+
753+
[Back to **List of methods**](#list-of-methods)
754+
724755
## chain_containsTransaction
725756
Returns true if the transaction with the given hash is in the chain.
726757

test/src/e2e/chain.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ describe("chain", function() {
158158
expect(signed!.unsigned).to.deep.equal(tx);
159159
});
160160

161+
it("sendPayTx, getTransactionSigner", async function() {
162+
const tx = node.sdk.core.createPayTransaction({
163+
recipient: "tccqxv9y4cw0jwphhu65tn4605wadyd2sxu5yezqghw",
164+
quantity: 0
165+
});
166+
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
167+
const hash = await node.sdk.rpc.chain.sendSignedTransaction(
168+
tx.sign({
169+
secret: faucetSecret,
170+
fee: 10,
171+
seq
172+
})
173+
);
174+
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.true;
175+
const signer = await node.sdk.rpc.sendRpcRequest(
176+
"chain_getTransactionSigner",
177+
[hash]
178+
);
179+
expect(signer).equal(faucetAddress.toString());
180+
const signed = await node.sdk.rpc.chain.getTransaction(hash);
181+
expect(signed).not.null;
182+
expect(signed!.unsigned).to.deep.equal(tx);
183+
expect(
184+
node.sdk.core.classes.PlatformAddress.fromPublic(
185+
signed!.getSignerPublic(),
186+
{ networkId: "tc" }
187+
).toString()
188+
).equal(signer);
189+
});
190+
161191
it("getRegularKey, getRegularKeyOwner", async function() {
162192
const key = node.sdk.util.getPublicFromPrivate(
163193
node.sdk.util.generatePrivateKey()

0 commit comments

Comments
 (0)