@acryl/ledger 中文文档教程

发布于 5年前 浏览 10 项目主页 更新于 3年前

Acryl sign data by ledger in browser

Install

$ npm install --save @acryl/ledger

Usage

Create connection
const AcrylLedger = require('@acryl/ledger');
const ledger = new AcrylLedger();

AcrylLedger 可以有可选参数。

    import TransportNodeHid from '@ledgerhq/hw-transport-node-u2f';

    const options = {
        debug: true, //boolean,
        openTimeout: 3000, //number,
        listenTimeout: 30000, //number,
        exchangeTimeout: 30000, //number,
        networkCode: 65, //number,
        transport: TransportNodeHid
    };

    const ledger = new AcrylLedger(options);
Where:

debug 是否启用二进制交换日志。 openTimeout 是等待连接的延迟数(以毫秒为单位)。
listenTimeout 是等待 U2F 设备监听请求的延迟数(以毫秒为单位)。
exchangeTimeout 是交换调用的超时(以毫秒为单位)。 只有一些传输可能会实现它。 (例如 U2F)。
networkCode 是 Acryl 网络代码(65 - 对于 mainet)
transport 是 U2F 传输实现。 (默认为 hw-transport-u2f)

阅读有关传输的信息

AcrylLedger API

  • probeDevice

    probeDevice(): Promise。 如果设备可用且准备就绪,则 Promise 返回 true。

      const canIUse = async () => {
          return await ledger.probeDevice();
      }
    
  • tryConnect

    tryConnect():Promise<>。 重新连接到传输和初始化分类帐库。

    ```js 常量 isLedgerReady = async () => { 尝试 { 返回 await ledger.tryConnect(); } 抓住 (e) { ///…错误处理程序 } };

  • getUserDataById

    getUserDataById(id): Promise<用户>。 从分类帐中获取用户,其中用户是: { id: number, path: string, acrylAddress: string, publicKey: string }

    id - 是从 0 开始的数字
    路径 - 是内部分类帐格式的字符串
    acrylAddress - 是 base58 格式的字符串
    publicKey - 是 base58 格式的字符串

    ledger.getUserDataById(id)
        .then(
            (user) => {...},
            (err) => {...}
        );
    
  • signTransaction

    Sign acryl transaction bytes (ledger show detailed transaction info)

        ledger.signTransaction(userId, asset, data)
    

    userId - number
    数据 - Array数字形式 0 到 255 asset - {precision: number, …}

    结果是带有 base58 格式签名字符串的 Promise

  • signSomeData

    签署任何字节(账本不能显示详细信息)

      ledger.signSomeData(userId, data)
    

    userId - number
    data - Arraynumber form 0 to 255

    Result is Promise with signature string in base58 format

  • signRequest

    签署任何字节(账本不能显示详细信息)

      ledger.signRequest(userId, data)
    

    userId - number
    data - Arraynumber form 0 to 255

    结果是 Promise with base58 format signature string

  • signMessage

    Sign any string (ledger can't show detail info)

    ledger.signMessage(userId, data)
    

    userId - number
    data - string

    结果是带有 base58 格式签名字符串的 Promise

Acryl sign data by ledger in browser

Install

$ npm install --save @acryl/ledger

Usage

Create connection
const AcrylLedger = require('@acryl/ledger');
const ledger = new AcrylLedger();

AcrylLedger can have optional arguments.

    import TransportNodeHid from '@ledgerhq/hw-transport-node-u2f';

    const options = {
        debug: true, //boolean,
        openTimeout: 3000, //number,
        listenTimeout: 30000, //number,
        exchangeTimeout: 30000, //number,
        networkCode: 65, //number,
        transport: TransportNodeHid
    };

    const ledger = new AcrylLedger(options);
Where:

debug enable or not logs of the binary exchange. openTimeout is a delay number in ms for waiting connection.
listenTimeout is a delay number in ms for waiting listen request to U2F device.
exchangeTimeout is a timeout (in milliseconds) for the exchange call. Only some transport might implement it. (e.g. U2F).
networkCode is Acryl network code (65 - for mainet)
transport is U2F Transport implementation. (hw-transport-u2f by default)

Read about transport

AcrylLedger API

  • probeDevice

    probeDevice(): Promise. If device available and ready, Promise returned true.

      const canIUse = async () => {
          return await ledger.probeDevice();
      }
    
  • tryConnect

    tryConnect(): Promise<>. reconnect to Transport and init ledger libs.

    ```js const isLedgerReady = async () => { try { return await ledger.tryConnect(); } catch (e) { ///…error handlers } };

  • getUserDataById

    getUserDataById(id): Promise. Get user from ledger, where user is: { id: number, path: string, acrylAddress: string, publicKey: string }

    id - is number from 0
    path - is string in internal ledger format
    acrylAddress - is string in base58 format
    publicKey - is string in base58 format

    ledger.getUserDataById(id)
        .then(
            (user) => {...},
            (err) => {...}
        );
    
  • signTransaction

    Sign acryl transaction bytes (ledger show detailed transaction info)

        ledger.signTransaction(userId, asset, data)
    

    userId - number
    data - Arraynumber form 0 to 255 asset - {precision: number, …}

    Result is Promise with signature string in base58 format

  • signSomeData

    Sign any bytes (ledger can't show detail info)

      ledger.signSomeData(userId, data)
    

    userId - number
    data - Arraynumber form 0 to 255

    Result is Promise with signature string in base58 format

  • signRequest

    Sign any bytes (ledger can't show detail info)

      ledger.signRequest(userId, data)
    

    userId - number
    data - Arraynumber form 0 to 255

    Result is Promise with signature string in base58 format

  • signMessage

    Sign any string (ledger can't show detail info)

    ledger.signMessage(userId, data)
    

    userId - number
    data - string

    Result is Promise with signature string in base58 format

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文