datona-crypto

The datona-crypto component provides all the datona-lib cryptographic classes and functions.

Class Key

Encapsulates a private key and provides cryptographic functions that use it. The Key is a core class of datona-lib.

Properties

  • privateKey (PrivateKey) - the private key given to the constructor
  • publicKey (PublicKey) - the public key derived from the private key
  • address (Address) - the public blockchain address derived from the private key

Constructor

Constructs the instance with the given private key.

new Key(privateKey);

Parameters

  1. privateKey (PrivateKey) - 32-byte private key in hex (64 hex characters)

Example

const myKey = new Key("e68e40257cfee330038c49637fcffff82fae04b9c563f4ea071c20f2eb55063c");
console.log(myKey.address);
> 0x41A60F71063CD7c9e5247d3E7d551f91f94b5C3b

sign

Signs the given hash with this key.

sign(hash);

Parameters

  1. hash (Hash) - 32-byte hash to sign in hex (64 hex characters)

Returns

DatonaSignature - the signature of the given hash derived from this key

Example

const signature = myKey.sign(hash("Hello World!"));

encrypt

Encrypts the given data using the Elliptic Curve Integrated Encryption Scheme. The symmetric encryption key is generated from this private key and the given public key. The resulting encrypted data can be decrypted with this public key and the private part of the given public key.

The key derivation function used is the standard datona crypto hash function. The encryption scheme used is AES-GCM. ECIES has been selected instead of an asymmetric scheme like RSA for performance reasons.

encrypt(publicKeyTo, data);

Parameters

  1. publicKeyTo (address) public part of the remote key that will be used to decrypt this data
  2. data (bytes) data to encrypt (e.g. as a string)

Returns

bytes - the encrypted data

Example

const encryptedData = myKey.encrypt(theirPublicKey, "Hello World"))

decrypt

Decrypts the given data that has been encrypted with the encrypt function. The given public key must be the public part of the private key used to encrypt the data and this key must be the private part of the public key used to encrypt the data.

decrypt(publicKeyFrom, data);

Parameters

  1. publicKeyFrom (address) public part of the remote key that was used to encrypt this data
  2. data (bytes) the encrypted data

Returns

bytes - the decrypted data

Example

const key1 = new Key("e68e40257cfee330038c49637fcffff82fae04b9c563f4ea071c20f2eb55063c");
const key2 = new Key("b692ef5519cd87854b9bd97dd47a8929cbe473fe7a0da53e4ec79efec540cd2b");
const encryptedData = key1.encrypt(key2.publicKey, "Hello World"));
const decryptedData = key2.decrypt(key1.publicKey, encryptedData));
assert(decryptedData == "Hello World");

Functions

generateKey

Generates a new Key object with a random private key. NB: This function does not use a true random source. Use only for experimental and test purposes.

generateKey();

Returns

Key - a new Key object with a random private key.

Example

const myPrivateKey = datona.crypto.generateKey();

sign

Signs the given hash using the given private key.

sign(hash, privateKey);

Parameters

  1. hash (Hash) - 32-byte hash to sign in hex (64 hex characters)
  2. privateKey (PrivateKey) - 32-byte private key in hex (64 hex characters)

Returns

DatonaSignature - the signature of the given hash derived from the given key

Example

const myPrivateKey = "e68e40257cfee330038c49637fcffff82fae04b9c563f4ea071c20f2eb55063c";
const signature = sign(hash("Hello World!"), myPrivateKey);

verify

Verifies that the signatory of the given hash and signature matches the given address

verify(hash, signature, address);

Parameters

  1. hash (Hash) - 32-byte hash to sign in hex (64 hex characters)
  2. signature (DatonaSignature) - 65-byte DatonaSignature in hex (130 hex characters)
  3. address (Address) - expected signatory address to verify against

Returns

bool - true if signatory matches the given address

Throws

  • InvalidHashError if the hash is invalid
  • InvalidSignatureError if the signatory could not be recovered

Example

const myKey = new Key("e68e40257cfee330038c49637fcffff82fae04b9c563f4ea071c20f2eb55063c");
const myHash = hash("Hello World!");
const signature = myKey.sign(myHash);
const matches = verify(myHash, signature, myKey.address);

console.log(matches);
> true

recover

Recovers the address of the signatory of the given hash and signature

recover(hash, signature);

Parameters

  1. hash (Hash) - 32-byte hash to sign in hex (64 hex characters)
  2. signature (DatonaSignature) - 65-byte DatonaSignature in hex (130 hex characters)

Returns

Address - address of the signatory (with leading 0x)

Throws

  • InvalidHashError if the hash is invalid
  • InvalidSignatureError if the signatory could not be recovered

Example

const myKey = new Key("e68e40257cfee330038c49637fcffff82fae04b9c563f4ea071c20f2eb55063c");
const myHash = hash("Hello World!");
const signature = myKey.sign(myHash);
const address = recover(myHash, signature);

console.log(address);
> 0x41A60F71063CD7c9e5247d3E7d551f91f94b5C3b

console.log(myKey.address == address);
> true

hash

Generates a keccak256 hash of the given data string

hash(data);

Parameters

  1. data (Buffer) - the data to be hashed

Returns

Hash - hash of the given data as a 32-byte hex string (64 hex characters)

Example

const myHash = hash("Hello World!");

console.log(myHash);
> 3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0

calculateContractAddress

Generates a contract address

calculateContractAddress(ownerAddress, nonce);

Parameters

  1. ownerAddress (Address) - the blockchain address of the deployer
  2. nonce (uint) - the owner’s next transaction nonce

Returns

Address - blockchain address of the contract

Example

const contractAddress = calculateContractAddress(myKey.address, 1);