1. 程式人生 > >以太坊和ERC20代幣批量簽名

以太坊和ERC20代幣批量簽名

一.批量簽名

本地簽名:這裡是有nodeJS

1.以太坊

var bip39 = require('bip39')
const bip32 = require('bip32')
const bitcoin = require('bitcoinjs-lib')
const baddress = require('bitcoinjs-lib/src/address')
const bcrypto = require('bitcoinjs-lib/src/crypto')
const NETWORKS = require('bitcoinjs-lib/src/networks')
const hdkey =  require('ethereumjs-wallet/hdkey')
const util = require('ethereumjs-util')
const Web3 =require ('web3')
const transaction = require('ethereumjs-tx');
if (typeof web3 !== 'undefined')
{
    var web3 = new Web3(web3.currentProvider);
} else {
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

function MultiSig(sendData) {
    if(sendData == null) {
        console.log("param is invalid, sendData is null");
    }
    console.log("param is valid, start multiSign transaction");
    var calcNonce = sendData.nonce;
    var arrData = sendData.signDta;
    var outArr = [];
    for(var i = 0; i < arrData.length; i++){
        var transactionNonce = parseInt(calcNonce).toString(16);
        var balancetoWei = web3.toWei(parseFloat(sendData.signDta[i].value), "ether");
        var balanceValue = parseInt(balancetoWei).toString(16);
        var gas = web3.toWei(parseFloat(sendData.gas), "ether");
        var oxGas = parseInt(gas).toString(16);
        var gasPrice = web3.toWei(parseFloat(sendData.gasPrice), "ether");
        var oxGasPrice = parseInt(gasPrice).toString(16);
        var privateKeyBuffer =  Buffer.from(sendData.privateKey, 'hex');
        var rawTx = {
            nonce:'0x' + transactionNonce,
            gasPrice: oxGasPrice,
            gas:oxGas,
            to: sendData.signDta[i].toAddress,
            value:'0x' + balanceValue,
            data: '',
            chainId: 1
        };
        var tx = new transaction(rawTx);
        tx.sign(privateKeyBuffer);
        var serializedTx = tx.serialize();
        if(serializedTx == null) {
            console.log("Serialized transaction fail")
        } else {
            outArr = outArr.concat(serializedTx.toString('hex'))
            if (tx.verifySignature()) {
                console.log('Signature Checks out!')
            } else {
                console.log("Signature checks fail")
            }
        }
        calcNonce = calcNonce + 1;
    }
   return { signCoin:"ETH", signDataArr:outArr}
}

console.log(MultiSig(sendData));

測試資料

	var sendData =
	{
	    "signMark":"ETH",
	    "privateKey":"5204627f8e58b3c75be408423c121988a1cb942e73470fb19186ef8986cd50b3",
	    "gasPrice":3000,
	    "gas":1000,
	    "nonce":1,
	    "signDta":[
	        {
	            "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
	            "value":100000,
	        },{
	            "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
	            "value":100000,
	        },{
	            "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
	            "value":100000,
	        },{
	            "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
	            "value":100000,
	        },{
	            "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
	            "value":100000,
	        }
	    ]
	}

2.ERC20批量簽名

var bip39 = require('bip39')
const bip32 = require('bip32')
const bitcoin = require('bitcoinjs-lib')
const baddress = require('bitcoinjs-lib/src/address')
const bcrypto = require('bitcoinjs-lib/src/crypto')
const NETWORKS = require('bitcoinjs-lib/src/networks')
const hdkey =  require('ethereumjs-wallet/hdkey')
const util = require('ethereumjs-util')
const Web3 =require ('web3')
const transaction = require('ethereumjs-tx');

function addPreZero(num){
    var t = (num+'').length,
        s = '';
    for(var i=0; i<64-t; i++){
        s += '0';
    }
    return s+num;
}

function ethereumErc20CoinSign(erc20SignData) {
    var errInfo = {code:2000, message:"引數錯誤", data: null}
    var outErc20Data = [];
    if(erc20SignData === null) {
        console.log("erc30SignData param is null, please give a valid param");
        return errInfo;
    }
    var calcNonce = erc20SignData.nonce;
    console.log("receive params success");
    for (var i = 0; i < erc20SignData.signDta.length; i++){
        var transactionNonce = parseInt(calcNonce).toString(16);
        var gasLimit = parseInt(erc20SignData.gasLimit).toString(16);
        var gasPrice = parseFloat(erc20SignData.gasPrice).toString(16);
        var totx = parseFloat(erc20SignData.signDta[i].totalAmount*(1**erc20SignData.decimal)).toString(16);
        var txData = {
            nonce: '0x'+ transactionNonce,
            gasLimit: '0x' + gasLimit,
            gasPrice: '0x' +gasPrice,
            to: erc20SignData.contractAddress,
            from: erc20SignData.currentAccount,
            value: '0x00',
            data: '0x' + 'a9059cbb' + addPreZero(erc20SignData.signDta[i].toAddress) + addPreZero(totx)
        }
        var tx = new transaction(txData);
        const privateKey1 = new Buffer(erc20SignData.privateKey, 'hex');
        tx.sign(privateKey1);
        var serializedTx = tx.serialize().toString('hex');
        outErc20Data = outErc20Data.concat(serializedTx)
        calcNonce = calcNonce + 1;
    }
    return { signCoin:"ERC20", signDataArr:outErc20Data}
}

//測試JSON
var sendErc20Data =
    {
        "signMark":"ERC20",
        "privateKey":"5204627f8e58b3c75be408423c121988a1cb942e73470fb19186ef8986cd50b3",
        "contractAddress":"0xdac17f958d2ee523a2206206994597c13d831ec7",
        "currentAccount":"0xe558be4e90b2ac96ae5cad47dc39cd08316f2e57",
        "gasPrice":2100000,
        "gasLimit":21000,
        "nonce":1,
        "decimal":6,
        "signDta":[
            {
                "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
                "totalAmount":1000,
            },{
                "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
                "totalAmount":1000,
            },{
                "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
                "totalAmount":1000,
            },{
                "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
                "totalAmount":1000,
            },{
                "toAddress":"0xc6328b3a137b3be3f01c35ecda4ecda375be7fdf",
                "totalAmount":1000,
            }
        ]
    }

console.log(ethereumErc20CoinSign(sendErc20Data));

區塊鏈錢包技術指南:https://github.com/guoshijiang/blockchain-wallet/