nativeScript-electrumx-client
由 pixxl 开发 | v0.1.20
为 NativeScript 提供的 ElectrumX 客户端包装器
npm i --save nativescript-electrumx-client

NativeScript ElectrumX-Client android-support

ODIN Powered

npm version license

NPM

ElectrumX-Client NativeScript 插件允许您的应用程序与 ElectrumX 服务器 进行通信。ElectrumX 服务器充当中间件,允许远程客户端通过 TCP 和 TLS 套接字获取和发送区块链请求以获取和发送与区块链相关的信息。

需求

此插件需要 android.permission.INTERNET 以正常工作。这必须添加到您的 AndroidManifest.xml 文件中。

安装

$ tns plugin add nativescript-electrumx-client

用法

只需从该插件导入 ElectrumxClient 并开始在您的应用程序中使用。此插件支持 async/await/promises 以提供回调结构,并使用 events 允许对特定事件和来自 ElectrumX 服务器的流进行订阅。

以下列出示例用法

import { ElectrumxClient } from 'nativescript-electrumx-client';

export class HelloWorldModel {
private electrumxClient: ElectrumxClient;

constructor() {
this.electrumxClient = new ElectrumxClient('server.example.com', 50001);
this.setupSubscriptions(); // subscription examples
this.initClient(); // initialization example
}

public setupSubscriptions() {
let _client = this.electrumxClient;

/**
* Subscribe to all incoming data from the ElectrumX server
* to your application.
* @param rawData Is the raw string response.
*/
_client.subscribe.on('data', (rawData: string) => {
});

/**
* Subscribe to all finished actions.
* @param tcpActionId Is a unique, incremented ID assigned to each action.
*/
_client.subscribe.on('finished', (tcpActionId: number) => {
});

/**
* Subscribe to any errors streamed from this plugin.
* There are two primary error types to watch out for:
*
* err.name === "UnexpectedResponseError"
* This error comes from an unexpected response from ElectrumX as
* ElectrumX should always return a JSON.parse-able string response.
*
* err.name === "TCPClientError"
* This error comes from the base class TcpClient when a connection
* fails.
*/
_client.subscribe.on('error', async (err) => {
});

/**
* Subscribe to a particular "subscribable" event from ElectrumX.
* @param args Contains the parsed result from the ElectrumX server.
* Results may vary (heh) view more from ElectrumX Server Docs.
*/
_client.subscribe.on('blockchain.scripthash.subscribe', (...args) => {
});
}

public async init() {
try {
// Connect to the host/port set earlier
await this.electrumxClient.connect();

// Fetch the version of the remote ElectrumX Server
let version = await this.electrumxClient.server_version('2.7.11', '1.1');
console.log(`Remote Version: ${version[0]}`);
} catch (err) {
console.log('Connection error');
}
}
}

限制

此插件目前不支持 iOS 设备(欢迎和期望对 iOS 支持的贡献!)并且目前仅支持通过 TCP 的套接字连接。TLS 支持将会很棒,但需要进一步改进我们正在工作的 SimpleNetworking 插件。

致谢

此插件包含基于以下来源修改的代码

node-electrum-client https://github.com/you21979/node-electrum-client
nativescript-simple-networking https://github.com/yaqwsx/nativescript-simple-networking

我想感谢上述作品的贡献者和作者,他们的解决方案使此插件更容易解决(有点吧 😅)。