Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
webhook: add txId() to SignTransactionRequest
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 74300591b0dd04fa6b1aa9c241c8e68569950742
  • Loading branch information
kikengineering committed Nov 11, 2020
1 parent 6ba23ad commit 6cfb11d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Add optional `subsidizer` parameter to `Client.create_account`, `Payment`, and `EarnBatch`
- Add optional `senderResolution` and `destinationResolution` parameters to `Client.submitPayment` and `Client.submitEarnBatch`
- Mark `tx_hash` property in `Event` as deprecated.
- Mark `SignTransactionRequest.txHash()` as deprecated in favour of `SignTransactionRequest.txId()`.

## [0.2.3](https://github.com/kinecosystem/kin-node/releases/tag/0.2.3)
- Add Kin 2 support
Expand Down
12 changes: 12 additions & 0 deletions src/webhook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,24 @@ export class SignTransactionRequest {
this.kinVersion = kinVersion;
}

/**
* @deprecated - Use `txId()` instead.
*/
txHash(): Buffer {
if (!this.envelope || !this.networkPassphrase) {
throw new Error("this transaction has no hash");
}
return TransactionBuilder.fromXDR(this.envelope!, this.networkPassphrase!).hash();
}

txId(): Buffer | undefined {
if (this.transaction) {
return this.transaction.signature;
}
if (this.envelope) {
return TransactionBuilder.fromXDR(this.envelope!, this.networkPassphrase!).hash();
}
}
}

export class SignTransactionResponse {
Expand Down
35 changes: 34 additions & 1 deletion test/webhook/webhook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
import request from "supertest";
import { hmac, sha256 } from "hash.js";
import { Keypair, xdr, TransactionBuilder } from "stellar-base";
import { Transaction as SolanaTransaction } from "@solana/web3.js";
import { Account as SolanaAccount, Transaction as SolanaTransaction, } from "@solana/web3.js";

import {
Event,
Expand Down Expand Up @@ -402,3 +402,36 @@ test("signTransactionHandler rejection Kin 4", async () => {
expect(invoiceErrors[i].reason).toBe(expectedReasons[i]);
}
});

test("signTransactionRequest getTxId", async () => {
const owner = PrivateKey.random();
const sender = PrivateKey.random().publicKey();
const destination = PrivateKey.random().publicKey();
const recentBlockhash = PrivateKey.random().publicKey();
const tokenProgram = PrivateKey.random().publicKey();

const transaction = new SolanaTransaction({
feePayer: sender.solanaKey(),
recentBlockhash: recentBlockhash.toBase58(),
}).add(
TokenProgram.transfer({
source: sender.solanaKey(),
dest: destination.solanaKey(),
owner: owner.publicKey().solanaKey(),
amount: BigInt(100),
}, tokenProgram.solanaKey(),
));
transaction.sign(new SolanaAccount(owner.secretKey()));

let req = new SignTransactionRequest([], 4, undefined, undefined, transaction);
expect(req.txId()).toEqual(transaction.signature);

const keypair = PrivateKey.random();
let envelope = xdr.TransactionEnvelope.fromXDR("AAAAAEUO4l6xxAcS8984GVe3Kq02DSZzOwojZCJsVqLtGbiyAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAKAAAAAQAAAABFDuJescQHEvPfOBlXtyqtNg0mczsKI2QibFai7Rm4sgAAAAEAAAAARQ7iXrHEBxLz3zgZV7cqrTYNJnM7CiNkImxWou0ZuLIAAAAAAAAAAAAAAAoAAAABAAAAAEUO4l6xxAcS8984GVe3Kq02DSZzOwojZCJsVqLtGbiyAAAAAQAAAADAp4yjwgs7DQ5hMiUyMqzpC22u6NWTXaX85D4qbzTj9wAAAAAAAAAAAAAACgAAAAEAAAAARQ7iXrHEBxLz3zgZV7cqrTYNJnM7CiNkImxWou0ZuLIAAAABAAAAALpctlQBhbHSdXACe6mk64mbrrl6DjRI5U7eAy2I3TUTAAAAAAAAAAAAAAAKAAAAAQAAAABFDuJescQHEvPfOBlXtyqtNg0mczsKI2QibFai7Rm4sgAAAAEAAAAAlbaWfZsuwTJg+gyJYp8vcDTwNWazt4rt+0K8TMkW374AAAAAAAAAAAAAAAoAAAABAAAAAEUO4l6xxAcS8984GVe3Kq02DSZzOwojZCJsVqLtGbiyAAAAAQAAAAA6KpnKS3rx9Vyqcj1oVWUHHXo9Tnf9t0ComjOg7C26AwAAAAAAAAAAAAAACgAAAAEAAAAARQ7iXrHEBxLz3zgZV7cqrTYNJnM7CiNkImxWou0ZuLIAAAABAAAAAGhVkpXOey36N862ZAPRVa2MAUJt93b4DRjarjSn9mZUAAAAAAAAAAAAAAAKAAAAAQAAAABFDuJescQHEvPfOBlXtyqtNg0mczsKI2QibFai7Rm4sgAAAAEAAAAA6BljUXmxqUtHbyBqIF09xdgf115SP4FbwFg+49en2IoAAAAAAAAAAAAAAAoAAAABAAAAAEUO4l6xxAcS8984GVe3Kq02DSZzOwojZCJsVqLtGbiyAAAAAQAAAABqBXeFh+UFtWbGv2hJ2jLYEQsfTY3aeE16LkP0S1P0MgAAAAAAAAAAAAAACgAAAAEAAAAARQ7iXrHEBxLz3zgZV7cqrTYNJnM7CiNkImxWou0ZuLIAAAABAAAAACFDsaY8xZjoFL3U9TZYdOdcAHOYD78JI/a9dY95sGNUAAAAAAAAAAAAAAAKAAAAAQAAAABFDuJescQHEvPfOBlXtyqtNg0mczsKI2QibFai7Rm4sgAAAAEAAAAAS8TkraTWvQD38UQZcDqEWKX7UPlUlQGwsZfKQ9O2KPIAAAAAAAAAAAAAAAoAAAAAAAAAAA==", "base64") ;
const builder = TransactionBuilder.fromXDR(envelope, NetworkPasshrase.Test);
builder.sign(keypair.kp);
envelope = builder.toEnvelope();

req = new SignTransactionRequest([], 3, NetworkPasshrase.Test, envelope);
expect(req.txId()).toEqual(builder.hash());
});

0 comments on commit 6cfb11d

Please sign in to comment.