import { handleHex } from './stringUtil' ; import { Transaction } from '../config/types' ; const Web3 = require ( 'web3' ) ; const rlp = require ( 'rlp' ) ; const elliptic = require ( 'elliptic' ) ; const ec = new elliptic . ec ( 'secp256k1' ) ; param { { nonce : string , gasPrice : string , gasLimit : string , to : string , value : string , data : string , chainId : number } } transaction return { Array < Buffer > } export const getRawHex = ( transaction ) : Array < Buffer > => { const rawData = [ ] ; rawData . push ( transaction . nonce ) ; rawData . push ( transaction . gasPrice ) ; 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' ) ; } ) ; raw [ 6 ] = Buffer . from ( [ transaction . chainId ] ) ; raw [ 7 ] = Buffer . allocUnsafe ( 0 ) ; raw [ 8 ] = Buffer . allocUnsafe ( 0 ) ; return raw ; } ; description param { Array < Buffer > } payload param { Number } v param { String } r param { String } s param { number } chainId return { String } export const composeSignedTransacton = ( payload , v , r , s , chainId ) : string => { const vValue = v + chainId * 2 + 8 ; const transaction = payload . slice ( 0 , 6 ) ; transaction . push ( Buffer . from ( [ vValue ] ) , Buffer . from ( r , 'hex' ) , Buffer . from ( s , 'hex' ) ) ; const serializedTx = rlp . encode ( transaction ) ; return `0x${ serializedTx . toString ( 'hex' ) }` ; } ; description param { { r : string , s : string } } canonicalSignature param { Buffer } payload param { String } compressedPubkey return { Promise < { v : Number , r : String , s : String } > } export const genEthSigFromSESig = async ( canonicalSignature , payload , compressedPubkey = undefined ) : Promise < { v : number ; r : string ; s : string ; } > => { const hash = Web3 . utils . keccak256 ( payload ) ; const data = Buffer . from ( handleHex ( hash ) , 'hex' ) ; const keyPair = ec . keyFromPublic ( compressedPubkey , 'hex' ) ; const recoveryParam = ec . getKeyRecoveryParam ( data , canonicalSignature , keyPair . pub ) ; const v = recoveryParam + 27 ; const { r } = canonicalSignature ; const { s } = canonicalSignature ; return { v , r , s } ; } ; description param { String } address return { Function } description param { string } hexString return { string } function trimFirst12Bytes ( hexString ) { return '0x' . concat ( hexString . substr ( hexString . length - 40 ) ) ; } param { string } compressedPubkey return { string } export function pubKeyToAddress ( compressedPubkey ) { const keyPair = ec . keyFromPublic ( compressedPubkey , 'hex' ) ; const pubkey = `0x${ keyPair . getPublic ( false , 'hex' ) . substr ( 2 ) }` ; const address = trimFirst12Bytes ( Web3 . utils . keccak256 ( pubkey ) ) ; return Web3 . utils . toChecksumAddress ( address ) ; }