本文作者:问几许

柚子 | 用 JavaScript 生成EOS钱包

问几许 4年前 ( 2019-05-27 ) 5841 抢沙发
柚子 | 用 JavaScript 生成EOS钱包摘要: EOS,可以理解为Enterprise Operation System,即为商用分布式应用设计的一款区块链操作系统。EOS是引入的一种新的区块链架构,旨在实现分布式应用的性能扩展...

EOS,可以理解为Enterprise Operation System,即为商用分布式应用设计的一款区块链操作系统。EOS是引入的一种新的区块链架构,旨在实现分布式应用的性能扩展。注意,它并不是像比特币和以太坊那样的货币,而是基于EOS软件项目之上发布的代币,被称为区块链3.0。

下面教程就是用JavaScript去生成一个简易的EOS钱包。

开发环境

NodeJS

依赖库安装

eosjs-ecc

npm  install bip39
npm install eosjs-ecc
npm install eosjs

引入依赖库

import bip39 from 'bip39';
import eosEcc from 'eosjs-ecc';

随机生成钱包

eosEcc.randomKey().then(privateKey => {
     console.log('Private Key:\t', privateKey) 
     let privateKey = privateKey;
     let publicKey  = eosEcc.privateToPublic(privateKey);
});

通过助记词生成私钥、公钥

let privateKey = eosEcc.seedPrivate(mnemonic);
let publicKey  = eosEcc.privateToPublic(privateKey);

注册EOS账户

var Eos = require('eosjs')
var eosConfig = {
    keyProvider: ['私钥'], // 配置私钥字符串
    httpEndpoint: 'http://51.15.224.168:8888', //DEV开发链url与端口
    //httpEndpoint: 'https://nodes.get-scatter.com', //主网
    chainId: "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca", // 通过cleos get info可以获取chainId
    //chainId: "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906", //主网
    broadcast: true,
}
var eos = Eos(eosConfig)
 
var creatoraccount = "accounthr123"; //主账号
var newaccount = "sdrghiochaiq"; //新账号
var newaccount_pubkey = pubkey; //新账号的公钥
//构建transaction对象
eos.transaction(tr => {
    //新建账号
    tr.newaccount({
        creator: creatoraccount,
        name: newaccount,
        owner: newaccount_pubkey,
        active: newaccount_pubkey
    })
 
    //为新账号充值RAM
    tr.buyrambytes({
        payer: creatoraccount,
        receiver: newaccount,
        bytes: 3072
    })
    //为新账号抵押CPU和NET资源
    tr.delegatebw({
        from: creatoraccount,
        receiver: newaccount,
        stake_net_quantity: '1.0000 EOS',
        stake_cpu_quantity: '1.0000 EOS',
        transfer: 0
    })
}).then(r => {
    console.log(r);
}).catch(e => {
    console.log(e)
});


文章版权及转载声明

作者:问几许本文地址:https://wenjixu.com/blog/120.html发布于 4年前 ( 2019-05-27 )
文章转载或复制请以超链接形式并注明出处问几许

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论

快捷回复:

评论列表 (暂无评论,5841人围观)参与讨论

还没有评论,来说两句吧...