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, password
return 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 wallet
try {
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

metamask中的import account的代码实现的更多相关文章

  1. 在vue中使用import()来代替require.ensure()实现代码打包分离

    最近看到一种router的写法 import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const login = ...

  2. iOS中@class #import #include 简介

    [转载自:http://blog.csdn.net/chengwuli125/article/details/9705315] 一.解析        很多刚开始学习iOS开发的同学可能在看别人的代码 ...

  3. python中的import,reload,以及__import__

    python中的import,reload,以及__import__ 分类: UNIX/LINUX C/C++LINUX/UNIX shellpython2013-04-24 20:294536人阅读 ...

  4. spring框架中的@Import注解

    spring框架中的@Import注解 Spring框架中的@Import注解 在之前的文章中,作者介绍了Spring JavaConfig. 这是除了使用传统的XML文件之外,spring带来的新的 ...

  5. (转)关于ES6的 模块功能 Module 中export import的用法和注意之处

    关于ES6的 模块功能 Module 中export import的用法和注意之处 export default 的用法 export default命令用于指定模块的默认输出.显然,一个模块只能有一 ...

  6. 在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示

    在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示) 1.使用npm安装依赖 npm install --save codemirror; 2.在页面中放入如下代码 ...

  7. ES6中的import()函数

    import(specifier) 上面代码中,import函数的参数specifier,指定所要加载的模块的位置.import命令能够接受什么参数,import()函数就能接受什么参数,两者区别主要 ...

  8. CSS中的层叠、特殊性、继承、样式表中的@import

    CSS中的层叠.特殊性.继承.样式表中的@import 层叠 CSS有一个机制是层叠,层叠可以理解为对样式的覆盖,优先性为: 网站开发者的样式表 用户样式(通过设置浏览器的显示选项) 浏览器默认的样式 ...

  9. python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数

    import os,re #代码所在位置 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件统计其中的代码行数,包括空格和注 ...

随机推荐

  1. NLog 配置

    之前我介绍过如何使用log4net来记录日志,但最近喜欢上了另一个简单好用的日志框架NLog. 关于NLog和log4net的比较这里就不多讨论了,感兴趣的朋友可以参看.NET日志工具介绍和log4n ...

  2. PowerDesigner反向生成物理数据模型

    什么是PowerDesigner Power Designer 是Sybase公司的CASE工具集,使用它可以方便地对管理信息系统进行分析设计,它几乎包括了数据库模型设计的全过程.利用Power De ...

  3. webstrom vue项目让局域网访问

    vue项目package.json "dev": "webpack-dev-server --inline --progress --config build/webpa ...

  4. Hash table: why size should be prime?

    Question: Possible Duplicate:Why should hash functions use a prime number modulus? Why is it necessa ...

  5. SSM实现简单后台分页

    1.简单思路 这是最常见的分页格式,分析一下我们需要传什么数据给前端吧! 首先是左边下面的总共几条记录,然后是右边的当前页面,然后就是一些你所需要展示的数据.对了每页显示多少条是不也得控制下,下面的显 ...

  6. 关于html5中的 网页图标问题

    在html5 中 设置网页图标的语句<link rel="icon" type="image/x-icon" href="favicon.ico ...

  7. BZOJ4259: 残缺的字符串(FFT 字符串匹配)

    题意 题目链接 Sol 知道FFT能做字符串匹配的话这就是个裸题了吧.. 考虑把B翻转过来,如果\(\sum_{k = 0}^M (B_{i - k} - A_k)^2 * B_{i-k}*A_k = ...

  8. 基础篇|一文搞懂RNN(循环神经网络)

    基础篇|一文搞懂RNN(循环神经网络) https://mp.weixin.qq.com/s/va1gmavl2ZESgnM7biORQg 神经网络基础 神经网络可以当做是能够拟合任意函数的黑盒子,只 ...

  9. 程序员简单打造一个灵活智能的自动化运维系统C#实例程序

    你是一个程序员,被派去管理公司500台计算机.这些机器可能需要执行一些自动化任务,一台台手动操作会把你累死.重复性的工作还是交给电脑处理,怎么解决这个问题呢?一个自动化的运维系统是必须的.自己实现的好 ...

  10. (后端)异常不仅仅是try/catch

    前言  编程时我们往往拿到的是业务流程正确的业务说明文档或规范,但实际开发中却布满荆棘和例外情况,而这些例外中包含业务用例的例外,也包含技术上的例外.对于业务用例的例外我们别无它法,必须要求实施人员与 ...