链眼社区:专注于区块链安全,区块链数据分析, 区块链信息整合,区块链技术服务和区块链技术咨询。

通过 Js 如何离线生成 ETH 地址和离线签名

问题描述

我想使用 UniAPP 自己开发一个 ETH 离线钱包,需要使用哪些库,怎么去实现

回复区

扫地僧 2021-10-10 07:48:45

开发过程中遇到问题可以发联系:13611267041

扫地僧 2021-10-10 07:48:01

离线签名代码如下:

const util = require('ethereumjs-util');
const EthereumTx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');
const constant = require('../../constant');


if (typeof web3 !== 'undefined') {
    var web3 = new Web3(web3.currentProvider);
} else {
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

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

var EthereumSign = function (privateKey, nonce, toAddress, sendToBalance, gasPrice, gasLimit) {
    if(!privateKey && !toAddress && !sendToBalance && !gasPrice && !gasLimit) {
        return constant.paramsErr;
    } else {
        const privateKeyBuf = Buffer.from(
            privateKey,
            'hex',
        )
        var transactionNonce = parseInt(nonce).toString(16);
        var numBalance = parseFloat(sendToBalance);
        var balancetoWei = web3.toWei(numBalance, "ether");
        var oxNumBalance = parseInt(balancetoWei).toString(16);
        var gasPriceHex = parseInt(gasPrice).toString(16);
        var gasLimitHex = parseInt(gasLimit).toString(16);
        var rawTx = {
            nonce: '0x' + transactionNonce,
            gasPrice: '0x' + gasPriceHex,
            gasLimit:'0x'+ gasLimitHex,
            to: toAddress,
            value: '0x' + oxNumBalance,
            data: "0x"
        };
        var tx = new EthereumTx(rawTx);
        tx.sign(privateKeyBuf);
        var serializedTx = tx.serialize();
        if(serializedTx == null) {
            return constant.serializedErr;
        } else {
            if (!tx.verifySignature()) {
                console.log('验证签名失败');
                return constant.serializedErr;
            }
        }
    }
    return '0x' + serializedTx.toString('hex');
};

var EthereumErc20CoinSign = function(privateKey, nonce, currentAccount,  contractAddress, toAddress,  gasPrice,  gasLimit, totalAmount , decimal) {
    if(!privateKey && !nonce && !currentAccount && !contractAddress && !toAddress  && !gasPrice && !gasLimit && !totalAmount && !decimal) {
        return constant.paramsErr;
    }
    var transactionNonce = parseInt(nonce).toString(16);
    var gasLimits = parseInt(gasLimit).toString(16);
    var gasPrices = parseFloat(gasPrice).toString(16);
    var txboPrice = parseFloat(totalAmount*(10**decimal)).toString(16)
    var txData = {
        nonce: '0x'+ transactionNonce,
        gasLimit: '0x' + gasLimits,
        gasPrice: '0x' +gasPrices,
        to: contractAddress,
        from: currentAccount,
        value: '0x00',
        data: '0x' + 'a9059cbb' + addPreZero(toAddress.substr(2)) + addPreZero(txboPrice)
    }
    var tx = new EthereumTx(txData);
    const privateKeyBuf = new Buffer(
        privateKey,
        'hex'
    );
    tx.sign(privateKeyBuf);
    var serializedTx = tx.serialize().toString('hex');
    return '0x'+ serializedTx;
};

module.exports = {EthereumSign, EthereumErc20CoinSign}
扫地僧 2021-10-10 07:47:06

生成地址代码如下:

const bitcoin  = require('bitcoinjs-lib');
const baddress = require('bitcoinjs-lib/src/address');
const NETWORKS = require('bitcoinjs-lib/src/networks');
const wallet = require('ethereumjs-wallet/hdkey');
const util = require('ethereumjs-util');
const bip32  = require( 'bip32');
const constant = require('../../constant');
const bcrypto  = require('bitcoinjs-lib/src/crypto');

var CreateEthAddressBySeed = function(seed, number) {
    if(!seed && !number) {
        return constant.paramsErr;
    }
    var rootMasterKey = wallet.fromMasterSeed(seed);
    var childKey = rootMasterKey.derivePath("m/44'/60'/0'/0/" +  number + "");
    console.log(childKey._hdkey._publicKey.toString('hex'))
    var address = util.pubToAddress(childKey._hdkey._publicKey, true).toString('hex');
    var privateKey = childKey._hdkey._privateKey.toString('hex');
    return { privateKey: privateKey, address: "0x" + address };
}

var CreateEthAddressByPk = function(private_key) {
    let addr = util.privateToAddress(
        Buffer.from(private_key, "hex")
    ).toString('hex');
    return { privateKey: private_key, address: "0x" + addr };
}

回答

合作伙伴