All files / app/hooks use-create-ledger-contract-call-tx.ts

0% Statements 0/33
0% Branches 0/5
0% Functions 0/6
0% Lines 0/30

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { bytesToHex } from '@stacks/common';
import {
  ContractCallOptions,
  createMessageSignature,
  deserializeTransaction,
  makeUnsignedContractCall,
  makeUnsignedSTXTokenTransfer,
  SingleSigSpendingCondition,
  TokenTransferOptions,
} from '@stacks/transactions';
import { RootState } from '@store/index';
import { selectPublicKey } from '@store/keys';
import { selectCoreNodeInfo, selectPoxInfo } from '@store/stacking';
import { LedgerError } from '@zondax/ledger-blockstack';
import { useCallback } from 'react';
import { useSelector } from 'react-redux';
 
function signTransactionWithSignature(transaction: string, signatureVRS: string) {
  const deserialzedTx = deserializeTransaction(transaction);
  const spendingCondition = createMessageSignature(signatureVRS);
  (deserialzedTx.auth.spendingCondition as SingleSigSpendingCondition).signature =
    spendingCondition;
  return deserialzedTx;
}
 
function useCreateLedgerTxFactory(
  method: typeof makeUnsignedContractCall | typeof makeUnsignedSTXTokenTransfer
) {
  const { publicKey, poxInfo, coreNodeInfo } = useSelector((state: RootState) => ({
    publicKey: selectPublicKey(state),
    poxInfo: selectPoxInfo(state),
    coreNodeInfo: selectCoreNodeInfo(state),
  }));
 
  return useCallback(
    async (options: TokenTransferOptions | ContractCallOptions) => {
      Iif (coreNodeInfo === null || !publicKey) throw new Error('Stacking requires coreNodeInfo');
 
      Iif (!poxInfo) throw new Error('`poxInfo` or `stacksApp` is not defined');
 
      console.log({ publicKey: publicKey.toString('hex') });
 
      const unsignedTx = await method({
        ...options,
        publicKey: publicKey.toString('hex'),
      } as any);
 
      const resp = await main.ledger.signTransaction(bytesToHex(unsignedTx.serialize()));
 
      Iif (resp.returnCode !== LedgerError.NoErrors) {
        throw new Error('Ledger responded with errors');
      }
      console.log('xxxxxxxxx', resp);
      return signTransactionWithSignature(
        bytesToHex(unsignedTx.serialize()),
        resp.signatureVRS as unknown as string
      );
    },
    [coreNodeInfo, method, poxInfo, publicKey]
  );
}
 
export function useCreateLedgerContractCallTx() {
  const method = useCreateLedgerTxFactory(makeUnsignedContractCall);
  const createLedgerContractCallTx = useCallback(method, [method]);
 
  return { createLedgerContractCallTx };
}
 
export function useCreateLedgerTokenTransferTx() {
  const method = useCreateLedgerTxFactory(makeUnsignedSTXTokenTransfer);
  const createLedgerTokenTransferTx = useCallback(method, [method]);
 
  return { createLedgerTokenTransferTx };
}