Examples

Initialization

1const { Identities } = require("@arkecosystem/crypto");
2 
3// Throughout this document, the keys object used is:
4const keys = Identities.Keys.fromPassphrase("this is a top-secret passphrase");
5 
6// Throughout this document, the recipientId variable used is:
7const recipientId = Identities.Address.fromPassphrase("this is a top-secret passphrase");
8 
9// Throughout this document, the senderPublicKey variable used is:
10const senderPublicKey = Identities.PublicKey.fromPassphrase("this is a top-secret passphrase");

Transactions

A transaction is an object specifying the transfer of funds from the sender’s wallet to the recipient’s. Each transaction must be signed by the sender’s private key to prove authenticity and origin. After broadcasting through the client SDK, a transaction is permanently incorporated in the blockchain by a Delegate Node.

Sign

The crypto SDK can sign a transaction using your private key or passphrase (from which the private key is generated). Ensure you are familiar with digital signatures before using the crypto SDKs.

1const { Transactions } = require("@arkecosystem/crypto");
2 
3const transaction = {
4 type: 0,
5 amount: 1000,
6 fee: 2000,
7 recipientId,
8 timestamp: 121212,
9 asset: {},
10 senderPublicKey
11};
12 
13Transactions.Signer.sign(transaction, keys);
14 
15>>> string

Serialize (AIP11)

Serialization of a transaction object ensures it is compact and properly formatted to be incorporated in the ARK blockchain. If you are using the crypto SDK in combination with the public API SDK, you should not need to serialize manually.

1const { Transactions } = require("@arkecosystem/crypto");
2 
3const transaction = Transactions.BuilderFactory
4 .transfer()
5 .amount(1000)
6 .fee(2000)
7 .recipientId(recipientId)
8 .senderPublicKey(senderPublicKey)
9 .sign("sender")
10 .build();
11 
12const serialized = Transactions.Serializer.serialize(transaction).toString("hex");
13 
14>>> string

Deserialize (AIP11)

A serialized transaction may be deserialized for inspection purposes. The public API does not return serialized transactions, so you should only need to deserialize in exceptional circumstances.

1const { Transactions } = require("@arkecosystem/crypto");
2const deserialized = Transactions.deserializer.deserialize(serialized);
3 
4>>> ITransaction

Message

The crypto SDK not only supports transactions but can also work with other arbitrary data (expressed as strings).

Sign

Signing a string works much like signing a transaction: in most implementations, the message is hashed, and the resulting hash is signed using the private key or passphrase.

ECDSA

1const { Crypto } = require("@arkecosystem/crypto");
2 
3const message = "Arbitrary entry of data";
4const hash = Crypto.HashAlgorithms.sha256(message);
5const signature = Crypto.Hash.signECDSA(hash, keys);
6 
7const signed = {
8 message,
9 hash,
10 signature
11};
12 
13>>> IMessage

Schnorr

1const { Crypto } = require("@arkecosystem/crypto");
2 
3const message = "Arbitrary entry of data";
4const hash = Crypto.HashAlgorithms.sha256(message);
5const signature = Crypto.Hash.signSchnorr(hash, keys);
6 
7const signed = {
8 message,
9 hash,
10 signature
11};
12 
13>>> IMessage

Verify

A message’s signature can easily be verified by hash, without the private key that signed the message, by using the verify method.

ECDSA

1Crypto.Hash.verifyECDSA(
2 signed.hash,
3 signed.signature,
4 "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b"
5);
6 
7>>> boolean

Schnorr

1Crypto.Hash.verifySchnorr(
2 signed.hash,
3 signed.signature,
4 "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b"
5);
6 
7>>> boolean

Identities

The identities class allows for the creation and inspection of keyPairs from passphrases. Here you find vital functions when creating transactions and managing wallets.

Derive the Address from a Passphrase

1const { Identities } = require("@arkecosystem/crypto");
2Identities.Address.fromPassphrase("this is a top secret passphrase");
3 
4>>> string

Derive the Address from a Public Key

1const { Identities } = require("@arkecosystem/crypto");
2Identities.Address.fromPublicKey(
3 "validPublicKey"
4);
5 
6>>> string

Derive the Address from a Private Key

1const { Identities } = require("@arkecosystem/crypto");
2Identities.Address.fromPrivateKey(
3 "validPrivateKey"
4);
5 
6>>> string

Derive the Address from a WIF

1const { Identities } = require("@arkecosystem/crypto");
2Identities.Address.fromWIF(
3 "validWif"
4);
5 
6>>> string

Validate an Address

1const { Identities } = require("@arkecosystem/crypto");
2Identities.Address.validate("validAddress");
3 
4>>> boolean

Private Key

As the name implies, private keys and passphrases are to remain private. Never store these unencrypted and minimize access to these secrets

Derive the Private Key from a Passphrase

1const { Identities } = require("@arkecosystem/crypto");
2Identities.PrivateKey.fromPassphrase("this is a top secret passphrase");
3 
4>>> string

Derive the Private Key Instance Object from a Hexadecimal Encoded String

1This function has not been implemented in this client library.

Derive the Private Key from a WIF

1const { Identities } = require("@arkecosystem/crypto");
2Identities.PrivateKey.fromWIF(
3 "validWif"
4);
5 
6>>> string

Public Key

Public Keys may be freely shared, and are included in transaction objects to validate the authenticity.

Derive the Public Key from a Passphrase

1const { Identities } = require("@arkecosystem/crypto");
2Identities.PublicKey.fromPassphrase("this is a top secret passphrase");
3 
4>>> string

Derive the Public Key Instance Object from a Hexadecimal Encoded String

1This function has not been implemented in this client library.

Validate a Public Key

1const { Identities } = require("@arkecosystem/crypto");
2Identities.PublicKey.validate(
3 "validPublicKey"
4);
5 
6>>> boolean

WIF

The WIF should remain secret, just like your passphrase and private key.

Derive the WIF from a Passphrase

1const { Identities } = require("@arkecosystem/crypto");
2Identities.WIF.fromPassphrase("this is a top secret passphrase");
3 
4>>> string
Last updated 11 months ago
Edit Page
Share:
2022 © ARK.io | All Rights Reserved
An ARK.io Product