Skip to content
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

add keypair package in a second workspace #102

Merged
merged 5 commits into from
Mar 11, 2024
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: 5 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
project: [
"examples/tsconfig.json",
"@stellar/typescript-wallet-sdk/examples/tsconfig.json",
"@stellar/typescript-wallet-sdk/tsconfig.json",
"@stellar/typescript-wallet-sdk/test/tsconfig.json",
"@stellar/typescript-wallet-sdk-km/tsconfig.json",
"@stellar/typescript-wallet-sdk-km/test/tsconfig.json",
],
sourceType: "module",
},
Expand All @@ -28,6 +30,7 @@ module.exports = {
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/no-base-to-string": "off",
"jsdoc/check-indentation": "off",

// Warn
"jsdoc/check-param-names": "warn",
Expand Down Expand Up @@ -66,11 +69,10 @@ module.exports = {
"@typescript-eslint/no-empty-function": "error",
"@typescript-eslint/no-empty-interface": "error",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
},
overrides: [
{
files: ["test/**", "examples/**"],
files: ["**/test/**", "@stellar/typescript-wallet-sdk/examples/**"],
rules: {
"@typescript-eslint/no-shadow": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/npmPublish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: npm publish
name: npm publish wallet sdk
on:
release:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi I couldn't find a good way to auto publish two projects in the same mono repo. But I don't imagine the KeyManager code will be changing much, so I think publishing that manually shouldn't be to terrible?

and the main SDK will still have the auto publishing capability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types: [published]
Expand Down
5 changes: 5 additions & 0 deletions @stellar/typescript-wallet-sdk-km/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const parentConfig = require("../../babel.config");

module.exports = {
...parentConfig,
};
52 changes: 52 additions & 0 deletions @stellar/typescript-wallet-sdk-km/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@stellar/typescript-wallet-sdk-km",
"version": "1.0.0",
"engines": {
"node": ">=18"
},
"browser": "./lib/bundle_browser.js",
"main": "./lib/bundle.js",
"types": "./lib/index.d.ts",
"license": "Apache-2.0",
"private": false,
"devDependencies": {
"@babel/preset-env": "^7.24.0",
"@babel/preset-typescript": "^7.23.3",
"@stellar/prettier-config": "^1.0.1",
"@stellar/tsconfig": "^1.0.2",
"@types/jest": "^29.5.12",
"@typescript-eslint/parser": "^7.1.1",
"babel-jest": "^29.7.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
"jest-mock-random": "^1.1.1",
"node-localstorage": "^3.0.5",
"npm-run-all": "^4.1.5",
"sinon": "^17.0.1",
"ts-jest": "^29.1.2",
"ts-loader": "^9.5.1",
"tslib": "^2.6.2",
"typescript": "^5.3.3",
"webpack": "^5.90.3",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@albedo-link/intent": "^0.12.0",
"@ledgerhq/hw-app-str": "^6.28.4",
"@ledgerhq/hw-transport-u2f": "^5.36.0-deprecated",
"@stellar/freighter-api": "^2.0.0",
"@stellar/stellar-sdk": "^11.1.0",
"@trezor/connect-plugin-stellar": "^9.0.2",
"bignumber.js": "^9.1.2",
"scrypt-async": "^2.0.1",
"trezor-connect": "^8.2.12",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1"
},
"scripts": {
"prepare": "husky install",
"test": "jest --watchAll",
"test:ci": "jest --ci",
"build": "webpack --config webpack.config.js"
}
}
49 changes: 49 additions & 0 deletions @stellar/typescript-wallet-sdk-km/src/Handlers/albedo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import albedo from "@albedo-link/intent";
import {
Networks,
Transaction,
TransactionBuilder,
} from "@stellar/stellar-sdk";

import {
HandlerSignTransactionParams,
KeyTypeHandler,
KeyType,
} from "../Types";

export const albedoHandler: KeyTypeHandler = {
keyType: KeyType.albedo,
async signTransaction(params: HandlerSignTransactionParams) {
const { transaction, key } = params;

if (key.privateKey !== "") {
throw new Error(
`Non-ledger key sent to ledger handler: ${JSON.stringify(
key.publicKey,
)}`,
);
}

try {
const xdr = transaction.toXDR();
const response = await albedo.tx({ xdr });

if (!response.signed_envelope_xdr) {
throw new Error("We couldn’t sign the transaction with Albedo.");
}

// fromXDR() returns type "Transaction | FeeBumpTransaction" and
// signTransaction() doesn't like "| FeeBumpTransaction" type, so casting
// to "Transaction" type.
return TransactionBuilder.fromXDR(
response.signed_envelope_xdr,
Networks.PUBLIC,
) as Transaction;
} catch (error) {
const errorMsg = error.toString();
throw new Error(
`We couldn’t sign the transaction with Albedo. ${errorMsg}.`,
);
}
},
};
47 changes: 47 additions & 0 deletions @stellar/typescript-wallet-sdk-km/src/Handlers/freighter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import freighterApi from "@stellar/freighter-api";
import {
Networks,
Transaction,
TransactionBuilder,
} from "@stellar/stellar-sdk";

import {
HandlerSignTransactionParams,
KeyTypeHandler,
KeyType,
} from "../Types";

export const freighterHandler: KeyTypeHandler = {
keyType: KeyType.freighter,
async signTransaction(params: HandlerSignTransactionParams) {
const { transaction, key, custom } = params;

if (key.privateKey !== "") {
throw new Error(
`Non-ledger key sent to ledger handler: ${JSON.stringify(
key.publicKey,
)}`,
);
}

try {
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
// to "Transaction" type.
return TransactionBuilder.fromXDR(
response,
Networks.PUBLIC,
) as Transaction;
} catch (error) {
const errorMsg = error.toString();
throw new Error(
`We couldn’t sign the transaction with Freighter. ${errorMsg}.`,
);
}
},
};
6 changes: 6 additions & 0 deletions @stellar/typescript-wallet-sdk-km/src/Handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./albedo";
export * from "./freighter";
export * from "./ledger";
export * from "./plaintextKey";
// TODO - fix trezor errors
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving trezor commented out for a separate PR, since giving errors

we have this ticket for reaching out to trezor

// export * from "./trezor";
47 changes: 47 additions & 0 deletions @stellar/typescript-wallet-sdk-km/src/Handlers/ledger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import LedgerStr from "@ledgerhq/hw-app-str";
import LedgerTransport from "@ledgerhq/hw-transport-u2f";
import { Keypair, xdr } from "@stellar/stellar-sdk";

import {
HandlerSignTransactionParams,
KeyTypeHandler,
KeyType,
} from "../Types";

export const ledgerHandler: KeyTypeHandler = {
keyType: KeyType.ledger,
async signTransaction(params: HandlerSignTransactionParams) {
const { transaction, key } = params;

if (key.privateKey !== "") {
throw new Error(
`Non-ledger key sent to ledger handler: ${JSON.stringify(
key.publicKey,
)}`,
);
}

/*
There's a naive way to do this (to keep all functions stateless and
make the connection anew each time), and there's some way of weaving state
into this.

Gonna do the naive thing first and then figure out how to do this right.
*/
const transport = await LedgerTransport.create(60 * 1000);
const ledgerApi = new LedgerStr(transport);
const result = await ledgerApi.signTransaction(
key.path,
transaction.signatureBase(),
);

const keyPair = Keypair.fromPublicKey(key.publicKey);
const decoratedSignature = new xdr.DecoratedSignature({
hint: keyPair.signatureHint(),
signature: result.signature,
});
transaction.signatures.push(decoratedSignature);

return Promise.resolve(transaction);
},
};
36 changes: 36 additions & 0 deletions @stellar/typescript-wallet-sdk-km/src/Handlers/plaintextKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Keypair } from "@stellar/stellar-sdk";

import {
HandlerSignTransactionParams,
KeyTypeHandler,
KeyType,
} from "../Types";

export const plaintextKeyHandler: KeyTypeHandler = {
keyType: KeyType.plaintextKey,
signTransaction(params: HandlerSignTransactionParams) {
const { transaction, key } = params;
if (key.privateKey === "") {
throw new Error(
`Non-plaintext key sent to plaintext handler: ${JSON.stringify(
key.publicKey,
)}`,
);
}

const keyPair = Keypair.fromSecret(key.privateKey);

/*
* NOTE: we need to use the combo of getKeypairSignature() + addSignature()
* here in place of the shorter sign() call because sign() results in a
* "XDR Write Error: [object Object] is not a DecoratedSignature" error
* on React Native whenever we try to call transaction.toXDR() on the signed
* transaction.
*/

const signature = transaction.getKeypairSignature(keyPair);
transaction.addSignature(keyPair.publicKey(), signature);

return Promise.resolve(transaction);
},
};
62 changes: 62 additions & 0 deletions @stellar/typescript-wallet-sdk-km/src/Handlers/trezor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import TrezorConnect from "trezor-connect";
import transformTransaction from "@trezor/connect-plugin-stellar";

import {
HandlerSignTransactionParams,
KeyTypeHandler,
KeyType,
} from "../Types";

export const trezorHandler: KeyTypeHandler = {
keyType: KeyType.trezor,
async signTransaction(params: HandlerSignTransactionParams) {
const { transaction, key, custom } = params;

if (key.privateKey !== "") {
throw new Error(
`Non-ledger key sent to ledger handler: ${JSON.stringify(
key.publicKey,
)}`,
);
}

if (!custom || !custom.email || !custom.appUrl) {
throw new Error(
`Trezor Connect manifest with "email" and "appUrl" props is required.
Make sure they are passed through "custom" prop.`,
);
}

try {
TrezorConnect.manifest({
email: custom.email,
appUrl: custom.appUrl,
});

const trezorParams = transformTransaction("m/44'/148'/0'", transaction);
const response = await TrezorConnect.stellarSignTransaction(trezorParams);

if (response.success) {
const signature = Buffer.from(
response.payload.signature,
"hex",
).toString("base64");
transaction.addSignature(key.publicKey, signature);

return transaction;
} else {
throw new Error(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(response.payload as any).error ||
"We couldn’t sign the transaction with Trezor.",
);
}
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errorMsg = error.toString();
throw new Error(
`We couldn’t sign the transaction with Trezor. ${errorMsg}.`,
);
}
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { EncryptedKey, KeyMetadata } from "../Types";

export function getKeyMetadata(encryptedKey: EncryptedKey): KeyMetadata {
const { id } = encryptedKey;

return {
id,
};
}
2 changes: 2 additions & 0 deletions @stellar/typescript-wallet-sdk-km/src/Helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./getKeyMetadata";
export { decrypt, encrypt, NONCE_BYTES } from "./scryptEncryption";
Loading
Loading