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:
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 usingrandomhex.
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 tonullthe memory after usage.signTransaction(tx [, callback])-Function: The function to sign transactions. Seemeterify.eth.accounts.signTransaction()for more.sign(data)-Function: The function to sign transactions. Seemeterify.eth.accounts.sign()for more.
privateKeyToAccount
Creates an account object from a private key.
Parameters
Parameters:
privateKey-String: The private key hex string beginning with0x.
Returns:
Object - The account object with the structure seen here.
signTransaction
Signs a Meter transaction with a given private key.
Parameters
Parameters
Parameters
tx-Object: The transaction's properties object as follows:nonce-String: (optional) The nonce to use when signing this transaction. Default will usemeterify.eth.getTransactionCount().chainId-String: (optional) The chain id to use when signing this transaction. Default will usemeterify.eth.net.getChainId().to-String: (optional) The receiver of the transaction, can be empty when deploying a contract.data-String: (optional) The call data of the transaction, can be empty for simple value transfers.value-String: (optional) The value of the transaction in wei.gasPrice-String: (optional) The gas price set by this transaction. If empty, it will usemeterify.eth.getGasPrice()gas-String: The gas provided by the transaction.
privateKey-String: The private key to sign with.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:
messageHash-String: The hash of the given message.r-String: First 32 bytes of the signatures-String: Next 32 bytes of the signaturev-String: Recovery value + 27rawTransaction-String: The RLP encoded transaction, ready to be send usingmeterify.eth.sendSignedTransaction.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:
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:
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:
messageorsignatureObject-StringorObject: 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 istrue, 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.passwordString: 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.
Example:
Parameters:
account-StringorObject: A private key or account object created withmeterify.eth.accounts.create().
Returns:
Object: The added account.
wallet.remove
Removes an account from the wallet.
Example:
Parameters:
account-StringorNumber: 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:
password-String: The password which will be used for encryption.
Returns:
Array: The encrypted keystore v3.
wallet.decrypt
Decrypts keystore v3 objects.
Example:
Parameters:
keystoreArray-Array: The encrypted keystore v3 objects to decrypt.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:
password-String: The password to encrypt the wallet.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:
password-String: The password to decrypt the wallet.keyName-String: (optional) The key used for the local storage position, defaults to"web3js_wallet".
Returns:
Object: The wallet object.
Last updated
Was this helpful?