API Documentation
Identity
Type | Name | Required | Description |
---|---|---|---|
string | passphrase | Yes | Passphrase |
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'
Type | Name | Required | Description |
---|---|---|---|
string | publicKey | Yes | Public 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'
Type | Name | Required | Description |
---|---|---|---|
EccPrivateKey | privateKey | Yes | Private 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'
Type | Name | Required | Description |
---|---|---|---|
string | address | Yes | Address 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
Derive a private key from a passphrase
static fromPassphrase(passphrase: string): PrivateKey
Parameters
Type | Name | Required | Description |
---|---|---|---|
string | passphrase | Yes | Passphrase |
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
Type | Name | Required | Description |
---|---|---|---|
string | privateKey | Yes | Hexadecimal 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
Type | Name | Required | Description |
---|---|---|---|
string | wif | Yes | Hexadecimal 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
Type | Name | Required | Description |
---|---|---|---|
string | message | Yes | Message 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
Derive a public key from a passphrase
static fromPassphrase(passphrase: string): PublicKey
Parameters
Type | Name | Required | Description |
---|---|---|---|
string | passphrase | Yes | Passphrase |
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
Type | Name | Required | Description |
---|---|---|---|
string | publicKey | Yes | Public 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
Type | Name | Required | Description |
---|---|---|---|
string | message | Yes | Message to use to recover from in Buffer format |
bigint | r | Yes | Signature r in bigint format |
bigint | s | Yes | Signature s in bigint format |
number | v | Yes | Signature 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
Type | Name | Required | Description |
---|---|---|---|
string | passphrase | Yes | Passphrase |
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
Set the network you want to use in the crypto library
public static set(network: AbstractNetwork): void
Parameters
Type | Name | Required | Description |
---|---|---|---|
AbstractNetwork | network | Yes | Network 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
This function returns the current network.
import { Network } from '@arkecosystem/typescript-crypto';
const network = Network.get();
>> AbstractNetwork
This method returns the chain ID as an integer.
import { Testnet } from '@arkecosystem/typescript-crypto';
new Testnet().chainId();
>> 10000
This method returns the epoch time in string format.
import { Testnet } from '@arkecosystem/typescript-crypto';
(new Testnet()).epoch();
>> '2017-03-21T13:00:00.000Z'
This method returns the WIF in hexadecimal string format.
import { Testnet } from '@arkecosystem/typescript-crypto';
new Testnet().wif();
>> 'ba'
Transactions
For full examples of using the Builders to create transactions, please have a look at the Examples page.
Type | Name | Required | Description |
---|---|---|---|
Record<string, any> | undefined | data | No | Optional 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
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":""}'
Type | Name | Required | Description |
---|---|---|---|
string | BigNumber | gas | Yes | Gas 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
Type | Name | Required | Description |
---|---|---|---|
string | to | Yes | Recipient 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
Type | Name | Required | Description |
---|---|---|---|
string | BigNumber | gasPrice | Yes | Gas 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
Type | Name | Required | Description |
---|---|---|---|
string | nonce | Yes | Nonce 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
Type | Name | Required | Description |
---|---|---|---|
int | network | Yes | Network 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
Type | Name | Required | Description |
---|---|---|---|
string | passphrase | Yes | Passphrase 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
Type | Name | Required | Description |
---|---|---|---|
string | passphrase | Yes | Passphrase for signing the transaction |
string | secondPassphrase | Yes | Second 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
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
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": "",
}
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": ""}'
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
string | payload | Yes | EVM 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
Inherits from:AbstractTransactionBuilder
Add recipient to the transaction
public pay(address: string, value: string | BigNumber): IMultipaymentBuilder
Parameters
Type | Name | Required | Description |
---|---|---|---|
string | address | Yes | Address to recipient |
string | BigNumber | value | Yes | Amount 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
Inherits from:AbstractTransactionBuilder
Set the value to transfer (in wei)
public value(value: string | BigNumber): ITransferBuilder
Parameters
Type | Name | Required | Description |
---|---|---|---|
string | BigNumber | value | Yes | Amount 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
Inherits from:AbstractTransactionBuilder
Set the validator BLS public key
public validatorPublicKey(validatorPublicKey: string): IValidatorRegistrationBuilder
Parameters
Type | Name | Required | Description |
---|---|---|---|
string | validatorPublicKey | Yes | Validator 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
Type | Name | Required | Description |
---|---|---|---|
string | BigNumber | value | Yes | Amount 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
Inherits from:AbstractTransactionBuilder
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
string | username | Yes | Username 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
Inherits from:AbstractTransactionBuilder
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
string | vote | Yes | Public 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
Inherits from:AbstractTransactionBuilder
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
.
Type | Name | Required | Description |
---|---|---|---|
TransactionData | data | Yes | The 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
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
Type | Name | Required | Description |
---|---|---|---|
PrivateKey | keys | Yes | Private 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
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"}'
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
Type | Name | Required | Description |
---|---|---|---|
boolean | skipSignature | No | Serialization 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
Type | Name | Required | Description |
---|---|---|---|
boolean | skipSignature | No | Serialization 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
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
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: ...}
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":...}'
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Serializer
Type | Name | Required | Description |
---|---|---|---|
ITransaction | transaction | Yes | Transaction 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
Type | Name | Required | Description |
---|---|---|---|
ITransaction | transaction | Yes | Transaction 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
Type | Name | Required | Description |
---|---|---|---|
bool | skipSignature | No | Whether 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
Type | Name | Required | Description |
---|---|---|---|
string | serialized | Yes | Serialized 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
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
Type | Name | Required | Description |
---|---|---|---|
array | data | Yes | The array containing the payload data to decode. |
ContractAbiType | abiType | No | Contract 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
Type | Name | Required | Description |
---|---|---|---|
SignedMessage | message | Yes | Message 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
Type | Name | Required | Description |
---|---|---|---|
string | message | Yes | Message to sign |
string | passphrase | Yes | Passphrase 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
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
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',
]
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"}'
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
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
This method returns the network start epoch in int format.
import { Slot } from '@arkecosystem/typescript-crypto';
const epoch = Slot.epoch();
>> 1490101200
ArgumentDecoder
Type | Name | Required | Description |
---|---|---|---|
string | bytes | Yes | The 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
This method returns the decoded string.
import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';
const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeString();
>> string
This method returns the decoded address.
import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';
const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeAddress();
>> string
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
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
This method returns a boolean.
import { ArgumentDecoder } from '@arkecosystem/typescript-crypto';
const decoder = new ArgumentDecoder(argument)
const value = decoder.decodeBool();
>> bool
AbiBase
Create a new instance
constructor(type: ContractAbiType = ContractAbiType.CONSENSUS, customAbi?: string)
Parameters
Type | Name | Required | Description |
---|---|---|---|
ContractAbiType | type | No | Contract ABI Type to decode against |
string | undefined | customAbi | No | Custom 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
Inherits from:AbiBase
Type | Name | Required | Description |
---|---|---|---|
string | data | Yes | The 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
Inherits from:AbiBase
Type | Name | Required | Description |
---|---|---|---|
string | functionName | Yes | The function name to encode |
any[] | parameters | Yes | The 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
Type | Name | Required | Description |
---|---|---|---|
string | username | Yes | The 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
Type | Name | Required | Description |
---|---|---|---|
string | hex | Yes | The 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
Create a new instance
public static toBuffer(transaction: TransactionData, skipSignature: boolean = false): Buffer
Parameters
Type | Name | Required | Description |
---|---|---|---|
TransactionData | transaction | Yes | The transaction to convert to a buffer |
boolean | skipSignature | Yes | Whether 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
Type | Name | Required | Description |
---|---|---|---|
TransactionData | transaction | Yes | The transaction to generate a hash for |
boolean | skipSignature | Yes | Whether 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
Parse a value into the appropriate units
static parseUnits(value: number|string, unit: string = 'ark'): BigNumber
Parameters
Type | Name | Required | Description |
---|---|---|---|
number|string | value | Yes | The value to be converted |
string | unit | No | The 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
Type | Name | Required | Description |
---|---|---|---|
string | value | Yes | The value in the smallest unit (as a string) |
string | unit | No | The 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
Type | Name | Required | Description |
---|---|---|---|
number|string | value | Yes | The wei value (as a string, int or float) |
string | suffix | No | The 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
Type | Name | Required | Description |
---|---|---|---|
string | int | float | value | Yes | The gwei value (as a string, int or float) |
string | suffix | No | The 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