Bitski 提供引擎
Bitski的"提供商Engine"是Metamask原版提供商引擎库的再造版本. 这是Bitski Javascript SDKs(既是浏览器又是节点)的基础.
安装
npm install @bitski/provider-engine
与原始软件包的差异
- 用打字符写
- 先为浏览器设计的ES6标准模块
- 设计以适当导出子提供者接口来扩展
- 基础类执行新的 Ethereum 提供标准
- 与最新版本的web3.js相兼容
- 清除重度依赖度以保持基包光度(主要是依赖于椭圆值的所有依赖度)
- 许多小错误修正和改进
待执行
- 作为单独软件包的 HookedWallet 子提供商
- 将 VmSub 提供者作为单独的软件包
- 将非跟踪器作为单独的软件包
- 提高测试覆盖率
可编曲
构建为模块化的 - 通过一叠“ 子提供者” 工作, 类似普通的 Web3 提供者, 但只处理一组 rpc 方法 .
子提供者可以发出新的rpc请求,以便处理自己的请求;例如. eth_call 可能触发 eth_getAccountBalance, (中文(简体) ), eth_getCode还有其他.
供应商引擎还处理缓存的rpc请求结果.
import {
default as ProviderEngine,
BlockCacheSubprovider,
FixtureSubprovider,
FilterSubprovider,
FetchSubprovider
} from '@bitski/provider-engine';
const engine = new ProviderEngine();
const web3 = new Web3(engine);
// static results
engine.addProvider(new FixtureSubprovider({
web3_clientVersion: 'ProviderEngine/v0.0.0/javascript',
net_listening: true,
eth_hashrate: '0x00',
eth_mining: false,
eth_syncing: true,
}));
// cache layer
engine.addProvider(new BlockCacheSubprovider());
// filters
engine.addProvider(new FilterSubprovider());
// data source
engine.addProvider(new FetchSubprovider({
rpcUrl: 'https://testrpc.metamask.io/',
}));
// log new blocks
engine.on('block', (block) => {
console.log('================================')
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
console.log('================================')
});
// network connectivity error
engine.on('error', (err) => {
// report connectivity errors
console.error(err.stack);
});
// start polling for blocks
engine.start();
写入您的子提供者
通过写出自己的子提供者实例来扩展此模块的功能是很容易的.
见 src/子提供者.ts 用于完整的接口.
import { Subprovider } from '@bitski/provider-engine';
export default class MySubprovider extends Subprovider {
// Only requirement is to implement handleRequest
public handleRequest(payload, next, end) {
// The payload includes the original JSON RPC request
if (payload.method === 'eth_helloWorld') {
// Call end() to handle the request in this subprovider
end('hello world!');
} else {
// Call next() to fall-through to the next request in the stack
next();
}
}
}
承认
特别感谢Metamask的朋友们 构思并撰写了原始图书馆.