-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @piyalbasu