@acryl/signature-adapter 中文文档教程

发布于 4 年前 浏览 19 项目主页 更新于 2 年前

JS library for signing data

Installation

$ npm install --save @acryl/signature-adapter

Usage

import { SeedAdapter, SIGN_TYPE } from '@acryl/signature-adapter';
import { Money, Asset } from '@acryl/data-entities';

const asset = new Asset({
   ticker: 'ACRYL',
   id: 'ACRYL',
   name: 'Acryl',
   precision: 8,
   description: '',
   height: 0,
   timestamp: new Date('2016-04-11T21:00:00.000Z'),
   sender: '',
   quantity: 10000000000000000,
   reissuable: false
});

const transferTransactionData = {
   recipient: 'some address or alias',
   amount: Money.fromTokens(1, asset),
   attachment: 'Some attachment text less 140 bytes',
   fee: Money.fromTokens(0.001, asset)
};

const adapter = new SeedAdapter('some seed phrase with 15 or more chars');
const signable = adapter.makeSignable({
   type: SIGN_TYPE.TRANSFER,
   data: transferTransactionData
});

signable.getDataForApi().then(data => fetch('node-url', {
   method: 'POST',
   body: JSON.stringify(data)
}));

适配器用于签署数据。 有两种类型的适配器: SeedAdapter - 是一个适配器,用于在字符串表示中使用种子 LedgerAdapter - 是与 Ledger 硬件钱包一起使用的适配器

SeedAdapter

SeedAdapter 接受助记词或包含有关创建助记词信息的对象 对象结构: encryptedSeed {string} - 用密码编码的种子短语 password {string} - 加密助记词的密码 encryptionRounds {number} - 加密复杂性

要更改网络字节,请使用 @acryl/signature-generator 配置:

import { config } from '@acryl/signature-generator';

config.set('networkByte', 'K'.charCodeAt(0))

如果您使用种子短语创建 SeedAdapter,请注意短语的最小长度为 15 个字符。 有关详细信息,请参阅文档@acryl/signature-generator

使用助记词创建 SeedAdapter 的示例:

import { SeedAdapter } from '@acryl/signature-adapter';

const adapter = new SeedAdapter('some seed phrase more 15 chars');

LedgerAdapter

SeedAdapter 在创建时接受一个对象,该对象包含有关 Ledger 中的地址和钱包 ID 的信息 对象结构:

  • publicKey {string} - public key for the address
  • address {string} - address
  • id {number} - wallet ID in Ledger

要获取用于创建 LedgerAdapter 实例的对象,有类“getUserList”的静态方法。

  • getUserList(from?: number, to?: number): Promise>;

其中“from”是 Ledger 中的钱包 ID,从中开始接收数据,“to”- 完成的数字。

示例:

import { LedgerAdapter } from '@acryl/signature-adapter';

LedgerAdapter.getUserList().then(([userData]) => new LedgerAdapter(userData));

Common methods for both types of adapters

  • isAvailable(): Promise\; 返回 promise,它在适配器不可用时返回错误。

  • getPublicKey(): Promise\<字符串>; 返回公钥

  • getAddress(): Promise\; 返回地址

  • getPrivateKey(): Promise\; 返回私钥或错误如果适配器无法返回它(在 LedgerAdapter 的情况下)

  • signRequest(bytes: Uint8Array): Promise\; 接受用于签名的字节数组,返回带有签名的承诺。 此方法用于签署除交易和订单之外的任何数据

  • signTransaction(bytes: Uint8Array): Promise\; 接受用于签名的字节数组,返回带有签名的承诺。 该方法用于签署交易

  • signOrder(bytes: Uint8Array): Promise\; 接受用于签名的字节数组,返回带有签名的承诺。 该方法用于签署订单

  • signData(bytes: Uint8Array): Promise\; 接受用于签名的字节数组,返回带有签名的承诺。 此方法用于签署任何数据 由于 Ledger 上的确认显示不正确,不建议使用此方法签署交易

  • getSeed(): Promise\; 返回种子短语或错误如果适配器无法返回它(在 LedgerAdapter 的情况下)

  • makeSignable(signData): Signable; 接受签名数据(访问interfaces获取更多关于“ TSignData”接口)并返回 Signable 类

Signable

  • getTxData(): {object} 的一个实例; 返回为签名传输的对象数据的副本

  • getSignData(): Promise\; 返回用于制作签名的接收到的签名

  • 数据 接受签名以将其添加到签名列表。 签名列表用于“getDataForApi”方法

  • getId(): Promise\; 返回带有用于签名的所有数据的散列的承诺

  • sign(): Promise\; 方法启动签名并返回等待签名完成的承诺。 此方法在 Signable 类的一个实例的生命周期内仅添加一个签名。 当您再次调用它时,它会返回一个先前创建的承诺

  • getSignature(): Promise\; 与 sign() 方法相同,但返回带有签名

  • getBytes() 的承诺:Promise\; 返回用于签名的字节

  • getMyProofs(): Promise\>; 返回属于适配器公钥的签名数组,从中创建类 Signable 的实例

  • hasMySignature(): Promise\; 返回具有关于“自己的”签名存在的布尔状态的承诺(与“getMyProofs”方法中的相同)

  • addMyProof(): Promise\; 此方法检查“自己”签名的存在。 如果签名存在,它返回最后一个“自己的”签名,否则它签名并返回新的签名

  • getDataForApi():Promise\; 调用方法“addMyProof”以保证存在一个“自己的”签名。 返回准备发送到节点的数据。 添加签名列表

JS library for signing data

Installation

$ npm install --save @acryl/signature-adapter

Usage

import { SeedAdapter, SIGN_TYPE } from '@acryl/signature-adapter';
import { Money, Asset } from '@acryl/data-entities';

const asset = new Asset({
   ticker: 'ACRYL',
   id: 'ACRYL',
   name: 'Acryl',
   precision: 8,
   description: '',
   height: 0,
   timestamp: new Date('2016-04-11T21:00:00.000Z'),
   sender: '',
   quantity: 10000000000000000,
   reissuable: false
});

const transferTransactionData = {
   recipient: 'some address or alias',
   amount: Money.fromTokens(1, asset),
   attachment: 'Some attachment text less 140 bytes',
   fee: Money.fromTokens(0.001, asset)
};

const adapter = new SeedAdapter('some seed phrase with 15 or more chars');
const signable = adapter.makeSignable({
   type: SIGN_TYPE.TRANSFER,
   data: transferTransactionData
});

signable.getDataForApi().then(data => fetch('node-url', {
   method: 'POST',
   body: JSON.stringify(data)
}));

Adapters are used to sign data. There are two types of adapters: SeedAdapter - is an adapter for usage with seed in string representation LedgerAdapter - is an adapter for usage with Ledger hardware wallet

SeedAdapter

SeedAdapter accepts either a seed phrase or an object containing information about the seed phrase on create Object structure: encryptedSeed {string} - seed phrase encoded with passwords password {string} - password that is encrypted seed phrase encryptionRounds {number} - encryption complexity

For change network byte use @acryl/signature-generator config:

import { config } from '@acryl/signature-generator';

config.set('networkByte', 'K'.charCodeAt(0))

If you use seed phrase to create SeedAdapter note that the minimum length of a phrase is 15 characters. See the documentation @acryl/signature-generator for more details

Example of creation SeedAdapter with seed phrase:

import { SeedAdapter } from '@acryl/signature-adapter';

const adapter = new SeedAdapter('some seed phrase more 15 chars');

LedgerAdapter

SeedAdapter accepts an object that contains information about an address and wallet ID in Ledger on create Object structure:

  • publicKey {string} - public key for the address
  • address {string} - address
  • id {number} - wallet ID in Ledger

To get an objects for creating an instance of LedgerAdapter there is a static method of the class "getUserList".

  • getUserList(from?: number, to?: number): Promise>;

Where ‘from’ is wallet ID in the Ledger from which to start receiving data, ‘to’ - the number to finish.

Example:

import { LedgerAdapter } from '@acryl/signature-adapter';

LedgerAdapter.getUserList().then(([userData]) => new LedgerAdapter(userData));

Common methods for both types of adapters

  • isAvailable(): Promise\<void>; Returns promise which returns an error in case the adapter is unavailable.

  • getPublicKey(): Promise\<string>; Returns a public key

  • getAddress(): Promise\<string>; Returns an address

  • getPrivateKey(): Promise\<string>; Returns a private key or an error If adapter is not able to return it (in case of LedgerAdapter)

  • signRequest(bytes: Uint8Array): Promise\<string>; Accepts byte array for signing, returns promise with a signature. This method is used for signing any data except transactions and orders

  • signTransaction(bytes: Uint8Array): Promise\<string>; Accepts byte array for signing, returns promise with a signature. This method is used for signing transactions

  • signOrder(bytes: Uint8Array): Promise\<string>; Accepts byte array for signing, returns promise with a signature. This method is used for signing orders

  • signData(bytes: Uint8Array): Promise\<string>; Accepts byte array for signing, returns promise with a signature. This method is used for signing any data This method is not recommended for signing transactions due to incorrect confirmation display on Ledger

  • getSeed(): Promise\<string>; Returns the seed phrase or an error If adapter is not able to return it (in case of LedgerAdapter)

  • makeSignable(signData): Signable; Accepts data for signature (Visit interfaces for more information about “TSignData” interface) and returns an instance of the Signable class

Signable

  • getTxData(): {object}; Returns a copy of the object data transferred for signature

  • getSignData(): Promise\<object>; Returns data for signing as received for making signature @acryl/signature-generator

  • addProof(proof: string): Signable; Accepts the signature for adding it to the signature list. Signature list is used for "getDataForApi" method

  • getId(): Promise\<string>; Returns promise with hash of all data used for signature

  • sign(): Promise\<Signable>; Method initiates signing and returns promise that waits the completion of signing. This method adds only one signature for the lifetime of one instance of the Signable class. When you call it again it returns a previously created promise

  • getSignature(): Promise\<string>; Acts same as sign() method, but returns promise with signature

  • getBytes(): Promise\<Uint8Array>; Returns bytes for signing

  • getMyProofs(): Promise\<Array\<string>>; Returns array of signatures that belongs to public key of the adapter from which the instance of class Signable was created

  • hasMySignature(): Promise\<boolean>; Returns promise with boolean status about presence of “own” signature (Same as in “getMyProofs” method)

  • addMyProof(): Promise\<string>; This method checks the presence of “own” signature. If signature exists it returns the last “own” signature, otherwise its signs and returns the new signature

  • getDataForApi():Promise\<object>; Calls the method “addMyProof” to guarantee the presence of one “own” signature. Returns the data that is prepared to send into node. Adds the list of signatures

更多

友情链接

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