Created
June 21, 2023 11:32
-
-
Save kind3r/788d0956554c364fa72a8bcedc8d3328 to your computer and use it in GitHub Desktop.
LedgerWallet
This file contains hidden or 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
import { Keypair, PublicKey, Transaction } from "@solana/web3.js"; | |
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid"; | |
import Solana from "@ledgerhq/hw-app-solana"; | |
import { Wallet } from "@project-serum/anchor/dist/esm/provider"; | |
export class LedgerWallet implements Wallet { | |
public publicKey: PublicKey; | |
public payer: Keypair; | |
private ledgerPath: string; | |
private solana: Solana; | |
public static async fromPath(path: string): Promise<LedgerWallet | void> { | |
try { | |
const transport = await TransportNodeHid.create(); | |
const solana = new Solana(transport); | |
const response = await solana.getAddress("44'/501'/5'"); | |
const address = new PublicKey(response.address); | |
const wallet = new LedgerWallet(path, address, solana); | |
return wallet; | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
private constructor(readonly path: string, address: PublicKey, solana: Solana) { | |
this.ledgerPath = path; | |
this.publicKey = address; | |
this.solana = solana; | |
} | |
public async signTransaction(tx: Transaction): Promise<Transaction> { | |
const signature = await this.solana.signTransaction(this.path, tx.serializeMessage()); | |
tx.addSignature(this.publicKey, signature.signature); | |
return tx; | |
} | |
public async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> { | |
for (const tx of txs) { | |
const signature = await this.solana.signTransaction(this.path, tx.serializeMessage()); | |
tx.addSignature(this.publicKey, signature.signature); | |
} | |
return txs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment