import BackgroundController from './BackgroundController' ; import { ProviderOpts } from 'web3-provider-engine' ; import ethUtil = require ( 'ethereumjs-util' ) ; import sigUtil = require ( 'eth-sig-util' ) ; import Tx = require ( 'ethereumjs-tx' ) ; export type ApproveTransactionCallback = ( error : any , isApproved ? : boolean ) => void ; export type ApproveSignCallback = ( error : any , rawMsgSig ? : string ) => void ; export default class ProviderOptions { background : BackgroundController ; rpcUrl : string ; constructor ( background , rpcUrl ) { this . background = background ; this . rpcUrl = rpcUrl ; } getAccounts ( callback ) { this . background . getAccounts ( ) . then ( accounts => callback ( null , accounts ) ) . catch ( error => { callback ( error ) ; } ) ; } approveTransactionAlways ( txParams , callback ) { callback ( null , true ) ; } signTransaction ( rawTx , callback ) { this . background . getPrivateKey ( ) . then ( privateKey => { let tx = new Tx ( rawTx ) ; tx . sign ( privateKey ) ; let txHex = '0x' + Buffer . from ( tx . serialize ( ) ) . toString ( 'hex' ) ; callback ( null , txHex ) ; } ) . catch ( error => { callback ( error . message ) ; } ) ; } signMessageAlways ( messageParams , callback ) { this . background . getPrivateKey ( ) . then ( privateKey => { const msg = messageParams . data ; const hashBuf = new Buffer ( msg . split ( 'x' ) [ 1 ] , 'hex' ) ; const prefix = new Buffer ( '\x19Ethereum Signed Message:\n' ) ; const buf = Buffer . concat ( [ prefix , new Buffer ( String ( hashBuf . length ) ) , hashBuf ] ) ; const data = ethUtil . sha3 ( buf ) ; const msgSig = ethUtil . ecsign ( data , privateKey ) ; const rawMsgSig = ethUtil . bufferToHex ( sigUtil . concatSig ( msgSig . v , msgSig . r , msgSig . s ) ) ; callback ( null , rawMsgSig ) ; } ) . catch ( error => { callback ( error . message ) ; } ) ; } approving ( ) { return { static : { eth_syncing : false , web3_clientVersion : `LiteratePayments/v${ 1.0 }` } , rpcUrl : this . rpcUrl , getAccounts : this . getAccounts . bind ( this ) , approveTransaction : this . approveTransactionAlways . bind ( this ) , signTransaction : this . signTransaction . bind ( this ) , signMessage : this . signMessageAlways . bind ( this ) } ; } }