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. Bootstrap学习记录-1.Navigation

    Bootstrap中的导航栏功能需要添加bootstrap.css.jquery.js.bootstrap.js,其中,jquery.js文件是bootstrap.js文件中必须的,否则就会抛出异常信 ...

  2. c# 后台拼接分页Html

    public static string ReplaceStr(string originalStr, string oldStr, string newStr) { if (string.IsNul ...

  3. 探秘小程序(7):view组件

    小程序中最基础,最常用的组件--view,类似于html中div的存在有四个属性: ①hover-class:指定按下去的样式类.当 hover-class="none" 时,没有 ...

  4. IBatisNet动态update以及DateTime类型字段处理

    在维护一个老项目中碰到的问题.SQL配置如下(只简单列出两个字段): <update id="ProjectInfo.Update" parameterClass=" ...

  5. Java学习笔记之——类与对象

    1.参数的传递方式 1)值传递 2)引用传递 2.类和对象: (1)类的定义: public class 类名{ 类型 属性1: 类型 属性2: ……… public 返回值类型 方法名1(形参){ ...

  6. Spring全家桶–SpringBoot Rest API

    Spring Boot通过提供开箱即用的默认依赖或者转换来补充Spring REST支持.在Spring Boot中编写RESTful服务与SpringMVC没有什么不同.总而言之,基于Spring ...

  7. LVOOP设计模式在路上(二)-- 策略模式

    前言 最近工作还挺忙的,连着好些周都是单休了,今天休息在家就来写写关于策略模式的理解和labivew的实现. 正文 1.什么是策略模式 定义是这样描述的:它定义了算法家族,分别封装起来,让它们之间可以 ...

  8. HDU4825(01字典树)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  9. 纯css抖动效果

    HTML: <button class="shake">按钮</button> CSS: .shake{ width: 120px; height: 33p ...

  10. stylus解决移动端1像素边框的问题

    首先 我是借用了yo框架的border和他的媒体查询组合 这两个分别是在yo>lib>core>classes>_border.scss(用来获取yo框架封装的border) ...