All files / app/main ledger-actions.ts

0% Statements 0/29
0% Branches 0/5
0% Functions 0/4
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                                                                             
import Transport from '@ledgerhq/hw-transport';
import { AddressVersion } from '@stacks/transactions';
import StacksApp, { LedgerError, ResponseAddress, ResponseSign } from '@zondax/ledger-blockstack';
 
const chainIdMap = {
  mainnet: AddressVersion.MainnetSingleSig,
  testnet: AddressVersion.TestnetSingleSig,
};
 
const STX_DERIVATION_PATH = `m/44'/5757'/0'/0/0`;
 
export async function ledgerRequestStxAddress(transport: Transport | null) {
  Iif (!transport) throw new Error('No device transport');
  const stacksApp = new StacksApp(transport);
  const resp = await stacksApp.showAddressAndPubKey(
    STX_DERIVATION_PATH,
    chainIdMap[process.env.STX_NETWORK as keyof typeof chainIdMap]
  );
  Iif (resp.publicKey) {
    return { ...resp, publicKey: resp.publicKey.toString('hex') };
  }
  return resp as Omit<ResponseAddress, 'publicKey'>;
}
 
export async function ledgerShowStxAddress(transport: Transport | null) {
  Iif (!transport) throw new Error('No device transport');
  const stacksApp = new StacksApp(transport);
  const resp = await stacksApp.getAddressAndPubKey(
    STX_DERIVATION_PATH,
    chainIdMap[process.env.STX_NETWORK as keyof typeof chainIdMap]
  );
  return resp;
}
 
export function ledgerRequestSignTx(transport: Transport | null) {
  return async (unsignedTransaction: string) => {
    Iif (!transport) throw new Error('No device transport');
 
    const stacksApp = new StacksApp(transport);
    const txBuffer = Buffer.from(unsignedTransaction, 'hex');
    const response: ResponseSign = await stacksApp.sign(STX_DERIVATION_PATH, txBuffer);
    await transport.close();
    Iif (response.returnCode !== LedgerError.NoErrors) {
      return response;
    }
    return {
      ...response,
      postSignHash: response.postSignHash.toString('hex'),
      signatureCompact: response.signatureCompact.toString('hex'),
      signatureVRS: response.signatureVRS.toString('hex'),
      signatureDER: response.signatureDER.toString('hex'),
    };
  };
}