API Documentation

Identity

Crypto / API Documentation / Identity / Address

Derive an address from a passphrase

static fromPassphrase(passphrase: string): string

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase

Return Value

This function returns an address derived from the given passphrase as a string.

import { Address } from '@arkecosystem/typescript-crypto';

const address = Address.fromPassphrase('super secret passphrase');

>> '0x2289577F3784788784226Eb41E9B0ca9705C7C37'

Derive an address from a public key

static fromPublicKey(publicKey: string): string

Parameters

TypeNameRequiredDescription
stringpublicKeyYesPublic key

Return Value

This function returns an address derived from the given public key as a string.

import { Address } from '@arkecosystem/typescript-crypto';

const address = Address.fromPublicKey('023efc1da7f315f3c533a4080e491f32cd4219731cef008976c3876539e1f192d3');

>> '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22'

Derive an address from a private key

static fromPrivateKey(privateKey: string): string

Parameters

TypeNameRequiredDescription
EccPrivateKeyprivateKeyYesPrivate key

Return Value

This function returns an address derived from the given private key as a string.

import { Address, PrivateKey } from '@arkecosystem/typescript-crypto';

const privateKey = PrivateKey.fromPassphrase('this is a top secret passphrase');
const address = Address.fromPrivateKey($privateKey);

>> '0xb0FF9213f7226bBB72b84dE16af86e56f1f38B01'

Validate the given address

static validate(address: string): boolean

Parameters

TypeNameRequiredDescription
stringaddressYesAddress to validate

Return Value

This method returns a boolean indicating the validity of the address.

import { Address } from '@arkecosystem/typescript-crypto';

const isValid = Address.validate('0x2289577F3784788784226Eb41E9B0ca9705C7C37');

>> true
Crypto / API Documentation / Identity / PrivateKey

Derive a private key from a passphrase

static fromPassphrase(passphrase: string): PrivateKey

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase

Return Value

This method returns a PrivateKey object derived from the given passphrase.

import { PrivateKey } from '@arkecosystem/typescript-crypto';

const privateKey = PrivateKey.fromPassphrase('this is a top secret passphrase');

>> PrivateKey

Derive a private key from a hex string

static fromHex(hex: string): PrivateKey

Parameters

TypeNameRequiredDescription
stringprivateKeyYesHexadecimal representation of the private key

Return Value

This method returns a PrivateKey object derived from a hex string.

import { PrivateKey } from '@arkecosystem/typescript-crypto';

const privateKey = PrivateKey.fromHex('d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712');

>> PrivateKey

Derive a private key from a WIF

static fromWif(wif: string): PrivateKey

Parameters

TypeNameRequiredDescription
stringwifYesHexadecimal representation of the WIF string

Return Value

This method returns a PrivateKey object derived from a WIF string.

import { PrivateKey } from '@arkecosystem/typescript-crypto';

const privateKey = PrivateKey.fromWif('UZYnRZ8qpeQWTLeCNzw93guWSdKLmr2vHEWGG4sNv7TJofL7TZvy');

>> PrivateKey

Signs a message using the private key

async sign(message: string): Promise<RecoverableSignature>

Parameters

TypeNameRequiredDescription
stringmessageYesMessage to sign in hex (without 0x)

Return Value

This method signs a message using the private key.

import { PrivateKey } from '@arkecosystem/typescript-crypto';

const privateKey = PrivateKey.fromPassphrase('this is a top secret passphrase');
const signature = await privateKey.sign(Buffer.from("Hello World").toString('hex'));

>> RecoverableSignature
Crypto / API Documentation / Identity / PublicKey

Derive a public key from a passphrase

static fromPassphrase(passphrase: string): PublicKey

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase

Return Value

This function returns a public key instance object derived from the given passphrase.

import { PublicKey } from '@arkecosystem/typescript-crypto';

const publicKey = PublicKey.fromPassphrase('this is a top secret passphrase');

>> PublicKey

Create a public key instance from a hex string

static fromHex(publicKey: string): PublicKey

Parameters

TypeNameRequiredDescription
stringpublicKeyYesPublic key in hex string or Buffer format

Return Value

This function returns a PublicKey object created from the given hex string.

import { PublicKey } from '@arkecosystem/typescript-crypto';

const publicKey = PublicKey.fromHex('034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192');

>> PublicKey

Recover a public key from a signature

static recover(message: Buffer, r: bigint, s: bigint, v: number): PublicKey

Parameters

TypeNameRequiredDescription
stringmessageYesMessage to use to recover from in Buffer format
bigintrYesSignature r in bigint format
bigintsYesSignature s in bigint format
numbervYesSignature Recovery ID

Return Value

This function returns a PublicKey object recovered from the given signature and message.

import { PublicKey } from '@arkecosystem/typescript-crypto';

const publicKey = PublicKey.recover('message', r, s, v);

>> PublicKey
Crypto / API Documentation / Identity / WIF

Derive a WIF from a passphrase

static fromPassphrase(passphrase: string): string

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase

Return Value

This method returns a WIF in string format.

import { WIF } from '@arkecosystem/typescript-crypto';

const wif = WIF.fromPassphrase('this is a top secret passphrase');

>> WIF

Network

Crypto / API Documentation / Configuration / Network

Set the network you want to use in the crypto library

public static set(network: AbstractNetwork): void

Parameters

TypeNameRequiredDescription
AbstractNetworknetworkYesNetwork object (Testnet, Mainnet, etc)

Return Value

This method returns void.

import { Network, Testnet } from '@arkecosystem/typescript-crypto';

Network.set(new Testnet());

>> void
// Custom network example

import { AbstractNetwork, Network } from '@arkecosystem/typescript-crypto';

class CustomNetwork extends AbstractNetwork {
  ...
}

Network.set(new CustomNetwork());

>> void

Get the currently used network

public static get(): AbstractNetwork

Return Value

This function returns the current network.

import { Network } from '@arkecosystem/typescript-crypto';

const network = Network.get();

>> AbstractNetwork

Get the chain ID of the Network

public chainId(): number

Return Value

This method returns the chain ID as an integer.

import { Testnet } from '@arkecosystem/typescript-crypto';

new Testnet().chainId();

>> 10000

Get the epoch time of the start of the Network

public epoch(): string

Return Value

This method returns the epoch time in string format.

import { Testnet } from '@arkecosystem/typescript-crypto';

(new Testnet()).epoch();

>> '2017-03-21T13:00:00.000Z'

Get the WIF of the Network as a hexadecimal string

public wif(): string

Return Value

This method returns the WIF in hexadecimal string format.

import { Testnet } from '@arkecosystem/typescript-crypto';

new Testnet().wif();

>> 'ba'

Transactions

Crypto / API Documentation / Transactions / AbstractTransactionBuilder

For full examples of using the Builders to create transactions, please have a look at the Examples page.

Create a new transaction instance

public static new(data?: Record<string, any>): T

Parameters

TypeNameRequiredDescription
Record<string, any> | undefineddataNoOptional transaction data

Return Value

This method returns a new transaction instance in self format.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = TransferBuilder.new();

>> TransferBuilder

Convert the transaction to its string representation

public toString(): string

Return Value

This method returns the string representation of the transaction.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = TransferBuilder.new().toString();

>> '{"gasPrice":"5000000000","network":30,"gasLimit":"1000000","nonce":"1","senderPublicKey":"","value":"0","data":""}'

Set the gas limit for the transaction

public gasLimit(gas: string | BigNumber): T

Parameters

TypeNameRequiredDescription
string | BigNumbergasYesGas limit for the transaction

Return Value

This method returns the transaction instance in self format.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = TransferBuilder.new().gasLimit('1000000');
const anotherTransaction = TransferBuilder.new().gasLimit(UnitConverter.parseUnits('0.1', 'gwei'));

>> TransferBuilder

Set the recipient address for the transaction

public to(to: string): T

Parameters

TypeNameRequiredDescription
stringtoYesRecipient address

Return Value

This method returns the transaction instance in self format.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = TransferBuilder.new().to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22');

>> TransferBuilder

Set the gas price for the transaction

public gasPrice(gasPrice: string | BigNumber): T

Parameters

TypeNameRequiredDescription
string | BigNumbergasPriceYesGas price for the transaction

Return Value

This method returns the transaction instance in self format.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = TransferBuilder.new().gasPrice('5000000000');
const anotherTransaction = TransferBuilder.new().gasPrice(UnitConverter.parseUnits('5', 'gwei'));

>> TransferBuilder

Set the nonce for the transaction

public nonce(nonce: string): T

Parameters

TypeNameRequiredDescription
stringnonceYesNonce for the transaction

Return Value

This method returns the transaction instance in self format.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = TransferBuilder.new().nonce('1');

>> TransferBuilder

Set the network for the transaction

public network(network: number): T

Parameters

TypeNameRequiredDescription
intnetworkYesNetwork identifier (e.g., Mainnet, Testnet)

Return Value

This method returns the transaction instance in self format.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = TransferBuilder.new().network(23);

>> TransferBuilder

Sign the transaction using the given passphrase

public async sign(passphrase: string): Promise<T>

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase for signing the transaction

Return Value

This method signs the transaction using the provided passphrase and returns the builder instance.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = await TransferBuilder.new().sign('this is a top secret passphrase');

>> TransferBuilder

Sign the transaction with a second signature (only for legacy addresses)

public async legacySecondSign(passphrase: string, secondPassphrase: string): Promise<T>

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase for signing the transaction
stringsecondPassphraseYesSecond Passphrase for signing the transaction

Return Value

This method signs the transaction using the provided passphrases and returns the builder instance.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const transaction = await TransferBuilder.new().legacySecondSign('this is a top secret passphrase','this is a top secret second passphrase');

>> TransferBuilder

Verify the transaction validity

public verify(): boolean

Return Value

This method returns the validity of the transaction as a boolean.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const isValid = (await TransferBuilder.new()
  .to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  .sign('this is a top secret passphrase'))
  .verify();

>> true

Convert the transaction to an object

public toObject(): TransactionData

Return Value

This method returns the transaction data as an array.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const data = TransferBuilder.new().toObject();

>> {
  "gasPrice": "5000000000",
  "network": 30,
  "gasLimit": 1000000,
  "nonce": 1,
  "senderPublicKey": "",
  "value": "0",
  "data": "",
}

Convert the transaction to its JSON representation

public toJson(): string

Return Value

This method returns the JSON representation of the transaction as a string.

import { TransferBuilder } from '@arkecosystem/typescript-crypto';

// Using TransferBuilder as an example of AbstractTransactionBuilder
const json = TransferBuilder.new().toJson();

>> '{"value": "0", ..., "data": ""}'
Crypto / API Documentation / Transactions / EvmCallBuilder

Inherits from:AbstractTransactionBuilder

Set the payload for the EVM call

public payload(payload: string): IEvmCallBuilder

Parameters

TypeNameRequiredDescription
stringpayloadYesEVM code in hex format

Return Value

Returns the transaction instance with the payload set.

import { EvmCallBuilder } from '@arkecosystem/typescript-crypto';

const transaction = EvmCallBuilder.new().payload('yourHexPayload');

>> EvmCallBuilder
Crypto / API Documentation / Transactions / MultipaymentBuilder

Inherits from:AbstractTransactionBuilder

Add recipient to the transaction

public pay(address: string, value: string | BigNumber): IMultipaymentBuilder

Parameters

TypeNameRequiredDescription
stringaddressYesAddress to recipient
string | BigNumbervalueYesAmount to send to recipient

Return Value

This method adds a recipient to the transaction and returns the builder instance.

import { MultipaymentBuilder } from '@arkecosystem/typescript-crypto';

const transaction = MultipaymentBuilder.new().pay('0x512F366D524157BcF734546eB29a6d687B762255', '100000');
const anotherTransaction = MultipaymentBuilder.new().pay('0x512F366D524157BcF734546eB29a6d687B762255', UnitConverter.parseUnits('0.1', 'gwei'));

>> MultipaymentBuilder
Crypto / API Documentation / Transactions / TransferBuilder

Inherits from:AbstractTransactionBuilder

Set the value to transfer (in wei)

public value(value: string | BigNumber): ITransferBuilder

Parameters

TypeNameRequiredDescription
string | BigNumbervalueYesAmount to transfer in wei

Return Value

This method returns the transaction instance with the value set.

import { TransferBuilder, UnitConverter } from '@arkecosystem/typescript-crypto';

const transaction = TransferBuilder.new().value('1000000000000000000');
const anotherTransaction = TransferBuilder.new().value(UnitConverter.parseUnits('1', 'ark'));

>> TransferBuilder
Crypto / API Documentation / Transactions / ValidatorRegistrationBuilder

Inherits from:AbstractTransactionBuilder

Set the validator BLS public key

public validatorPublicKey(validatorPublicKey: string): IValidatorRegistrationBuilder

Parameters

TypeNameRequiredDescription
stringvalidatorPublicKeyYesValidator BLS public key to register

Return Value

This method returns the transaction instance with the validator public key set.

import { ValidatorRegistrationBuilder } from '@arkecosystem/typescript-crypto';

const transaction = ValidatorRegistrationBuilder.new().validatorPublicKey('954f46d6097a1d314e900e66e11e0dad0a57cd03e04ec99f0dedd1c765dcb11e6d7fa02e22cf40f9ee23d9cc1c0624bd');

>> ValidatorRegistrationBuilder

Set the locked value (in wei)

public value(value: string | BigNumber): IValidatorRegistrationBuilder

Parameters

TypeNameRequiredDescription
string | BigNumbervalueYesAmount to lock in wei

Return Value

This method returns the transaction instance with the value set.

import { ValidatorRegistrationBuilder, UnitConverter } from '@arkecosystem/typescript-crypto';

const transaction = ValidatorRegistrationBuilder.new().value('250000000000000000000');
const anotherTransaction = ValidatorRegistrationBuilder.new().value(UnitConverter.parseUnits('250', 'ark'));

>> ValidatorRegistrationBuilder
Crypto / API Documentation / Transactions / ValidatorResignationBuilder

Inherits from:AbstractTransactionBuilder

Crypto / API Documentation / Transactions / UsernameRegistrationBuilder

Inherits from:AbstractTransactionBuilder

Set the Username

public username(username: string): IUsernameRegistrationBuilder

Parameters

TypeNameRequiredDescription
stringusernameYesUsername to register

Return Value

This method returns the transaction instance with the Username set.

import { UsernameRegistrationBuilder } from '@arkecosystem/typescript-crypto';

const transaction = UsernameRegistrationBuilder.new().username('myusername');

>> UsernameRegistrationBuilder
Crypto / API Documentation / Transactions / UsernameResignationBuilder

Inherits from:AbstractTransactionBuilder

Crypto / API Documentation / Transactions / VoteBuilder

Inherits from:AbstractTransactionBuilder

Set the vote for the transaction

public vote(vote: string): IVoteBuilder

Parameters

TypeNameRequiredDescription
stringvoteYesPublic key of the validator to vote for

Return Value

This method returns the transaction instance with the vote set.

import { VoteBuilder } from '@arkecosystem/typescript-crypto';

const transaction = VoteBuilder.new().vote('0xC3bBE9B1CeE1ff85Ad72b87414B0E9B7F2366763');

>> VoteBuilder
Crypto / API Documentation / Transactions / UnvoteBuilder

Inherits from:AbstractTransactionBuilder

Crypto / API Documentation / Transactions / AbstractTransaction

It is recommended to use the Transaction Builders instead of the AbstractTransaction class directly. You can also access the underlying transaction property of the Builder to work with the AbstractTransaction instance, e.g. TransferBuilder.new().transaction.

Create a new transaction instance

public constructor(data: TransactionData)

Parameters

TypeNameRequiredDescription
TransactionDatadataYesThe transaction data

Return Value

This method initializes a new transaction instance.

import { Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const transaction = new Transfer({...});

>> Transfer

Get the payload of the transaction

public abstract getPayload(): string

Return Value

This method returns the payload of the transaction as a string.

import { AbiEncoder, Transfer, Vote } from '@arkecosystem/typescript-crypto';

const payload = (new Transfer()).getPayload();
>> ''

const encoded = (new AbiEncoder()).encodeFunctionCall('vote', ['0xC3bBE9B1CeE1ff85Ad72b87414B0E9B7F2366763']);
const payload = (new Vote({data: encoded})).getPayload();

>> '0x6dd7d8ea000000000000000000000000c3bbe9b1cee1ff85ad72b87414b0e9b7f2366763'

Sign the transaction using the given private key

public async sign(privateKey: PrivateKey): Promise<ITransaction>

Parameters

TypeNameRequiredDescription
PrivateKeykeysYesPrivate key for signing

Return Value

This method signs the transaction and returns the transaction instance.

import { PrivateKey, Transfer } from '@arkecosystem/typescript-crypto'

// Using Transfer as an example of AbstractTransaction
;(await new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000',
}).sign(PrivateKey.fromPassphrase('this is a top secret passphrase'))) >>
  Transfer

Recover the sender public key and address

public recoverSender(): void

Return Value

This method recovers the sender's public key and address from the transaction signature.

import { PrivateKey, Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const transfer = (await new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000'
}).sign(PrivateKey.fromPassphrase('this is a top secret passphrase')));
transfer.recoverSender();
transfer.toJson();

>> '{..., "to":"0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22"}'

Verify the transaction

public verify(): boolean

Return Value

This method verifies the transaction signature and returns a boolean indicating validity.

import { PrivateKey, Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const transfer = (await new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000'
}).sign(PrivateKey.fromPassphrase('this is a top secret passphrase')));

transfer.verify();

>> true

Serialize the transaction

public serialize(skipSignature: boolean = false): Buffer

Parameters

TypeNameRequiredDescription
booleanskipSignatureNoSerialization options

Return Value

This method serializes the transaction and returns it as a Buffer.

import { Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const serialized = (new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000',
})).serialize();

>> Buffer

Get the hash for the transaction

public hash(skipSignature: boolean = false): Buffer

Parameters

TypeNameRequiredDescription
booleanskipSignatureNoSerialization options

Return Value

This method generates the hash for the transaction and returns it as a string.

import { Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const hash = (new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000',
})).hash();

>> string

Refresh the payload of the transaction

protected refreshPayloadData(): ITransaction

Return Value

This method refreshes the payload data of the transaction and returns the transaction instance.

import { Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const transaction = (new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000',
})).refreshPayloadData();

>> Transfer

Convert the transaction to an object

public toObject(): TransactionData

Return Value

This method returns the transaction data as an array.

import { Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const array = (new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000',
})).toObject();

>> {gasPrice: ..., gas: ...}

Convert the transaction to its JSON representation

public toJson(): string

Return Value

This method returns the JSON representation of the transaction as a string.

import { Transfer } from '@arkecosystem/typescript-crypto';

// Using Transfer as an example of AbstractTransaction
const json = (new Transfer({
  value: '1',
  nonce: 1,
  to: '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  gasPrice: '5000000000',
  gas: '21000',
})).toJson();

>> '{"gasPrice":..., "hash":...}'
Crypto / API Documentation / Transactions / EvmCall

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Multipayment

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Transfer

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / ValidatorRegistration

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / ValidatorResignation

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / UsernameRegistration

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / UsernameResignation

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Vote

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Unvote

Inherits from:AbstractTransaction


Serializer

Crypto / API Documentation / Transactions / Serializer

Create a new serializer instance

constructor(transaction: ITransaction)

Parameters

TypeNameRequiredDescription
ITransactiontransactionYesTransaction to be serialized

Return Value

This method initializes a new serializer instance.

import { TransferBuilder, Serializer } from '@arkecosystem/typescript-crypto';

const transaction = (await TransferBuilder.new()
  .value('0')
  .gasPrice('5000000000')
  .gasLimit('21000')
  .to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  .sign('this is a top secret passphrase')).transaction;

const serializer = new Serializer(transaction);

>> Serializer

Create a new serializer instance

static new(transaction: ITransaction): Serializer

Parameters

TypeNameRequiredDescription
ITransactiontransactionYesTransaction to be serialized

Return Value

This method returns a new serializer instance.

import { TransferBuilder, Serializer } from '@arkecosystem/typescript-crypto';

const transaction = (await TransferBuilder.new()
  .value('0')
  .gasPrice('5000000000')
  .gasLimit('21000')
  .to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  .sign('this is a top secret passphrase'))
  .transaction;

const serializer = Serializer.new(transaction);

>> Serializer

Serialize a transaction

serialize(skipSignature: boolean = false): Buffer

Parameters

TypeNameRequiredDescription
boolskipSignatureNoWhether to skip the signature in serialization.

Return Value

This method serializes the transaction and returns it as a Buffer.

import { TransferBuilder, Serializer } from '@arkecosystem/typescript-crypto';

const transaction = (await TransferBuilder.new()
  .value('0')
  .gasPrice('5000000000')
  .gasLimit('21000')
  .to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  .sign('this is a top secret passphrase'))
  .transaction;

const serialized = (Serializer.new(transaction)).serialize();

>> Buffer

Deserializer

Crypto / API Documentation / Transactions / Deserializer

Create a new deserializer instance

static new(serialized: string): Deserializer

Parameters

TypeNameRequiredDescription
stringserializedYesSerialized transaction data

Return Value

This method returns a new deserializer instance.

import { TransferBuilder, Serializer } from '@arkecosystem/typescript-crypto';

const transaction = (await TransferBuilder.new()
  .value('0')
  .gasPrice('5000000000')
  .gasLimit('21000')
  .to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  .sign('this is a top secret passphrase'))
  .transaction;

const deserializer = Deserializer.new(transaction.serialize().getHex());

>> Deserializer

Deserialize a transaction

deserialize(): ITransaction

Return Value

This method deserializes the transaction and returns an instance of AbstractTransaction.

import { TransferBuilder, Serializer } from '@arkecosystem/typescript-crypto';

const transaction = (await TransferBuilder.new()
  .value('0')
  .gasPrice('5000000000')
  .gasLimit('21000')
  .to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  .sign('this is a top secret passphrase'))
  .transaction;

const transaction = Deserializer.new(transaction.serialize().getHex()).deserialize();

>> Transfer

Decode the payload of a transaction

static decodePayload(data: TransactionData, abiType: ContractAbiType = ContractAbiType.CONSENSUS): AbiResult | null

Parameters

TypeNameRequiredDescription
arraydataYesThe array containing the payload data to decode.
ContractAbiTypeabiTypeNoContract ABI Type to decode against

Return Value

This method decodes the payload data of a transaction and returns it as an array. If decoding fails, it returns `null`.

import { ContractAbiType, Deserializer } from '@arkecosystem/typescript-crypto';

const payloadData = Deserializer.decodePayload(transactionData)

>> {'args': [...], ...}

const payloadData = Deserializer.decodePayload(transactionData, ContractAbiType.USERNAMES)

>> {'args': [...], ...}

Message

Crypto / API Documentation / Utils / Message

Create a new message instance

static new(message: SignedMessage): Message

Parameters

TypeNameRequiredDescription
SignedMessagemessageYesMessage data (object, array, or JSON string)

Return Value

This method returns a new message instance.

import { Message } from '@arkecosystem/typescript-crypto';

const message = Message.new({
  publicKey: '034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
  signature: '0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
  message: 'Hello World',
});

>> Message

Sign a message using the given passphrase

static async sign(message: string, passphrase: string): Promise<Message>

Parameters

TypeNameRequiredDescription
stringmessageYesMessage to sign
stringpassphraseYesPassphrase for signing

Return Value

This method signs the message and returns a new Message instance.

import { Message } from '@arkecosystem/typescript-crypto';

const signedMessage = await Message.sign('Hello World', 'this is a top secret passphrase');

>> Message

Verify the message contents

verify(): boolean

Return Value

This method verifies the message and returns a boolean value indicating validity.

import { Message } from '@arkecosystem/typescript-crypto';

const signedMessage = await Message.sign('Hello World', 'this is a top secret passphrase');
const isValid = signedMessage.verify();

>> true

Convert the message to an array

toArray(): string[]

Return Value

This method converts the message to an array and returns it.

import { Message } from '@arkecosystem/typescript-crypto';

const signedMessage = await Message.sign('Hello World', 'this is a top secret passphrase');
const arrayMessage = signedMessage.toArray();

>> [
  '034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
  '0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
  'Hello World',
]

Convert the message to its string representation

toString(): string

Return Value

This method returns the string representation of the message in JSON format.

import { Message } from '@arkecosystem/typescript-crypto';

const message = new Message({
  publicKey: '034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
  signature: '0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
  message: 'Hello World',
});

message.toString();

>> '{"publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b","message":"Hello World"}'

Convert the message to its JSON representation

toJson(): SignedMessage

Return Value

This method converts the message to a JSON string and returns it.

import { Message } from '@arkecosystem/typescript-crypto';

const signedMessage = await Message.sign('Hello World', 'this is a top secret passphrase');

const jsonMessage = message.toJson();

>> '{"publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b","message":"Hello World"}'

Slot

Crypto / API Documentation / Utils / Slot

Get the time difference between now and network start

static time(): number

Return Value

This method returns the time difference between now and the network start in int format.

import { Slot } from '@arkecosystem/typescript-crypto';

const timeDiff = Slot.time();

>> 200366014

Get the network start epoch

static epoch(): number

Return Value

This method returns the network start epoch in int format.

import { Slot } from '@arkecosystem/typescript-crypto';

const epoch = Slot.epoch();

>> 1490101200

ArgumentDecoder

Crypto / API Documentation / Utils / Abi / ArgumentDecoder

Create a new argument decoder instance

constructor(bytes: string)

Parameters

TypeNameRequiredDescription
stringbytesYesThe encoded bytes to be decoded

Return Value

This method returns a new instance of the ArgumentDecoder class.

import { ArgumentDecoder } from '@arkecosystem/typescript-crypto'

const decoder = new ArgumentDecoder(argument) >> ArgumentDecoder

Decode a string

decodeString(): string

Return Value

This method returns the decoded string.

import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';

const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeString();

>> string

Decode an Address

decodeAddress(): string

Return Value

This method returns the decoded address.

import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';

const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeAddress();

>> string

Decode an unsigned integer

decodeUnsignedInt(): string

Return Value

This method returns an unsigned integer as a string.

import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';

const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeUnsignedInt();

>> string

Decode a signed integer

decodeSignedInt(): string

Return Value

This method returns an signed integer as a string.

import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';

const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeSignedInt();

>> string

Decode a boolean

decodeBool(): boolean

Return Value

This method returns a boolean.

import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';

const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeBool();

>> bool

AbiBase

Crypto / API Documentation / Utils / AbiBase

Create a new instance

constructor(type: ContractAbiType = ContractAbiType.CONSENSUS, customAbi?: string)

Parameters

TypeNameRequiredDescription
ContractAbiTypetypeNoContract ABI Type to decode against
string | undefinedcustomAbiNoCustom ABI to decode

Return Value

This method returns a new instance of the AbiBase class.

import { AbiEncoder } from '@arkecosystem/typescript-crypto';

// Using AbiEncoder as an example of AbstractTransactionBuilder
const encoder = new AbiEncoder();

>> AbiEncoder
Crypto / API Documentation / Utils / AbiDecoder

Inherits from:AbiBase

Create a new instance

decodeFunctionData(data: string): AbiResult

Parameters

TypeNameRequiredDescription
stringdataYesThe data to be decoded

Return Value

This method decodes the function data and returns the result.

import { AbiDecoder } from '@arkecosystem/typescript-crypto';

const decoder = new AbiDecoder();

>> AbiDecoder

const result = decoder.decodeFunctionData('6dd7d8ea000000000000000000000000512f366d524157bcf734546eb29a6d687b762255')

>> AbiResult
Crypto / API Documentation / Utils / AbiEncoder

Inherits from:AbiBase

Create a new instance

encodeFunctionCall(functionName: string, parameters: any[]): string

Parameters

TypeNameRequiredDescription
stringfunctionNameYesThe function name to encode
any[]parametersYesThe parameters to encode

Return Value

This method decodes the function data and returns the result.

import { AbiEncoder } from '@arkecosystem/typescript-crypto';

const encoder = new AbiEncoder();

>> AbiEncoder

const result = encoder.encodeFunctionCall('vote', ['0x512F366D524157BcF734546eB29a6d687B762255'])

>> "0x6dd7d8ea000000000000000000000000512f366d524157bcf734546eb29a6d687b762255"

Helpers

Crypto / API Documentation / Utils / Helpers

Create a new instance

public static isValidUsername(username: string): boolean

Parameters

TypeNameRequiredDescription
stringusernameYesThe username to validate

Return Value

This method returns a boolean value indicating whether the username is valid. Throws Error if the username is invalid.

import { Helpers } from '@arkecosystem/typescript-crypto';

const isValid = Helpers.isValidUsername('validusername');

>> true

Helpers.isValidUsername('# invalid # username #')

>> Error

Create a new instance

public static removeLeadingHexZero(hex: string): string

Parameters

TypeNameRequiredDescription
stringhexYesThe hex string to remove leading zeros from

Return Value

This method returns a boolean value indicating whether the username is valid. Throws Error if the username is invalid.

import { Helpers } from '@arkecosystem/typescript-crypto';

const value = Helpers.removeLeadingHexZero('0x01234');

>> 01234

TransactionUtils

Crypto / API Documentation / Utils / TransactionUtils

Create a new instance

public static toBuffer(transaction: TransactionData, skipSignature: boolean = false): Buffer

Parameters

TypeNameRequiredDescription
TransactionDatatransactionYesThe transaction to convert to a buffer
booleanskipSignatureYesWhether to skip the signature

Return Value

This method returns the transaction as a buffer.

import { TransactionUtils } from '@arkecosystem/typescript-crypto';

const serialized = TransactionUtils.toBuffer(transaction, true);

>> Buffer

Create a new instance

public static toHash(transaction: TransactionData, skipSignature: boolean = false): string

Parameters

TypeNameRequiredDescription
TransactionDatatransactionYesThe transaction to generate a hash for
booleanskipSignatureYesWhether to skip the signature

Return Value

This method returns the hash of a transaction.

import { TransactionUtils } from '@arkecosystem/typescript-crypto';

const serialized = TransactionUtils.toHash(transaction, true);

>> string

UnitConverter

Crypto / API Documentation / Utils / UnitConverter

Parse a value into the appropriate units

static parseUnits(value: number|string, unit: string = 'ark'): BigNumber

Parameters

TypeNameRequiredDescription
number|stringvalueYesThe value to be converted
stringunitNoThe unit to convert to (default is 'ark')

Return Value

This method returns the value converted to the smallest unit as a string.

import { UnitConverter } from '@arkecosystem/typescript-crypto';

const weiValue = UnitConverter.parseUnits(1, 'wei');

>> BigNumber('1')

const gweiValue = UnitConverter.parseUnits(1, 'gwei');

>> BigNumber('1000000000')

const arkValue = UnitConverter.parseUnits(1, 'ark');

>> BigNumber('1000000000000000000')

const arkDecimalValue = UnitConverter.parseUnits(0.1, 'ark');

>> BigNumber('100000000000000000')

const exponentValue = UnitConverter.parseUnits(100000, 'ark').toString();

>> '1e+23'

const stringValue = UnitConverter.parseUnits(100000, 'ark').toFixed();

>> '1000000000000000000'

Format a value from smaller units to a larger unit

static formatUnits(value: string, unit: string = 'ark'): number

Parameters

TypeNameRequiredDescription
stringvalueYesThe value in the smallest unit (as a string)
stringunitNoThe unit to format to (default is 'ark')

Return Value

This method returns the value converted from the smallest unit to the specified unit as a float.

import { UnitConverter } from '@arkecosystem/typescript-crypto';

const formattedWei = UnitConverter.formatUnits('1', 'wei');

>> 1.0

const formattedGwei = UnitConverter.formatUnits('1000000000', 'gwei');

>> 1.0

const formattedArk = UnitConverter.formatUnits('1000000000000000000', 'ark');

>> 1.0

const formattedArkDecimal = UnitConverter.formatUnits('100000000000000000', 'ark');

>> 0.1

Format a wei value to an ark value

static weiToArk(value: number|string, suffix?: string): string

Parameters

TypeNameRequiredDescription
number|stringvalueYesThe wei value (as a string, int or float)
stringsuffixNoThe suffix to show after the formatted value (default is undefined)

Return Value

This method returns the wei value formatted as an ARK value.

import { UnitConverter } from '@arkecosystem/typescript-crypto';

const formattedValue = UnitConverter.weiToArk(1, 'DARK');

>> 0.000000000000000001 DARK

const formattedValue = UnitConverter.weiToArk(1);

>> 0.000000000000000001

const formattedValue = UnitConverter.weiToArk('1000000000000000000', 'DARK');

>> 1 DARK

const formattedValue = UnitConverter.weiToArk('1000000000000000000');

>> 1

Format a gwei value to an ark value

static gweiToArk(value: number|string, suffix?: string): string

Parameters

TypeNameRequiredDescription
string | int | floatvalueYesThe gwei value (as a string, int or float)
stringsuffixNoThe suffix to show after the formatted value (default is null)

Return Value

This method returns the gwei value formatted as an ARK value.

import { UnitConverter } from '@arkecosystem/typescript-crypto';

formattedValue = UnitConverter.gweiToArk(1, 'DARK');

>> 0.000000001 DARK

formattedValue = UnitConverter.gweiToArk(1);

>> 0.000000001

formattedValue = UnitConverter.gweiToArk('1000000000', 'DARK');

>> 1 DARK

formattedValue = UnitConverter.gweiToArk('1000000000');

>> 1