The meterify.eth.accounts contains functions to generate Meter accounts and sign transactions and data.
Note: Take precautions to clear memory properly, store the private keys safely, and test transaction receiving and sending functionality properly before using in production!
import{Accounts}from'web3-eth-accounts'; // Passing in the eth or web3 package is necessary to allow retrieving chainId, gasPrice and nonce automatically // for accounts.signTransaction().constaccounts=newAccounts('ws://wstest.meter.io',null,options);
create
meterify.eth.accounts.create([entropy]);
Generates an account object with private key and public key.
entropy - String(optional): A random string to increase entropy. If given it should be at least 32 characters. If none is given a random string will be generated using randomhex.
Returns:
Object - The account object with the following structure:
address - String: The account address.
privateKey - String: The accounts private key. This should never be shared or stored unencrypted in local storage! Also make sure to null the memory after usage.
The data will be UTF-8 HEX decoded and enveloped as follows: "\x19Ethereum Signed Message:\n" + message.length + message and hashed using keccak256.
Example:
Parameters:
message - String: A message to hash, if it's HEX it will be UTF8 decoded before.
Returns:
String: The hashed message
sign
Signs arbitrary data. This data is before UTF-8 HEX decoded and enveloped as follows: "\x19Ethereum Signed Message:\n" + message.length + message.
Parameters
Parameters:
data - String: The data to sign.
privateKey - String: The private key to sign with.
Returns:
Object: The signed data RLP encoded signature, or if returnSignature is true the signature values as follows:
message - String: The the given message.
messageHash - String: The hash of the given message.
r - String: First 32 bytes of the signature.
s - String: Next 32 bytes of the signature.
v - String: Recovery value + 27.
recover
Recovers the Meter address which was used to sign the given data.
Parameters
Parameters:
message or signatureObject - String or Object: Either signed message or hash, or the signature object as following values:
messageHash - String: The hash of the given message already prefixed with "\x19Ethereum Signed Message:\n" + message.length + message.
r - String: First 32 bytes of the signature.
s - String: Next 32 bytes of the signature.
v - String: Recovery value + 27
signature - String: The raw RLP encoded signature, OR parameter 2-4 as v, r, s values.
preFixed - Boolean (optional, default: false): If the last parameter is true, the given message will NOT automatically be prefixed with "\x19Ethereum Signed Message:\n" + message.length + message, and assumed to be already prefixed.
Returns:
String: The Meter address used to sign this data.
encrypt
Encrypts a private key to the web3 keystore v3 standard.
Example:
Parameters:
privateKey - String: The private key to encrypt.password
String: The password used for encryption.
Returns:
Object: The encrypted keystore v3 JSON.
decrypt
Decrypts a keystore v3 JSON, and creates the account.
Example:
Parameters:
keystoreJsonV3 - String: The encrypted keystore v3 JSON.
password - String: The password used for encryption.
Returns:
Object: The decrypted account.
wallet
Contains an in memory wallet with multiple accounts. These accounts can be used when using meterify.eth.sendTransaction().
Example:
wallet.create
Generates one or more accounts in the wallet. If wallets already exist they will not be overridden.
Example:
Parameters:
numberOfAccounts - Number: Number of accounts to create. Leave empty to create an empty wallet.
entropy - String(optional): A string with random characters as additional entropy when generating accounts. If given it should be at least 32 characters.
Returns:
Object: The wallet object.
wallet.add
Adds an account using a private key or account object to the wallet.
meterify.eth.accounts.hashMessage("Hello World")
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"
// the below results in the same hash
meterify.eth.accounts.hashMessage(web3.utils.utf8ToHex("Hello World"))
> "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"