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. C#检测是否联网

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  2. LeetCode-数组操作-Python<三>

    上一篇:LeetCode链表相加-Python<二> 以前没怎么做过算法题,来来去去都是那些循环,所以先从数组简单题开始做. 这两天最大心得: 总在边界里考虑不周到,导致错误 做晕的时候, ...

  3. IQuerable与IEnumable的区别

    核心区别: IQueryable该接口会把查询表达式先缓存到表达式树Expression 中,只有当真正用到数据的时候(例如 遍历 ),才会由IQueryProvider解析表达式树,生成sql语句执 ...

  4. Chart控件,chart、Series、ChartArea曲线图绘制的重要属性介绍

    先简单说一下,从图中可以看到一个chart可以绘制多个ChartArea,每个ChartArea都可以绘制多条Series.ChartArea就是就是绘图区域,可以有多个ChartArea叠加在一起, ...

  5. centos7使用yum安装mysql 【转】

    转自:http://blog.csdn.net/eclothy/article/details/52733891 使用: yum install mariadb*    (注意,带星号) 安装好后,启 ...

  6. github上值得关注的前端项目【转】

    今天突然看到了这些资源,所以就转载过来了,虽然是2015年的,但是可以看一下 综合/资源 frontend-dev-bookmarks 一个巨大的前端开发资源清单.star:15000 front-e ...

  7. mysql学习目录

    MySQL数据库 mysql 之库, 表的简易操作 mysql之行(记录)的详细操作 mysql之单表查询 mysql之多表查询 Navicat安装及简单使用 mysql之Navicat工具.pymy ...

  8. 在Visualforce页面中使用Visual Flow

    在本文中,我们将通过一个示例说明如何将"流"(Visual Flow)用于Visualforce页面. 更全面的知识可以参考官方文档. 创建流 我们要创建一个流,它的作用是得到一个 ...

  9. JavaScript按日期排序之灵活排序

    上代码: var dataContent = [ { ID: "1", hobbit: "去音乐", sport: "在篮球", movie ...

  10. android一个倾斜的TextView,适用于标签效果

    描述: android一个倾斜的TextView,适用于标签效果 应用截图: 使用说明: <com.haozhang.lib.SlantedTextView android:layout_wid ...