From 6cfb11d200a2c75c0b7747666055d347ae414067 Mon Sep 17 00:00:00 2001 From: Kik Engineering Date: Tue, 10 Nov 2020 17:28:27 -0500 Subject: [PATCH] webhook: add txId() to SignTransactionRequest GitOrigin-RevId: 74300591b0dd04fa6b1aa9c241c8e68569950742 --- CHANGELOG.md | 1 + src/webhook/index.ts | 12 ++++++++++++ test/webhook/webhook.spec.ts | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51af85a..8ad2eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/webhook/index.ts b/src/webhook/index.ts index 216614b..da0f67b 100644 --- a/src/webhook/index.ts +++ b/src/webhook/index.ts @@ -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 { diff --git a/test/webhook/webhook.spec.ts b/test/webhook/webhook.spec.ts index b45b8aa..42e868a 100644 --- a/test/webhook/webhook.spec.ts +++ b/test/webhook/webhook.spec.ts @@ -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, @@ -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()); +}); \ No newline at end of file