import { handleHex } from './stringUtil' ; import { EIP1559Transaction } from '../config/types' ; const Web3 = require ( 'web3' ) ; const rlp = require ( 'rlp' ) ; const elliptic = require ( 'elliptic' ) ; const ec = new elliptic . ec ( 'secp256k1' ) ; export const getRawHex = ( transaction ) : Array < Buffer | Buffer [ ] > => { const rawData = [ ] ; rawData . push ( '01' ) ; rawData . push ( transaction . nonce ) ; rawData . push ( transaction . gasTipCap ) ; rawData . push ( transaction . gasFeeCap ) ; rawData . push ( transaction . gasLimit ) ; rawData . push ( transaction . to ) ; rawData . push ( transaction . value ) ; rawData . push ( transaction . data ) ; const raw = rawData . map ( ( d ) => { const hex = handleHex ( d ) ; if ( hex === '00' || hex === '' ) { return Buffer . allocUnsafe ( 0 ) ; } return Buffer . from ( hex , 'hex' ) ; } ) ; const emptyAccessList = [ ] as Buffer [ ] ; raw . push ( emptyAccessList ) ; return raw ; } ; export const composeSignedTransacton = ( payload , v , r , s ) : string => { const transaction = payload ; transaction . push ( Buffer . from ( [ v ] ) , Buffer . from ( r , 'hex' ) , Buffer . from ( s , 'hex' ) ) ; const serializedTx = rlp . encode ( transaction ) ; return `0x02${ serializedTx . toString ( 'hex' ) }` ; } ; export const genEthSigFromSESig = async ( canonicalSignature , payload , compressedPubkey = undefined ) : Promise < { v : number ; r : string ; s : string ; } > => { const prefixedPayload = Buffer . concat ( [ Buffer . from ( [ 2 ] ) , payload ] ) ; const hash = Web3 . utils . keccak256 ( prefixedPayload ) ; const data = Buffer . from ( handleHex ( hash ) , 'hex' ) ; const keyPair = ec . keyFromPublic ( compressedPubkey , 'hex' ) ; const recoveryParam = ec . getKeyRecoveryParam ( data , canonicalSignature , keyPair . pub ) ; const v = recoveryParam ; const { r } = canonicalSignature ; const { s } = canonicalSignature ; return { v , r , s } ; } ;