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 KeyManager stuff from old wallet sdk #98

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -62,7 +63,6 @@ module.exports = {
"@typescript-eslint/no-empty-function": "error",
"@typescript-eslint/no-empty-interface": "error",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
},
overrides: [
{
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"eslint-plugin-jsdoc": "^46.8.2",
"husky": "^8.0.0",
"jest": "^29.4.1",
"jest-mock-random": "^1.1.1",
"lint-staged": "^14.0.1",
"node-localstorage": "^3.0.5",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
Expand All @@ -41,13 +43,23 @@
"webpack-cli": "^5.1.1"
},
"dependencies": {
"@albedo-link/intent": "^0.12.0",
"@ledgerhq/hw-app-str": "^6.28.3",
"@ledgerhq/hw-transport-u2f": "^5.36.0-deprecated",
"@stellar/freighter-api": "^1.7.1",
"@stellar/stellar-sdk": "^11.1.0",
"@trezor/connect-plugin-stellar": "^9.0.2",
"axios": "^1.4.0",
"bignumber.js": "^9.1.2",
"https-browserify": "^1.0.0",
"jws": "^4.0.0",
"lodash": "^4.17.21",
"query-string": "^7.1.3",
"scrypt-async": "^2.0.1",
"stream-http": "^3.2.0",
"trezor-connect": "^8.2.12",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1",
"url": "^0.11.0",
"util": "^0.12.5",
"utility-types": "^3.10.0"
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export {
TransactionBuilder,
SponsoringBuilder,
} from "./walletSdk/Horizon";
export { KeyManager } from "./walletSdk/KeyManager";
acharb marked this conversation as resolved.
Show resolved Hide resolved
export * as KeyManagerPlugins from "./walletSdk/KeyManager/plugins";
export { Recovery } from "./walletSdk/Recovery";
export { Watcher } from "./walletSdk/Watcher";

Expand Down
49 changes: 49 additions & 0 deletions src/walletSdk/KeyManager/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 src/walletSdk/KeyManager/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}.`,
);
}
},
};
47 changes: 47 additions & 0 deletions src/walletSdk/KeyManager/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 src/walletSdk/KeyManager/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 src/walletSdk/KeyManager/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}.`,
);
}
},
};
9 changes: 9 additions & 0 deletions src/walletSdk/KeyManager/helpers/getKeyMetadata.ts
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,
};
}
Loading
Loading