UNPKG

@ledgerhq/live-common

Version:
91 lines 4 kB
import xrpGetAddress from "@ledgerhq/coin-xrp/signer/getAddress"; import stellarGetAddress from "@ledgerhq/coin-stellar/signer/getAddress"; import Stellar from "@ledgerhq/hw-app-str"; import { signTransaction, stellarSignTransaction, tezosSignTransaction } from "./signTransaction"; import { StrKey } from "@stellar/stellar-sdk"; import { executeWithSigner } from "../../setup"; import Xrp from "@ledgerhq/hw-app-xrp"; import { DerivationType, LedgerSigner as TaquitoLedgerSigner } from "@taquito/ledger-signer"; import tezosGetAddress from "@ledgerhq/coin-tezos/signer/getAddress"; import Tezos from "@ledgerhq/hw-app-tezos"; const createSignerXrp = (transport) => { return new Xrp(transport); }; const signerContextXrp = executeWithSigner(createSignerXrp); const createSignerStellar = (transport) => { const stellar = new Stellar(transport); const originalSignTransaction = stellar.signTransaction; // Return the original Stellar instance with overridden methods return Object.assign(stellar, { signTransaction: async (path, transaction) => { const unsignedPayload = Buffer.from(transaction, "base64"); const { signature } = await originalSignTransaction(path, unsignedPayload); return signature.toString("base64"); }, getAddress: async (path, verify) => { const { rawPublicKey } = await stellar.getPublicKey(path, verify); const publicKey = StrKey.encodeEd25519PublicKey(rawPublicKey); return { path, address: publicKey, publicKey: publicKey, }; }, }); }; const signerContextStellar = executeWithSigner(createSignerStellar); const createSignerTezos = (transport) => { const tezos = new Tezos(transport); // align with genericSignOperation that calls signer.signTransaction return Object.assign(tezos, { async signTransaction(path, rawTxHex) { const { signature } = await tezos.signOperation(path, rawTxHex, {}); return signature; }, async getAddress(path, { verify } = {}) { // Use Taquito LedgerSigner to retrieve base58 public key and matching pkh (like oldbridge) const ledgerSigner = new TaquitoLedgerSigner(transport, path, !!verify, DerivationType.ED25519); const address = await ledgerSigner.publicKeyHash(); const publicKey = await ledgerSigner.publicKey(); return { path, address, publicKey }; }, createLedgerSigner(path, prompt, derivationType) { // Map 0 -> ED25519, 1 -> SECP256K1, 2 -> P256 by convention let dt = DerivationType.ED25519; if (derivationType === 1) dt = DerivationType.SECP256K1; else if (derivationType === 2) dt = DerivationType.P256; return new TaquitoLedgerSigner(transport, path, prompt, dt); }, }); }; const signerContextTezos = executeWithSigner(createSignerTezos); export function getSigner(network) { switch (network) { case "ripple": case "xrp": { return { getAddress: xrpGetAddress(signerContextXrp), signTransaction: signTransaction(signerContextXrp), context: signerContextXrp, }; } case "stellar": { return { getAddress: stellarGetAddress(signerContextStellar), signTransaction: stellarSignTransaction(signerContextStellar), context: signerContextStellar, }; } case "tezos": { return { getAddress: tezosGetAddress(signerContextTezos), signTransaction: tezosSignTransaction(executeWithSigner(createSignerTezos)), context: signerContextTezos, }; } } throw new Error(`signer for ${network} not implemented`); } //# sourceMappingURL=index.js.map