-
Notifications
You must be signed in to change notification settings - Fork 37
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
new module for ledger related utils #193
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8f40a33
new module for ledger related utils
dssei 2f7456b
pass with not tests
dssei 522c5a8
Add method docs
dssei 0285eb8
changeset
dssei 21a9e2a
Switch to offline signer implementation instead
dssei 4749846
Address review comments
dssei 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sei-js/ledger': minor | ||
--- | ||
|
||
Packge init + helper functions to work with Ledger in Cosmos Stargate client |
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 @@ | ||
node_modules | ||
dist |
Empty file.
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,13 @@ | ||
src | ||
node_modules | ||
docs | ||
coverage/ | ||
|
||
.gitkeep | ||
jest.config.ts | ||
tsconfig.json | ||
|
||
yarn-error.log | ||
|
||
.eslintignore | ||
eslintrc.json |
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 @@ | ||
# @sei-js/ledger | ||
|
||
## Installation | ||
```bash | ||
yarn add @sei-js/ledger | ||
``` | ||
|
||
## Usage | ||
```typescript | ||
import { | ||
coins, | ||
SigningStargateClient, | ||
StdFee | ||
} from "@cosmjs/stargate"; | ||
|
||
import { | ||
createTransportAndApp, | ||
getAddresses, | ||
LedgerOfflineAminoSigner | ||
} from "@sei-js/ledger"; | ||
|
||
const testApp = async () => { | ||
const validatorAddress = "seivaloper1sq7x0r2mf3gvwr2l9amtlye0yd3c6dqa4th95v"; | ||
const rpcUrl = "https://rpc-testnet.sei-apis.com/"; | ||
const memo = "Delegation"; | ||
const path = "m/44'/60'/0'/0/0"; | ||
|
||
const {app} = await createTransportAndApp(); | ||
const {nativeAddress} = await getAddresses(app, path); | ||
const ledgerSigner = new LedgerOfflineAminoSigner(app, path) | ||
const signingStargateClient = await SigningStargateClient.connectWithSigner(rpcUrl, ledgerSigner) | ||
|
||
const msgDelegate = { | ||
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", | ||
value: { | ||
delegatorAddress: nativeAddress.address, | ||
validatorAddress: validatorAddress, | ||
amount: coins(500, "usei"), | ||
}, | ||
}; | ||
|
||
const fee: StdFee = { | ||
amount: [{denom: "usei", amount: "20000"}], | ||
gas: "200000", | ||
}; | ||
|
||
const result = await signingStargateClient.signAndBroadcast(nativeAddress.address, [msgDelegate], fee, memo) | ||
console.log("Broadcast result:", result); | ||
}; | ||
|
||
testApp(); | ||
``` |
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,3 @@ | ||
{ | ||
"extends": ["../../eslint.base.json", "eslint:recommended", "plugin:@typescript-eslint/recommended"] | ||
} |
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,8 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
modulePathIgnorePatterns: ['<rootDir>/dist/'] | ||
}; |
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,50 @@ | ||
{ | ||
"name": "@sei-js/ledger", | ||
"version": "1.0.0", | ||
"description": "TypeScript library for SEI Ledger app helper functions", | ||
"main": "./dist/cjs/src/index.js", | ||
"module": "./dist/esm/src/index.js", | ||
"types": "./dist/types/src/index.d.ts", | ||
"sideEffects": false, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "yarn build:cjs && yarn build:esm && yarn build:prettier && yarn build:types", | ||
"build:cjs": "tsc --outDir dist/cjs --module commonjs", | ||
"build:esm": "tsc --outDir dist/esm --module esnext", | ||
"build:types": "tsc --project ./tsconfig.declaration.json", | ||
"build:prettier": "prettier --write 'dist/**/*.js'", | ||
"docs": "typedoc --out docs", | ||
"test": "jest --passWithNoTests", | ||
"lint": "eslint --ext .ts" | ||
}, | ||
"homepage": "https://github.com/sei-protocol/sei-js#readme", | ||
"keywords": [ | ||
"sei", | ||
"javascript", | ||
"typescript", | ||
"ledger" | ||
], | ||
"repository": "git@github.com:sei-protocol/sei-js.git", | ||
"license": "MIT", | ||
"private": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@cosmjs/stargate": "^0.32.4", | ||
"@ledgerhq/hw-transport-node-hid": "^6.29.3", | ||
"@zondax/ledger-sei": "^1.0.0" | ||
}, | ||
"peerDependencies": {}, | ||
"devDependencies": {}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/src/index.js", | ||
"require": "./dist/cjs/src/index.js", | ||
"types": "./dist/types/src/index.d.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 @@ | ||
export * from './wallet'; |
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 @@ | ||
export * from "./utils" |
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,77 @@ | ||
import Transport from '@ledgerhq/hw-transport-node-hid'; | ||
import { | ||
AminoSignResponse, | ||
encodeSecp256k1Signature, | ||
OfflineAminoSigner, | ||
serializeSignDoc, | ||
StdSignDoc | ||
} from '@cosmjs/amino'; | ||
import {fromHex } from '@cosmjs/encoding'; | ||
import { AccountData } from '@cosmjs/proto-signing'; | ||
import { Secp256k1Signature } from '@cosmjs/crypto'; | ||
import { SeiApp } from '@zondax/ledger-sei'; | ||
|
||
/** | ||
* A signer implementation that uses a Ledger device to sign transactions | ||
*/ | ||
export class LedgerOfflineAminoSigner implements OfflineAminoSigner { | ||
private readonly path: string; | ||
private readonly app: SeiApp; | ||
|
||
/** | ||
* Creates a new LedgerOfflineAminoSigner | ||
* @param app Ledger Sei app instance | ||
* @param path hd derivation path (e.g. "m/44'/60'/0'/0/0") | ||
*/ | ||
constructor(app: SeiApp, path: string) { | ||
this.path = path; | ||
this.app = app; | ||
} | ||
|
||
public async getAccounts(): Promise<readonly AccountData[]> { | ||
const nativeAddress = await this.app.getCosmosAddress(this.path); | ||
return [ | ||
{ | ||
address: nativeAddress.address, | ||
algo: "secp256k1", | ||
pubkey: fromHex(nativeAddress.pubKey) | ||
}, | ||
]; | ||
} | ||
|
||
public async signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse> { | ||
const signature = await this.app.signCosmos(this.path, Buffer.from(serializeSignDoc(signDoc))); | ||
const sig = new Secp256k1Signature(signature.r, signature.s).toFixedLength(); | ||
const nativeAddress = await this.app.getCosmosAddress(this.path); | ||
return { | ||
signed: signDoc, | ||
signature: encodeSecp256k1Signature(fromHex(nativeAddress.pubKey), sig), | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a transport and app instance | ||
* | ||
* @returns {Promise<{transport: Transport, app: SeiApp}>} transport and app instances | ||
*/ | ||
export const createTransportAndApp = async () => { | ||
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. nit: can we keep this in the 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. Done |
||
const transport = await Transport.create(); | ||
const app = new SeiApp(transport); | ||
return {transport, app}; | ||
}; | ||
|
||
/** | ||
* Get the EVM and Cosmos addresses from the Ledger device | ||
* @param app Ledger Sei app instance | ||
* @param path hd derivation path (e.g. "m/44'/60'/0'/0/0") | ||
* | ||
* @returns {Promise<{evmAddress: string, nativeAddress: string}>} EVM and Cosmos address objects containing | ||
* address and public key | ||
*/ | ||
export const getAddresses = async (app: SeiApp, path: string) => { | ||
const evmAddress = await app.getEVMAddress(path); | ||
const nativeAddress = await app.getCosmosAddress(path); | ||
return {evmAddress, nativeAddress}; | ||
}; | ||
|
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,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist/types", | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
"isolatedModules": false, | ||
"preserveConstEnums": false | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["./src/**/*" ], | ||
"compilerOptions": { | ||
"outDir": "./dist/types", | ||
"rootDir": "./", | ||
}, | ||
"typedocOptions": { | ||
"readme": "./README.md", | ||
"name": "registry", | ||
"entryPoints": ["src/index.ts"] | ||
} | ||
} |
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.
nice, this is great! should we name this
SeiLedgerOfflineAminoSigner
so it's more specific?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.
Done