meterify.eth.accounts

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().
    const accounts = new Accounts('ws://wstest.meter.io', null, options);

create

meterify.eth.accounts.create([entropy]);

Generates an account object with private key and public key.

Examples:

meterify.eth.accounts.create();
    > {
        address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
        privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
        signTransaction: function(tx){...},
        sign: function(data){...},
        encrypt: function(password){...}
    }

    meterify.eth.accounts.create('2435@#@#@±±±±!!!!678543213456764321§34567543213456785432134567');
    > {
        address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
        privateKey: "0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098",
        signTransaction: function(tx){...},
        sign: function(data){...},
        encrypt: function(password){...}
    }

    meterify.eth.accounts.create(web3.utils.randomHex(32));
    > {
        address: "0xe78150FaCD36E8EB00291e251424a0515AA1FF05",
        privateKey: "0xcc505ee6067fba3f6fc2050643379e190e087aeffe5d958ab9f2f3ed3800fa4e",
        signTransaction: function(tx){...},
        sign: function(data){...},
        encrypt: function(password){...}
    }

Parameters:

  1. 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:

  1. address - String: The account address.

  2. 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.

  3. signTransaction(tx [, callback]) - Function: The function to sign transactions. See meterify.eth.accounts.signTransaction() for more.

  4. sign(data) - Function: The function to sign transactions. See meterify.eth.accounts.sign() for more.

privateKeyToAccount

Creates an account object from a private key.

Parameters

Parameters:

  1. privateKey - String: The private key hex string beginning with 0x.

Returns:

Object - The account object with the structure seen here.

signTransaction

Signs a Meter transaction with a given private key.

Parameters

Parameters

Parameters

  1. tx - Object: The transaction's properties object as follows:

    1. nonce - String: (optional) The nonce to use when signing this transaction. Default will use meterify.eth.getTransactionCount().

    2. chainId - String: (optional) The chain id to use when signing this transaction. Default will use meterify.eth.net.getChainId().

    3. to - String: (optional) The receiver of the transaction, can be empty when deploying a contract.

    4. data - String: (optional) The call data of the transaction, can be empty for simple value transfers.

    5. value - String: (optional) The value of the transaction in wei.

    6. gasPrice - String: (optional) The gas price set by this transaction. If empty, it will use meterify.eth.getGasPrice()

    7. gas - String: The gas provided by the transaction.

  2. privateKey - String: The private key to sign with.

  3. callback - Function: (optional) Optional callback, returns an error object as first parameter and the result as second.

Returns

Promise returning Object: The signed data RLP encoded transaction, or if returnSignature is true the signature values as follows:

  1. messageHash - String: The hash of the given message.

  2. r - String: First 32 bytes of the signature

  3. s - String: Next 32 bytes of the signature

  4. v - String: Recovery value + 27

  5. rawTransaction - String: The RLP encoded transaction, ready to be send using meterify.eth.sendSignedTransaction.

  6. transactionHash - String: The transaction hash for the RLP encoded transaction.

recoverTransaction

Recovers the Meter address which was used to sign the given RLP encoded transaction.

Example:

Parameters:

  1. signature - String: The RLP encoded transaction.

Returns:

String: The Meter address used to sign this transaction.

hashMessage

Hashes the given message to be passed meterify.eth.accounts.recover() function.

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:

  1. 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:

  1. data - String: The data to sign.

  2. 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:

  1. message - String: The the given message.

  2. messageHash - String: The hash of the given message.

  3. r - String: First 32 bytes of the signature.

  4. s - String: Next 32 bytes of the signature.

  5. v - String: Recovery value + 27.

recover

Recovers the Meter address which was used to sign the given data.

Parameters

Parameters:

  1. message or signatureObject - String or Object: Either signed message or hash, or the signature object as following values:

    1. messageHash - String: The hash of the given message already prefixed with "\x19Ethereum Signed Message:\n" + message.length + message.

    2. r - String: First 32 bytes of the signature.

    3. s - String: Next 32 bytes of the signature.

    4. v - String: Recovery value + 27

  2. signature - String: The raw RLP encoded signature, OR parameter 2-4 as v, r, s values.

  3. 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:

  1. privateKey - String: The private key to encrypt.password

  2. 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:

  1. keystoreJsonV3 - String: The encrypted keystore v3 JSON.

  2. 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:

  1. numberOfAccounts - Number: Number of accounts to create. Leave empty to create an empty wallet.

  2. 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.

Example:

Parameters:

  1. account - String or Object: A private key or account object created with meterify.eth.accounts.create().

Returns:

Object: The added account.

wallet.remove

Removes an account from the wallet.

Example:

Parameters:

  1. account - String or Number: The account address, or index in the wallet.

Returns:

Boolean: true if the wallet was removed. false if it couldn't be found.

wallet.clear

Securely empties the wallet and removes all its accounts.

Example:

Parameters:

none

Returns:

Object: The wallet object.

wallet.encrypt

Encrypts all wallet accounts to an array of encrypted keystore v3 objects.

Example:

Parameters:

  1. password - String: The password which will be used for encryption.

Returns:

Array: The encrypted keystore v3.

wallet.decrypt

Decrypts keystore v3 objects.

Example:

Parameters:

  1. keystoreArray - Array: The encrypted keystore v3 objects to decrypt.

  2. password - String: The password which will be used for encryption.

Returns:

Object: The wallet object.

wallet.save

Stores the wallet encrypted and as string in local storage.

Example:

Note: Browser only.

Parameters:

  1. password - String: The password to encrypt the wallet.

  2. keyName - String: (optional) The key used for the local storage position, defaults to "web3js_wallet".

Returns:

Boolean

wallet.load

Loads a wallet from local storage and decrypts it.

Example:

Note: Browser only.

Parameters:

  1. password - String: The password to decrypt the wallet.

  2. keyName - String: (optional) The key used for the local storage position, defaults to "web3js_wallet".

Returns:

Object: The wallet object.

Last updated