@sec-block/secjs-account
v1.0.8
Published
This library eases the handling of SEC accounts, where accounts can be either external accounts or contracts.
Downloads
5
Readme
[]
SecAccount
Definition: This library eases the handling of SEC accounts, where accounts can be either external accounts or contracts.
Install
npm install @sec-block/secjs-account --save
SecAccount
Kind: global class
new SecAccount(data)
| Param | | --- | | data |
secAccount.serialize()
return the RLP serialization of the account as a Buffer.
Kind: instance method of SecAccount
secAccount.getCode(state, cb)
Fetches the code from the trie.
Kind: instance method of SecAccount
| Param | Type | | --- | --- | | state | String | | cb | Function |
secAccount.setCode(trie, code, cb)
Stores the code in the trie.
Kind: instance method of SecAccount
| Param | Type | | --- | --- | | trie | String | | code | Buffer | | cb | Function |
secAccount.getStorage(trie, key, cb)
Fetches key from the account's storage.
Kind: instance method of SecAccount
| Param | Type | | --- | --- | | trie | String | | key | Buffer | | cb | Function |
secAccount.setStorage(trie, key, val, cb)
Stores a val at the key in the contract's storage.
Kind: instance method of SecAccount
| Param | Type | | --- | --- | | trie | String | | key | Buffer | | val | Buffer | | cb | Function |
secAccount.isEmpty()
Returns a Boolean determining if the account is empty.
Kind: instance method of SecAccount
Example for getCode and setCode:
// Requires manual merkle-patricia-tree install
const SecureTrie = require('merkle-patricia-tree/secure')
const Account = require('./index.js')
let code = Buffer.from('73095e7baea6a6c7c4c2dfeb977efac326af552d873173095e7baea6a6c7c4c2dfeb977efac326af552d873157', 'hex')
let raw = {
nonce: '',
balance: '0x03e7',
stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4'
}
let account = new Account(raw)
let trie = new SecureTrie()
account.setCode(trie, code, function (err, codeHash) {
console.log(`Code with hash 0x${codeHash.toString('hex')} set to trie`)
account.getCode(trie, function (err, code) {
console.log(`Code ${code.toString('hex')} read from trie`)
})
})
Example for getStorage and setStorage:
// Requires manual merkle-patricia-tree install
const SecureTrie = require('merkle-patricia-tree/secure')
const Account = require('../src/index.js')
let raw = {
nonce: '',
balance: '0x03e7',
stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4'
}
let account = new Account(raw)
let trie = new SecureTrie()
let key = Buffer.from('0000000000000000000000000000000000000000', 'hex')
let value = Buffer.from('01', 'hex')
account.setStorage(trie, key, value, function (err, value) {
account.getStorage(trie, key, function (err, value) {
console.log(`Value ${value.toString('hex')} set and retrieved from trie.`)
})
})