diff --git a/package.json b/package.json index bc02c7cf..ed5c1897 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stellar/wallet-sdk", - "version": "0.4.0-rc.0", + "version": "0.5.0-rc.0", "description": "Libraries to help you write Stellar-enabled wallets in Javascript", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -81,7 +81,7 @@ "@albedo-link/intent": "^0.9.2", "@ledgerhq/hw-app-str": "^5.28.0", "@ledgerhq/hw-transport-u2f": "^5.26.0", - "@stellar/freighter-api": "^1.0.0-beta.0", + "@stellar/freighter-api": "^1.1.2", "@types/jest": "^24.0.11", "change-case": "^3.1.0", "lodash": "^4.17.14", diff --git a/src/keyTypeHandlers/freighter.test.ts b/src/keyTypeHandlers/freighter.test.ts new file mode 100644 index 00000000..3cf7d303 --- /dev/null +++ b/src/keyTypeHandlers/freighter.test.ts @@ -0,0 +1,79 @@ +import freighterApi from "@stellar/freighter-api"; +import sinon from "sinon"; +import { TransactionBuilder } from "stellar-sdk"; +import { freighterHandler } from "./freighter"; + +describe("freighterHandler", () => { + const XDR = "foo"; + const NETWORK = "baz"; + const SIGNED_TRANSACTION = "xxx"; + + let freighterApiMock: sinon.SinonMock; + let TransactionBuilderMock: sinon.SinonMock; + beforeEach(() => { + freighterApiMock = sinon.mock(freighterApi); + TransactionBuilderMock = sinon.mock(TransactionBuilder); + }); + afterEach(() => { + freighterApiMock.verify(); + freighterApiMock.restore(); + TransactionBuilderMock.verify(); + TransactionBuilderMock.restore(); + }); + test("signTransaction is called with network", () => { + freighterApiMock + .expects("signTransaction") + .once() + .withArgs(XDR, NETWORK) + .returns(Promise.resolve(SIGNED_TRANSACTION)); + TransactionBuilderMock.expects("fromXDR") + .once() + .withArgs(SIGNED_TRANSACTION) + .returns(true); + + freighterHandler.signTransaction({ + // @ts-ignore + transaction: { toXDR: () => XDR }, + // @ts-ignore + key: { privateKey: "" }, + custom: { network: NETWORK }, + }); + }); + test("signTransaction is called without network", () => { + freighterApiMock + .expects("signTransaction") + .once() + .withArgs(XDR, undefined) + .returns(Promise.resolve(SIGNED_TRANSACTION)); + TransactionBuilderMock.expects("fromXDR") + .once() + .returns(true); + + freighterHandler.signTransaction({ + // @ts-ignore + transaction: { toXDR: () => XDR }, + // @ts-ignore + key: { privateKey: "" }, + }); + }); + test("falsy config is passed as undefined to signTransaction", () => { + freighterApiMock + .expects("signTransaction") + .once() + .withArgs(XDR, undefined) + .returns(Promise.resolve(SIGNED_TRANSACTION)); + TransactionBuilderMock.expects("fromXDR") + .once() + .withArgs(SIGNED_TRANSACTION) + .returns(true); + + freighterHandler.signTransaction({ + // @ts-ignore + transaction: { toXDR: () => XDR }, + // @ts-ignore + key: { privateKey: "" }, + // @ts-ignore + custom: false, + }); + }); +}); diff --git a/src/keyTypeHandlers/freighter.ts b/src/keyTypeHandlers/freighter.ts index 41b003d1..d6b508bc 100644 --- a/src/keyTypeHandlers/freighter.ts +++ b/src/keyTypeHandlers/freighter.ts @@ -1,4 +1,4 @@ -import { signTransaction } from "@stellar/freighter-api"; +import freighterApi from "@stellar/freighter-api"; import { Networks, Transaction, TransactionBuilder } from "stellar-sdk"; import { HandlerSignTransactionParams, KeyTypeHandler } from "../types"; @@ -8,7 +8,7 @@ import { KeyType } from "../constants/keys"; export const freighterHandler: KeyTypeHandler = { keyType: KeyType.freighter, async signTransaction(params: HandlerSignTransactionParams) { - const { transaction, key } = params; + const { transaction, key, custom } = params; if (key.privateKey !== "") { throw new Error( @@ -19,7 +19,10 @@ export const freighterHandler: KeyTypeHandler = { } try { - const response = await signTransaction(transaction.toXDR()); + const response = await freighterApi.signTransaction( + transaction.toXDR(), + custom && custom.network ? custom.network : undefined, + ); // fromXDR() returns type "Transaction | FeeBumpTransaction" and // signTransaction() doesn't like "| FeeBumpTransaction" type, so casting diff --git a/yarn.lock b/yarn.lock index daab0fc5..40edc0e7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -930,10 +930,10 @@ resolved "https://registry.yarnpkg.com/@sinonjs/text-encoding/-/text-encoding-0.7.1.tgz#8da5c6530915653f3a1f38fd5f101d8c3f8079c5" integrity sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ== -"@stellar/freighter-api@^1.0.0-beta.0": - version "1.0.0-beta.0" - resolved "https://registry.yarnpkg.com/@stellar/freighter-api/-/freighter-api-1.0.0-beta.0.tgz#08c3f4ebb4d1b8c3e92039242ca5c877814864b4" - integrity sha512-wngITwqu9h4yfgqXvJS27VEkrlPh/u6uTDOPI2Ct8pZzde3fVPYGK+42izhGAe5oOvbziZQqHkHIPh+rkl7YHg== +"@stellar/freighter-api@^1.1.2": + version "1.1.2" + resolved "https://registry.yarnpkg.com/@stellar/freighter-api/-/freighter-api-1.1.2.tgz#9529352c7e4ac59087e14d16620d69322fca2e76" + integrity sha512-2vrPsOn1N3DYTff1763lM1eOy+LNKfkQPH5A3mXlKb3mDUK50wsyM6U03Y1DK/C/9dlMA49FspQ9Es2RF6NpPw== "@stellar/prettier-config@^1.0.1": version "1.0.1"