import * as rlp from 'rlp' ; import * as txUtil from './txUtil' ; import Transport from "../transport" ; import * as tx from '../apdu/transaction' ; import * as apdu from "../apdu/index" ; import { SDKError } from '../error/errorHandle' ; description param { String } keyId param { Buffer | Array < Buffer > } rawData param { String } readType return { String } export const prepareSEData = ( keyId , rawData , readType ) : string => { const inputIdBuffer = Buffer . from ( '00' , 'hex' ) ; const signDataBuffer = Buffer . from ( '00' , 'hex' ) ; const readTypeBuffer = Buffer . from ( readType , 'hex' ) ; const keyIdBuffer = Buffer . from ( keyId , 'hex' ) ; const data = [ inputIdBuffer , signDataBuffer , readTypeBuffer , keyIdBuffer , rawData ] ; const dataForSE = rlp . encode ( data ) ; return dataForSE . toString ( 'hex' ) ; } ; description param { Transport } transport param { String } appId param { String } appPrivateKey param { Array < { Function } > } preActions param { Array < { Function } > } actions param { Boolean } isEDDSA param { Function } txPrepareCompleteCallback param { Function } authorizedCallback param { Boolean } returnCanonical return { Promise < Array < { r : string , s : string } | Buffer > > } export const getSingleSignatureFromCoolWallet = async ( transport , preActions = undefined , action , isEDDSA = false , txPrepareCompleteCallback = undefined , authorizedCallback = undefined , returnCanonical = true ) => { if ( preActions ) { for ( const preAction of preActions ) { await preAction ( ) ; } } const encryptedSignature = await action ( ) ; if ( typeof txPrepareCompleteCallback === "function" ) txPrepareCompleteCallback ( ) ; await tx . finishPrepare ( transport ) ; if ( ! await tx . getTxDetail ( transport ) ) { throw new SDKError ( getSingleSignatureFromCoolWallet . name , 'get tx detail statusCode fail!!' ) ; } const signatureKey = await tx . getSignatureKey ( transport ) ; if ( typeof authorizedCallback === "function" ) { authorizedCallback ( ) ; } await tx . clearTransaction ( transport ) ; await apdu . mcu . control . powerOff ( transport ) ; const signature = txUtil . decryptSignatureFromSE ( encryptedSignature , signatureKey , isEDDSA , returnCanonical ) ; return signature ; } ; description param { Transport } transport param { String } appId param { String } appPrivateKey param { Array < { Function } > } preActions param { Array < { Function } > } actions param { Boolean } isEDDSA param { Function } txPrepareCompleteCallback param { Function } authorizedCallback param { Boolean } returnCanonical return { Promise < Array < { r : string , s : string } | Buffer > > } export const getSignaturesFromCoolWallet = async ( transport , preActions = undefined , actions , isEDDSA = false , txPrepareCompleteCallback = undefined , authorizedCallback = undefined , returnCanonical = true ) => { if ( preActions ) { for ( const preAction of preActions ) { await preAction ( ) ; } } const encryptedSignatureArray = [ ] ; for ( const action of actions ) { encryptedSignatureArray . push ( await action ( ) ) ; } if ( typeof txPrepareCompleteCallback === "function" ) txPrepareCompleteCallback ( ) ; await tx . finishPrepare ( transport ) ; if ( ! await tx . getTxDetail ( transport ) ) { throw new SDKError ( getSignaturesFromCoolWallet . name , 'get tx detail statusCode fail!!' ) ; } const signatureKey = await tx . getSignatureKey ( transport ) ; if ( typeof authorizedCallback === "function" ) { authorizedCallback ( ) ; } await tx . clearTransaction ( transport ) ; await apdu . mcu . control . powerOff ( transport ) ; const signatures = encryptedSignatureArray . map ( ( encryptedSignature ) => txUtil . decryptSignatureFromSE ( encryptedSignature , signatureKey , isEDDSA , returnCanonical ) ) ; return signatures ; } ;