A collection of utility functions for AI Network (AIN). The methods and tests have been modified from ethereumjs-util and ethjs-util.
npm install @ainblockchain/ain-util
const ainUtil = require('@ainblockchain/ain-util');
const message = { foo: 'bar' };
const privateKey = YOUR_OWN_PRIVATE_KEY;
// Alice generates signature using private key.
const signature = ainUtil.ecSignMessage(JSON.stringify(message), Buffer.from(privateKey, 'hex'));
// Bob verifies the message
const hash = ainUtil.hashMessage(JSON.stringify(message));
const sigBuffer = ainUtil.toBuffer(signature);
const len = sigBuffer.length;
const lenHash = len - 65;
const { r, s, v } = ainUtil.ecSplitSig(sigBuffer.slice(lenHash, len));
const publicKey = ainUtil.ecRecoverPub(Buffer.from(hash, 'hex'), r, s, v);
const address = ainUtil.toChecksumAddress(ainUtil.bufferToHex(ainUtil.pubToAddress(publicKey, publicKey.length === 65)));
const isVerified = ainUtil.ecVerifySig(JSON.stringify(message), signature, address);
// console.log(isVerified);
// will return
// ADDRESS
// true
Adds "0x" to a given string
if it does not already start with "0x".
Converts a Buffer
into a hex string
.
Signs a message with a private key and returns a string
signature.
ECDSA public key recovery from signature.
Converts signature format of the eth_sign
RPC method to signature parameters.
Checks if the signature is valid.
Signs a transaction body with a private key and returns a string
signature.
Generates keccak hash using a transaction body.
Returns the bitcoin's varint encoding of keccak-256 hash of message
,
prefixed with the header 'AINetwork Signed Message:\n'.
Checks whether the str
is prefixed with "0x".
Checks whether the privateKey
is a valid private key (follows the rules of the curve secp256k1).
Checks whether the publicKey
is a valid public key (follows the rules of the
curve secp256k1 and meets the AIN requirements).
Checks if the address is valid.
Checks if the two addresses are the same.
Creates Keccak hash of the input.
Returns the public key of a given private key.
Returns the AI Network address of a given public key.
Returns an Account with the given private key.
Serializes an object (e.g. tx data) using rlp encoding.
Pads and message
with leading zeros till it has length
bytes.
Truncates from the beginning if message
is longer than length
.
Removes '0x' from a given String
is present.
Attempts to turn a value into a Buffer
. As input it supports Buffer
,
String
, Number
, null/undefined, BN
and other objects with a toArray()
method.
Returns a checksummed address.
Encrypts message with publicKey.
Decrypts encrypted data with privateKey.
Creates an account with a given entropy.
privateToV3Keystore(privateKey: Buffer, password: string, options: V3KeystoreOptions = {}) -> V3Keystore
Converts an account into a V3 Keystore and encrypts it with a password.
Returns a private key from a V3 Keystore.
Tested on the below environments.
- v12.x.x
- v10.x.x
- v6.x.x
- v7.x.x
MPL-2.0