nodejs 使用 ethers创建以太坊钱包

时间:2023-01-17 10:27:56

创建钱包
创建钱包流程: 生成随机助记词 => 通过助记词创建钱包=>钱包信息和加密明文(私钥和密码加密)

导入钱包
通过插件提供方法,根据助记词|keyStore|私钥,找到钱包信息(地址和私钥)=>钱包信息和加密明文(私钥和密码加密)AES加密存入localStorage

 

生成记忆助词

var mnemonic = ethers.utils.HDNode.entropyToMnemonic(ethers.utils.randomBytes(16));

创建钱包

  if (!ethers.utils.HDNode.isValidMnemonic(mnemonic)) {
    result.success = false;
    result.message = "mnemonic Invalid";
  } else {
    var wallet = ethers.Wallet.fromMnemonic(mnemonic);
    result.data = {
      privateKey: wallet.privateKey,
      path: wallet.path,
      address: wallet.address,
      mnemonic: mnemonic
    }

找回钱包

 if (req.body.type == "privateKey") {//根据私钥
    var wallet = new ethers.Wallet(req.body.data);
    result.data = wallet.address
  } else {//根据记忆帮词
    var mnemonic = ethers.Wallet.fromMnemonic(req.body.data);
    var wallet = new ethers.Wallet(mnemonic.privateKey);
    result.data = wallet.address;
  }

源码

inde.html

<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>new a wallet</title>
  <style>
    .input-group{
    margin-bottom: 10px;
  }
  </style>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
  <div class="container">
    <div class="page-header">
      <h1>x-wallet demo</h1>
    </div>

    <div class="row">
      <h3>new a wallet</h3>
      <div class="col-12">
        <div class="input-group">
          <span class="input-group-addon text-primary" id="generate-mnemonic">generate mnemonic</span>
          <input id="mnemonic" type="text" class="form-control" aria-describedby="generate-mnemonic">
          <div class="input-group-btn">
            <button id="GenerateMnemonic" class="btn btn-primary" type="submit">create</button>
          </div>
        </div>
      </div>
      <div class="col-12">
        <div class="input-group">
          <span class="input-group-addon text-primary">create a wallet</span>
          <input type="text" class="form-control" disabled id="new-wallet">
          <div class="input-group-btn">
            <button id="createWallet" class="btn btn-primary" type="submit">create</button>
          </div>
        </div>
      </div>
      <div class="col-12">
        <p id="wallet-content">
          <p>
      </div>
      <h3>Retrieve the wallet</h3>
      <div class="col-12">
        <div class="input-group">
          <span class="input-group-addon text-primary" id="group-by-privateKey">Group by privateKey</span>
          <input id="retrieve-privateKey" type="text" class="form-control" aria-describedby="group-by-privateKey">
          <div class="input-group-btn">
            <button id="retrieve-waller-privateKey" class="btn btn-primary" type="submit">Retrieve</button>
          </div>
        </div>
      </div>
      <div class="col-12">
        <div class="input-group">
          <span class="input-group-addon text-primary" id="group-by-mnemonic">Group by mnemonic</span>
          <input id="retrieve-mnemonic" type="text" class="form-control" aria-describedby="group-by-mnemonic">
          <div class="input-group-btn">
            <button id="retrieve-waller-mnemonic" class="btn btn-primary" type="submit">Retrieve</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
  <script type="text/javascript">
    $(function () {
      $("#GenerateMnemonic").on('click', function () {
        $.post("http://localhost:8888/generateMnemonic", {}, function (res) {
          var mnemonic = $('#mnemonic');
          mnemonic.val(res.data);
        })
      })

      $("#createWallet").on('click', function () {
        $.post("http://localhost:8888/createWallet", {
          mnemonic: $('#mnemonic').val()
        }, function (res) {
          if (res.success) {
            $("#wallet-content").html("address:" + res.data.address + "<br/>" +
              "privateKey:" + res.data.privateKey + "<br/>" +
              "mnemonic:" + res.data.path + "<br/>");
          }
          $("#new-wallet").val(res.data.address);
        })
      })

      $('#retrieve-waller-privateKey').on('click', function () {
        $.post("http://localhost:8888/retrieveWallet", {
          type: "privateKey",
          data: $("#retrieve-privateKey").val()
        }, function (res) {
          if (res.success) {
            alert("wallet:" + res.data);
          }
        })
      })

      $('#retrieve-waller-mnemonic').on('click', function () {
        $.post("http://localhost:8888/retrieveWallet", {
          type: "mnemonic",
          data: $("#retrieve-mnemonic").val()
        }, function (res) {
          if (res.success) {
            alert("wallet:" + res.data);
          }
        })
      })

    })
  </script>
</body>

</html>

app.js

var express = require('express');
var ethers = require('ethers');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({
  extended: false
})

app.use(express.static('public'));

app.get('/', function (req, res) {
  res.sendFile(__dirname + "/app/index.html");
})
app.post('/generateMnemonic', urlencodedParser, function (req, res) {
  var mnemonic = ethers.utils.HDNode.entropyToMnemonic(ethers.utils.randomBytes(16));
  var result = {
    success: true,
    message: "success",
    data: mnemonic
  }

  res.send(result);
  res.end();
})

app.post('/createWallet', urlencodedParser, function (req, res) {
  var result = {
    success: true,
    message: "success",
    data: null
  }
  var mnemonic = req.body.mnemonic;
  if (!ethers.utils.HDNode.isValidMnemonic(mnemonic)) {
    result.success = false;
    result.message = "mnemonic Invalid";
  } else {
    var wallet = ethers.Wallet.fromMnemonic(mnemonic);
    result.data = {
      privateKey: wallet.privateKey,
      path: wallet.path,
      address: wallet.address,
      mnemonic: mnemonic
    }
  }
  res.send(result);
  res.end();
})

app.post('/retrieveWallet', urlencodedParser, function (req, res) {
  var result = {
    success: true,
    message: "success",
    data: null
  }
  if (req.body.type == "privateKey") {
    var wallet = new ethers.Wallet(req.body.data);
    result.data = wallet.address
  } else {
    var mnemonic = ethers.Wallet.fromMnemonic(req.body.data);
    var wallet = new ethers.Wallet(mnemonic.privateKey);
    result.data = wallet.address;
  }
  res.send(result);
  res.end();
})



var server = app.listen(8888, function () {
  var host = server.address().address
  var port = server.address().port
  console.log("server start port:" + port)
})

 效果:

nodejs 使用 ethers创建以太坊钱包

 

 

此随笔乃本人学习工作记录,如有疑问欢迎在下面评论,转载请标明出处。

如果对您有帮助请动动鼠标右下方给我来个赞,您的支持是我最大的动力。