Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Use @stellar/[email protected] for testnet transactions (#259)
Browse files Browse the repository at this point in the history
* use latest  so we can accept network config in

* rm package.lock

* add unit tests and fix falsy custom.network logic

* add types for mocks
  • Loading branch information
piyalbasu authored Aug 12, 2021
1 parent a6e19fe commit 93f278d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
79 changes: 79 additions & 0 deletions src/keyTypeHandlers/freighter.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
9 changes: 6 additions & 3 deletions src/keyTypeHandlers/freighter.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 93f278d

Please sign in to comment.