日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

metamask中的import account的代码实现

發(fā)布時間:2025/3/8 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 metamask中的import account的代码实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

metamask-extension/app/scripts/account-import-strategies/index.js

這部分就是用戶如果往metamask中import一個已有的賬戶調(diào)用的接口,就是是直接輸入私鑰privateKey還是使用json file

即如下圖:

?

const Wallet = require('ethereumjs-wallet') const importers = require('ethereumjs-wallet/thirdparty') const ethUtil = require('ethereumjs-util')const accountImporter = {importAccount (strategy, args) {try {const importer = this.strategies[strategy]//確認使用的是那種import的方法const privateKeyHex = importer.apply(null, args) //args就是輸入的值,如privateKey或者input, passwordreturn Promise.resolve(privateKeyHex)} catch (e) {return Promise.reject(e)}},strategies: {'Private Key': (privateKey) => {//輸入私鑰if (!privateKey) {throw new Error('Cannot import an empty key.')}const prefixed = ethUtil.addHexPrefix(privateKey)//加入0x前綴const buffer = ethUtil.toBuffer(prefixed)if (!ethUtil.isValidPrivate(buffer)) {throw new Error('Cannot import invalid private key.')}const stripped = ethUtil.stripHexPrefix(prefixed)//去掉前綴return stripped//輸出私鑰},'JSON File': (input, password) => {let wallettry {wallet = importers.fromEtherWallet(input, password)} catch (e) {console.log('Attempt to import as EtherWallet format failed, trying V3...')}if (!wallet) {wallet = Wallet.fromV3(input, password, true)}return walletToPrivateKey(wallet)},},}function walletToPrivateKey (wallet) {const privateKeyBuffer = wallet.getPrivateKey()return ethUtil.bufferToHex(privateKeyBuffer) }module.exports = accountImporter

ethereumjs-wallet

  • fromV1(input, password)?- import a wallet (Version 1 of the Ethereum wallet format)
  • fromV3(input, password, [nonStrict])?- import a wallet (Version 3 of the Ethereum wallet format). Set?nonStrict?true to accept files with mixed-caps.

var thirdparty = require('ethereumjs-wallet/thirdparty')

  • fromEtherWallet(input, password)?- import a wallet generated by EtherWallet

?

轉(zhuǎn)載于:https://www.cnblogs.com/wanghui-garcia/p/9791559.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的metamask中的import account的代码实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。