metamask中的import account的代码实现
生活随笔
收集整理的這篇文章主要介紹了
metamask中的import account的代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
metamask-extension/app/scripts/account-import-strategies/index.js
這部分就是用戶如果往metamask中import一個已有的賬戶調用的接口,就是是直接輸入私鑰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 = accountImporterethereumjs-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
?
轉載于:https://www.cnblogs.com/wanghui-garcia/p/9791559.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的metamask中的import account的代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android x86 下运行纯ARM版
- 下一篇: Dialog源码分析