keychain.js- Keychain class for working with the KeyChain WebSocketkeychainWeb3.js- KeychainWeb3 class with methodssignandsingTransactionfor substitutingweb3.eth.accounts.signandweb3.eth.accounts.signTransactionmethods
Usage
- Sign ethereum transaction
const{ Keychain, KeychainWeb3 }=require('keychain.js');constWeb3=require('web3');constweb3=newWeb3('YOUR_API_URL');// https://ropsten.infura.io/v3/046804e3dd3240b09834531326f310cfconsttx={to: '0xE8899BA12578d60e4D0683a596EDaCbC85eC18CC',value: 100,gas: 21000};constkeychain=newKeychain();constkeychainWeb3=newKeychainWeb3(keychain,web3);keychain.selectKey().then(publicKey=>keychainWeb3.signTransaction(tx,publicKey)).then(result=>web3.eth.sendSignedTransaction(result.rawTransaction));- Sign & build ready for broadcast bitcoin transaction
const{ Keychain }=require('keychain.js');constbitcoin=require('bitcoinjs-lib');constkeychain=newKeychain();constpublicKey=awaitkeychain.selectKey();consttx={from: addressFromPublicKey(publicKey),to: 'mqkrYyihgXVUZisi452KQ4tpTsaE8Tk8uj',amount: 20000,feeValue: 226};consttxb=newbitcoin.TransactionBuilder(bitcoin.networks.testnet);constunspents=awaitfetchUnspents(tx.from);consttotalUnspent=unspents.reduce((summ,{ satoshis })=>summ+satoshis,0);constchangeAmount=totalUnspent-tx.amount-tx.feeValue;unspents.forEach(({ txid, vout })=>txb.addInput(txid,vout,0xfffffffe));txb.addOutput(tx.to,tx.amount);if(changeAmount>546){txb.addOutput(tx.from,changeAmount);}consttxRaw=txb.buildIncomplete();// add input scripts to an unsigned transaction https://github.com/bitcoinjs/bitcoinjs-lib/issues/1011#issuecomment-368394185unspents.forEach(({ scriptPubKey },index)=>txRaw.ins[index].script=Buffer.from(scriptPubKey,'hex'));constrawHex=awaitkeychain.signTrx(txRaw.toHex(),publicKey,'bitcoin');// broadcast rawHex transactionFull example of signing and broadcasting bitcoin transaction can be found in example/signBTC.js
Run tests
npm run test
Add key to your key_data:
keyname: test1@76de427d42c38be4
password: qwe
