核心以太坊函数的JavaScript库

Posted 枭一

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了核心以太坊函数的JavaScript库相关的知识,希望对你有一定的参考价值。

ethereumjs-LIB是核心javascript复仇如在所描述的功能的黄皮书。这是一个简单的元模块,提供以下模块。大多数JS模块都在ethereumjs中进行跟踪


  • VM - 以太坊虚拟机和状态处理功能

  • 区块链 - 区块链管理

  •  - 块架构定义和验证

  • 事务 - 事务模式定义和验证

  • 帐户 - 帐户架构定义和验证

  • rlp - 递归长度前缀序列化

  • Trie - 修改的Merkle Patricia树

  • Ethash - 以太坊的工作证明算法

  • utils - 其他辅助函数


1、以Javascript实现的以太坊VM

1.1安装:npm install ethereumjs-vm

1.2用法

var VM = require('ethereumjs-vm')

//create a new VM instance
var vm = new VM()
var code = '7f4e616d65526567000000000000000000000000000000000000000000000000003055307f4e616d6552656700000000000000000000000000000000000000000000000000557f436f6e666967000000000000000000000000000000000000000000000000000073661005d2720d855f1d9976f88bb10c1a3398c77f5573661005d2720d855f1d9976f88bb10c1a3398c77f7f436f6e6669670000000000000000000000000000000000000000000000000000553360455560df806100c56000396000f3007f726567697374657200000000000000000000000000000000000000000000000060003514156053576020355415603257005b335415603e5760003354555b6020353360006000a233602035556020353355005b60007f756e72656769737465720000000000000000000000000000000000000000000060003514156082575033545b1560995733335460006000a2600033545560003355005b60007f6b696c6c00000000000000000000000000000000000000000000000000000000600035141560cb575060455433145b1560d25733ff5b6000355460005260206000f3'

vm.runCode({
 code: Buffer.from(code, 'hex'), // code needs to be a Buffer
 gasLimit: Buffer.from('ffffffff', 'hex')
}, function(err, results){
 console.log('returned: ' + results.return.toString('hex'));
})

1.3内部结构

  • runBlockchain

    • 对于每个块,runBlock

  • runBlock

    • 对于每个tx,runTx

    • 支付矿工和叔叔

  • runTx

    • 检查寄件人余额

    • 检查发件人nonce

    • runCall

    • 转移燃气费

  • runCall

    • 检查站状态

    • 转移价值

    • 加载代码

    • runCode

    • 具体化创建合同

    • 恢复或提交检查点

  • runCode

    • 迭代代码

    • 运行操作码

    • 跟踪燃气用量

  • OpFns

    • 运行单独的操作码

    • 修改堆栈

    • 修改记忆

    • 计算费用

opFns for CREATECALLCALLCODE回调runCall

2、用于存储块并与块交互的模块

2.1 安装

npm install ethereumjs-blockchain

2.2用法

以下是迭代现有Geth DB的示例(需要level单独安装)

const level = require('level')
const Blockchain = require('ethereumjs-blockchain')
const utils = require('ethereumjs-util')

const gethDbPath = './chaindata' // Add your own path here
const db = level(gethDbPath)

new Blockchain({db: db}).iterator('i', (block, reorg, cb) => {
 const blockNumber = utils.bufferToInt(block.header.number)
 const blockHash = block.hash().toString('hex')
 console.log(`BLOCK ${blockNumber}: ${blockHash}`)
 cb()
}, (err) => console.log(err || 'Done.'))


警告:由于ethereumjs-blockchain出于安全原因也在数据库上执行写操作,因此只能在数据库副本上运行此操作,否则可能会导致数据库状态受损。

2.3 api

Blockchain

  • new Blockchain(opts)

  • BlockChain 方法

    • blockchain.putGenesis(genesis, [cb])

    • blockchain.getHead(name, [cb])

    • blockchain.getLatestHeader([cb])

    • blockchain.getLatestBlock([cb])

    • blockchain.putBlocks(blocks, [cb])

    • blockchain.putBlock(block, [cb])

    • blockchain.getBlock(hash, [cb])

    • blockchain.getBlocks(blockId, maxBlocks, skip, reverse, [cb])

    • blockchain.putHeaders(headers, [cb])

    • blockchain.putHeader(header, [cb])

    • blockchain.selectNeededHashes(hashes, [cb])

    • blockchain.delBlock(blockHash, [cb])

    • blockchain.iterator(name, onBlock, [cb])

3、ethereumjs-block

以太坊的Block模式的编码,解码和验证


3.1安装

npm install ethereumjs-block


4、ethereumjs-tx

一个用于创建,操作和签署以太坊交易的简单模块


4.1安装

npm install ethereumjs-tx


4.2用法

const EthereumTx = require('ethereumjs-tx')
const privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

const txParams = {
 nonce: '0x00',
 gasPrice: '0x09184e72a000',
 gasLimit: '0x2710',
 to: '0x0000000000000000000000000000000000000000',
 value: '0x00',
 data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
 // EIP 155 chainId - mainnet: 1, ropsten: 3
 chainId: 3
}

const tx = new EthereumTx(txParams)
tx.sign(privateKey)
const serializedTx = tx.serialize()

注意:此软件包期望ECMAScript 6(ES6)作为最低环境。从缺乏ES6支持的浏览器,请在包含此repo的任何构建之前使用垫片(如es6-shim)。


5、ethereumjs-account

以太坊帐户架构的编码,解码和验证

此库可以简化以太坊帐户的处理,帐户可以是外部帐户或合同。

请注意,该库并不用于处理您的钱包帐户,例如使用库中的 web3-eth-personal包 web3.js。这只是一个语义包装器,可以简化帐户数据的使用,并提供从以太网状态trie读取和写入帐户的功能。

5.1 安装

npm install ethereumjs-account


5.2 api

  • new Account([data])

  • Account 属性

  • Account 方法

    • account.isEmpty()

    • account.isContract()

    • account.serialize()

    • account.toJSON()

    • account.getCode(trie, cb)

    • account.setCode(trie, code, cb)

    • account.getStorage(trie, key, cb)

    • account.setStorage(trie, key, val, cb)


6、rlp

JavaScript中的递归长度前缀编码

6.1 安装

npm install rlp

6.2 用法

var RLP = require('rlp');
var assert = require('assert');

var nestedList = [ [], [[]], [ [], [[]] ] ];
var encoded = RLP.encode(nestedList);
var decoded = RLP.decode(encoded);
assert.deepEqual(nestedList, decoded);

6.3 api

rlp.encode(plain)- RLP编码ArrayBufferString返回a Buffer

rlp.decode(encoded, [skipRemainderCheck=false])-解码的RLP编码BufferArrayString并返回一个Buffer或一个ArrayBuffers。如果skipRemainderCheck启用,rlp将只解码缓冲区中的第一个rlp序列。默认情况下,如果缓冲区中的字节数多于rlp序列使用的字节数,则会抛出错误。

7、merkle-patricia-tree

这是以太坊黄皮书中指定的修改过的merkle patricia树的实现。

支持的唯一后备存储是LevelDB到levelup模块。

7.1 安装

npm install merkle-patricia-tree


7.2 用法

var Trie = require('merkle-patricia-tree'),
level = require('level'),
db = level('./testdb'),
trie = new Trie(db);

trie.put('test', 'one', function () {
 trie.get('test', function (err, value) {
   if(value) console.log(value.toString())
 });
});

Merkle Proofs

Trie.prove(trie, 'test', function (err, prove) {
 if (err) return cb(err)
 Trie.verifyProof(trie.root, 'test', prove, function (err, value) {
   if (err) return cb(err)
   console.log(value.toString())
   cb()
 })
})

阅读Geth DB上的流

var level = require('level')
var Trie = require('./secure')

var stateRoot = "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544" // Block #222

var db = level('YOUR_PATH_TO_THE_GETH_CHAIN_DB')
var trie = new Trie(db, stateRoot)

trie.createReadStream()
 .on('data', function (data) {
   console.log(data)
 })
 .on('end', function() {
   console.log('End.')
 })


8、ethereumjs-util

以太坊的实用功能集合

以太坊的实用功能集合。它可以在node.js中使用,也可以在浏览器中使用browserify。

大多数字符串操作方法都是由ethjs-util提供的


9、ethashjs

纯Javascript中的ethash实现


9.1安装

npm install ethashjs


9.2 用法

const Ethash = require('ethashjs')
const Block = require('ethereumjs-block')
const levelup = require('levelup')
const memdown = require('memdown')

var cacheDB = levelup('', {
 db: memdown
})

var ethash = new Ethash(cacheDB);
var validblockRlp = "f90667f905fba0a8d5b7a4793baaede98b5236954f634a0051842df6a252f6a80492fd888678bda01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0f93c8db1e931daa2e22e39b5d2da6fb4074e3d544094857608536155e3521bc1a0bb7495628f9160ddbcf6354380ee32c300d594e833caec3a428041a66e7bade1a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b84559c17b9b9040001020304050607080910111213141516171819202122232410000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000100000000000000000002000000000000000000030000000000000000000400000000000000000005000000000000000000060000000000000000000700000000000000000008000000000000000000090000000000000000000100000000000000000001000000000000000000020000000000000000000300000000000000000004000000000000000000050000000000000000000600000000000000000007000000000000000000080000000000000000000900000000000000000001000000000000000000010000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000100000000000000000002000000000000000000030000000000000000000400000000000000000005000000000000000000060000000000000000000700000000000000000008000000000000000000090000000000000000000100000000000000000001000000000000000000020000000000000000000300000000000000000004000000000000000000050000000000000000000600000000000000000007000000000000000000080000000000000000000900000000000000000001000000000000000000010000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000100000000000000000002000000000000000000030000000000000000000400000000000000000005000000000000000000060000000000000000000700000000000000000008000000000000000000090000000000000000000100000000000000000001000000000000000000020000000000000000000300000000000000000004000000000000000000050000000000000000000600000000000000000007000000000000000000080000000000000000000900000000000000000001000000000000000000010000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000a09c7b47112a3afb385c12924bf6280d273c106eea7caeaf5131d8776f61056c148876ae05d46b58d1fff866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba01d2c92cfaeb04e53acdff2b5d42005ff6aacdb0105e64eb8c30c273f445d2782a01e7d50ffce57840360c57d94977b8cdebde614da23e8d1e77dc07928763cfe21c0"

var validblock = new Block(new Buffer(validblockRlp, 'hex'))

ethash.verifyPOW(validblock, function (result) {
 console.log(result)
})

以上是关于核心以太坊函数的JavaScript库的主要内容,如果未能解决你的问题,请参考以下文章

以太坊 Input Data 解析

第一行代码:以太坊-创建自己的私有区块链

第一行代码:以太坊-创建自己的私有区块链

python以太坊开发的主要相关技术

第一行代码:以太坊-使用Solidity语言开发和测试智能合约

RSK<->以太坊 trustless bridge