-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add keypair package in a second workspace (#102)
* add km package * fix tests * cleanup
- Loading branch information
Showing
44 changed files
with
10,773 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: npm publish | ||
name: npm publish wallet sdk | ||
on: | ||
release: | ||
types: [published] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const parentConfig = require("../../babel.config"); | ||
|
||
module.exports = { | ||
...parentConfig, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
@stellar/typescript-wallet-sdk-km/src/Handlers/freighter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}.`, | ||
); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// export * from "./trezor"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
@stellar/typescript-wallet-sdk-km/src/Handlers/plaintextKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
@stellar/typescript-wallet-sdk-km/src/Helpers/getKeyMetadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./getKeyMetadata"; | ||
export { decrypt, encrypt, NONCE_BYTES } from "./scryptEncryption"; |
Oops, something went wrong.