import { apdu } from "@coolwallet/core" ; import { blake2b } from './cryptoUtil' ; import * as types from '../config/types' ; import * as dotUtil from './dotUtil' ; import * as params from '../config/params' ; const elliptic = require ( 'elliptic' ) ; const ec = new elliptic . ec ( "secp256k1" ) ; const { encodeAddress , decodeAddress } = require ( '@polkadot/keyring' ) ; param { string } compressedPubkey return { string } export function pubKeyToAddress ( compressedPubkey , addressType ) { const zero = '0x' + Buffer . from ( blake2b ( compressedPubkey ) ) . toString ( 'hex' ) ; const address = encodeAddress ( zero , addressType ) ; return address ; } export async function getCompleteSignature ( transport , publicKey , canonicalSignature ) { if ( Buffer . isBuffer ( canonicalSignature ) ) { return '' ; } const { r , s } = canonicalSignature ; const { signedTx } = await apdu . tx . getSignedHex ( transport ) ; const keyPair = ec . keyFromPublic ( publicKey , "hex" ) ; const payloaBlake2bHash = blake2b ( signedTx ) ; const v = ec . getKeyRecoveryParam ( payloaBlake2bHash , canonicalSignature , keyPair . pub ) ; console . debug ( "v: " , v ) ; const sig = r + s + v . toString ( ) . padStart ( 2 , '0' ) ; return sig ; } returns export function getSubmitTransaction ( fromAddress , formatTxData , methodString , signature , version ) { const sumitTx = params . TX_ADDRESS_PRE + Buffer . from ( decodeAddress ( fromAddress ) ) . toString ( 'hex' ) + signature + formatTxData . mortalEra + formatTxData . encodeNonce + formatTxData . encodeTip + methodString ; const resultTx = dotUtil . addSignedTxLength ( dotUtil . addVersion ( sumitTx , version ) ) ; return '0x' + resultTx ; }