bip39 ethereumjs-wallet ethereumjs-util 的API 和引入 介绍

晋承运
2023-12-01

比特币 BIP39:的 JavaScript 实现:用于生成确定性密钥的助记符代码

ethereumjs-wallet

  • 轻量级钱包实现,支持密钥创建和各种格式之间的转换,- 轻巧,在浏览器中工作,使用单一的、维护的加密库版本(并且应该与 `ethereumjs-util` 和 `ethereumjs-tx` 一致),支持各种钱包格式之间的导入/导出

  • 不支持的功能:签署交易、管理存储(无论是在 node.js 还是浏览器中)

ethereumjs-util:以太坊的实用函数集合。它可以在 Node.js 和带有 [browserify](http://browserify.org/) 的浏览器中使用。

以上..

三个包的安装和引入:

  npm安装

npm install bip39

npm  install  ethereumjs-wallet

npm  install ethereumjs-util

引入

const bip39 = require('bip39')
// 默认为 BIP39 英文单词列表--只支持英文
const hdkey = require('ethereumjs-wallet/hdkey')
const util = require('ethereumjs-util')

bip39例子与所有方法:

``` js
// 生成随机助记符(在后台使用crypto.randomBytes),默认为 128 位bite
const mnemonic = bip39.generateMnemonic()
// => 'seed sock milk update focus rotate barely fade car face mechanic mercy'

bip39.mnemonicToSeedSync('basket actual').toString('hex')
// => '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f'

bip39.mnemonicToSeedSync('basket actual')
// => <Buffer 5c f2 d4 a8 b0 35 5e 90 29 5b df c5 65 a0 22 a4 09 af 06 3d 53 65 bb 57 bf 74 d9 52 8f 49 4b fa 44 00 f5 3d 83 49 b8 0f da e4 40 82 d7 f9 54 1e 1d ba 2b ...>

// mnemonicToSeed has an synchronous version
// mnemonicToSeedSync is less performance oriented
bip39.mnemonicToSeed('basket actual').then(console.log)
// => <Buffer 5c f2 d4 a8 b0 35 5e 90 29 5b df c5 65 a0 22 a4 09 af 06 3d 53 65 bb 57 bf 74 d9 52 8f 49 4b fa 44 00 f5 3d 83 49 b8 0f da e4 40 82 d7 f9 54 1e 1d ba 2b ...>

bip39.mnemonicToSeed('basket actual').then(bytes => bytes.toString('hex')).then(console.log)
// => '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f'

bip39.mnemonicToSeedSync('basket actual', 'a password')
// => <Buffer 46 16 a4 4f 2c 90 b9 69 02 14 b8 fd 43 5b b4 14 62 43 de 10 7b 30 87 59 0a 3b b8 d3 1b 2f 3a ef ab 1d 4b 52 6d 21 e5 0a 04 02 3d 7a d0 66 43 ea 68 3b ... >

bip39.validateMnemonic(mnemonic)
// => true

bip39.validateMnemonic('basket actual')
// => false
```

export declare function mnemonicToSeedSync(mnemonic: string, password?: string): Buffer;
export declare function mnemonicToSeed(mnemonic: string, password?: string): Promise<Buffer>;
export declare function mnemonicToEntropy(mnemonic: string, wordlist?: string[]): string;
export declare function entropyToMnemonic(entropy: Buffer | string, wordlist?: string[]): string;
export declare function generateMnemonic(strength?: number, rng?: (size: number) => Buffer, wordlist?: string[]): string;
export declare function validateMnemonic(mnemonic: string, wordlist?: string[]): boolean;
export declare function setDefaultWordlist(language: string): void;
export declare function getDefaultWordlist(): string;

[ethjs-util] 提供了以下方法

- arrayContainsArray
- toBuffer 
- getBinarySize 
- stripHexPrefix 
- isHexPrefixed 
- isHexString 
- padToEven 
- intToHex 
- fromAscii 
- fromUtf8 
- toUtf8 
- toAscii 
- getKeys

可以按名称导入:

import { intToHex, stripHexPrefix } from 'ethereumjs-util'

ethereumjs-wallet API

https://github.com/ethereumjs/ethereumjs-wallet/blob/master/docs/classes/wallet.md

目录


 类似资料: