Skip to content

[rc-1.5.0] Implement chain_getTransactionSigner #1661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rpc/src/v1/impls/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ where
Ok(self.client.transaction(&id).map(From::from))
}

fn get_transaction_signer(&self, transaction_hash: H256) -> Result<Option<PlatformAddress>> {
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<bool> {
Ok(self.client.transaction_block(&transaction_hash.into()).is_some())
}
Expand Down
4 changes: 4 additions & 0 deletions rpc/src/v1/traits/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ build_rpc_trait! {
# [rpc(name = "chain_getTransaction")]
fn get_transaction(&self, H256) -> Result<Option<Transaction>>;

/// Gets the signer of transaction with given hash.
# [rpc(name = "chain_getTransactionSigner")]
fn get_transaction_signer(&self, H256) -> Result<Option<PlatformAddress>>;

/// Query whether the chain has the transaction with given transaction hash.
# [rpc(name = "chain_containsTransaction")]
fn contains_transaction(&self, H256) -> Result<bool>;
Expand Down
31 changes: 31 additions & 0 deletions spec/JSON-RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down
30 changes: 30 additions & 0 deletions test/src/e2e/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down