-
Notifications
You must be signed in to change notification settings - Fork 142
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
fix: ledger wallet #530
Merged
+94
−30
Merged
fix: ledger wallet #530
Changes from all commits
Commits
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,3 +1,3 @@ | ||
export * from './constant'; | ||
export * from './ledger'; | ||
export * from './web-usb-hid/registry'; | ||
export * from './constant'; |
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,7 +1,7 @@ | ||
import { ChainRecord, ChainWalletBase, Wallet } from '@cosmos-kit/core'; | ||
|
||
export class LedgerChianWallet extends ChainWalletBase { | ||
export class LedgerChainWallet extends ChainWalletBase { | ||
constructor(walletInfo: Wallet, chainInfo: ChainRecord) { | ||
super(walletInfo, chainInfo); | ||
} | ||
} | ||
} |
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,9 +1,17 @@ | ||
import { StdSignDoc } from '@cosmjs/amino'; | ||
import { | ||
encodeSecp256k1Signature, | ||
OfflineAminoSigner, | ||
StdSignDoc, | ||
} from '@cosmjs/amino'; | ||
import { sortedJsonStringify } from '@cosmjs/amino/build/signdoc'; | ||
import { Secp256k1Signature } from '@cosmjs/crypto'; | ||
import { fromHex } from '@cosmjs/encoding'; | ||
import { Algo } from '@cosmjs/proto-signing'; | ||
import { WalletClient } from '@cosmos-kit/core'; | ||
import { SignType, WalletClient } from '@cosmos-kit/core'; | ||
import Cosmos from '@ledgerhq/hw-app-cosmos'; | ||
|
||
import { ChainIdToBech32Prefix, getCosmosApp, getCosmosPath } from './utils'; | ||
|
||
export class LedgerClient implements WalletClient { | ||
client: Cosmos; | ||
|
||
|
@@ -39,16 +47,44 @@ export class LedgerClient implements WalletClient { | |
username: username ?? path, | ||
address, | ||
algo: 'secp256k1' as Algo, | ||
pubkey: new TextEncoder().encode(publicKey), | ||
pubkey: fromHex(publicKey), | ||
isNanoLedger: true, | ||
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. We want the publicKey as an hex byte array, not a UTF-8 byte array |
||
}; | ||
} | ||
|
||
getOfflineSigner(chainId: string, preferredSignType?: SignType) { | ||
// Ledger doesn't support direct sign, only Amino sign | ||
if (preferredSignType === 'direct') { | ||
throw new Error('Unsupported sign type: direct'); | ||
} | ||
return this.getOfflineSignerAmino(chainId); | ||
} | ||
|
||
getOfflineSignerAmino(chainId: string): OfflineAminoSigner { | ||
return { | ||
getAccounts: async () => { | ||
return [await this.getAccount(chainId)]; | ||
}, | ||
signAmino: async (_signerAddress, signDoc) => { | ||
const { pubkey } = await this.getAccount(chainId); | ||
const { signature: derSignature } = await this.sign(signDoc); // The signature is in DER format | ||
const signature = Secp256k1Signature.fromDer(derSignature); // Convert the DER signature to fixed length (64 bytes) | ||
return { | ||
signed: signDoc, | ||
signature: encodeSecp256k1Signature( | ||
pubkey, | ||
signature.toFixedLength() | ||
), | ||
}; | ||
}, | ||
}; | ||
} | ||
|
||
async sign(signDoc: StdSignDoc, accountIndex = 0) { | ||
if (!this.client) await this.initClient(); | ||
return await this.client.sign( | ||
getCosmosPath(accountIndex), | ||
JSON.stringify(signDoc) | ||
sortedJsonStringify(signDoc) // signDoc MUST be serialized in lexicographical key order | ||
); | ||
} | ||
} |
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
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,25 +1,25 @@ | ||
import { chains } from 'chain-registry' | ||
import Cosmos from "@ledgerhq/hw-app-cosmos"; | ||
import Cosmos from '@ledgerhq/hw-app-cosmos'; | ||
import TransportWebHID from '@ledgerhq/hw-transport-webhid'; | ||
import TransportWebUSB from '@ledgerhq/hw-transport-webusb'; | ||
import TransportWebHID from '@ledgerhq/hw-transport-webhid' | ||
import { chains } from 'chain-registry'; | ||
|
||
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. we should re-think this |
||
export type TransportType = 'WebUSB' | 'WebHID' | ||
export type TransportType = 'WebUSB' | 'WebHID'; | ||
|
||
export async function getCosmosApp(type: TransportType = 'WebUSB') { | ||
if (type === 'WebUSB') { | ||
return new Cosmos(await TransportWebUSB.create()) | ||
return new Cosmos(await TransportWebUSB.create()); | ||
} | ||
if (type === 'WebHID') { | ||
return new Cosmos(await TransportWebHID.create()) | ||
return new Cosmos(await TransportWebHID.create()); | ||
} | ||
throw new Error(`Unknown transport type: ${type}`) | ||
throw new Error(`Unknown transport type: ${type}`); | ||
} | ||
|
||
export function getCosmosPath(accountIndex = 0) { | ||
return `44'/118'/${accountIndex}'/0/0` | ||
return `44'/118'/${accountIndex}'/0/0`; | ||
} | ||
|
||
export const ChainIdToBech32Prefix = {} as { [k: string]: string } | ||
export const ChainIdToBech32Prefix = {} as { [k: string]: string }; | ||
for (const chain of chains) { | ||
ChainIdToBech32Prefix[chain.chain_id] = chain.bech32_prefix | ||
} | ||
ChainIdToBech32Prefix[chain.chain_id] = chain.bech32_prefix; | ||
} |
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
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.
Fix typo and prettify