ara-util v2.0.3
ara-util
Common utility functions to be used in various Ara modules.
Status
This project is in active development.
Stability
Stability: 2 - Stable. Compatibility with the npm ecosystem is a high priority.
Installation
$ npm install ara-util --saveDependencies
API
Most of the functions exported by this module will check for input
correctness. If given incorrect input, a function will throw a
TypeError with a message describing the error.
- async util.getAFSOwnerIdentity(opts)
- async util.getAddressFromDID(did, keyringOpts)
- async util.isCorrectPassword(opts)
- async util.validate(opts)
- util.checkAFSExistence(opts)
- util.getDocumentKeyHex(ddo)
- util.getDocumentOwner(ddo)
- util.hash(str, [encoding])
- util.hashDID(did, [encoding])
- util.getIdentifier(did)
Transform
Web3
- async util.web3.account.load(opts)
- async util.web3.call(opts)
- util.web3.isAddress(address)
- util.web3.sha3(params, [abiEncode])
- util.web3.getContext(provider)
Contract
- async util.web3.contract.deploy(opts)
- async util.web3.contract.estimateGas(tx, opts)
- async util.web3.contract.get(abi, address)
Tx
- async util.web3.tx.create(opts, [signTx])
- async util.web3.tx.sendSignedTransaction(tx)
- util.web3.tx.estimateCost(tx, [denomination])
- util.web3.tx.sign(tx, privateKey)
- util.web3.tx.getTransactionReceipt(hash)
ABI
- util.web3.abi.encodeFunctionCall(abi, functionName, values)
- util.web3.abi.encodeParameter(type, parameter)
- util.web3.abi.encodeParameters(typesArray, parameters)
async util.getAFSOwnerIdentity(opts)
Returns the owner DID of a given AFS DID, used for resolving the document of the identity that created an AFS.
optsdid-DIDof the document to resolve the owner formnemonic- mnemonic of the owning identitypassword- password for the ownerkeyringOpts- optional Keyring options
const identity = await aid.create({ password })
const { mnemonic, did } = identity
const { did: owner } = did
const { afs } = await createAFS({ owner, password })
const resolvedOwner = await util.getAFSOwnerIdentity({ did: afs.did, mnemonic, password })async util.getAddressFromDID(did, keyringOpts)
Retrieves the Ethereum address associated with a DID.
did- TheDIDfrom which to obtain the Ethereum addresskeyringOpts- optional Keyring options
const address = await util.getAddressFromDID(did)async util.isCorrectPassword(opts)
Validates if an identity's password is correct by attempting to decrypt the Ara keystore (keystore/ara). The identity must be archived locally for this function to work. To recover an existing identity, see aid.recover.
optsddo- document to validate password forpassword- password to validate against
const password = 'myPass'
const isCorrect = await util.isCorrectPassword({ ddo, password })async util.validate(opts)
Validates that a resolved document based on a DID can be decrypted with the password, proving ownership. This uses util.isCorrectPassword internally after resolving the document and throws if the password is incorrect or the keystore is not locally accessible.
optsdid-DIDof identity to validatepassword- password of the identityddo- optionalDDOto use for validation (instead of resolving)keyringOpts- optional Keyring options
Returns object:
did- The identifier portion of theDIDthat was passed inddo- TheDDOthat thedidandpasswordwere authenticated against
const password = 'myPass'
const did = 'did:ara:41dd7aabfa3763306d8ec69559508c0635bbc2bb591fb217905f8e9a9676a7ec'
const { did: didIdentifier, ddo } = await util.validate({ did, password })util.checkAFSExistence(opts)
Checks if an AFS exists locally.
optsdid- TheDIDof the AFS to check
Returns a boolean indicating whether the AFS exists locally.
const exists = util.checkAFSExistence({ did })util.getDocumentKeyHex(ddo)
Returns the publicKeyHex of the first publicKey entry within a document.
ddo- Document to retreivepublicKeyHexfrom
const publicKeyHex = util.getDocumentKeyHex(ddo) // 41dd7aabfa3763306d8ec69559508c0635bbc2bb591fb217905f8e9a9676a7ecutil.getDocumentOwner(ddo)
Returns the DID identifier of a document owner, or first entry in the authentication array of a DDO. The difference between this function and util.getAFSOwnerIdentity is that this function does not do any resolving.
ddo- Document to retrieve owner from
const owner = util.getDocumentOwner(ddo)util.hash(str, [encoding])
blake2b hashes a string and returns the hashed string. An optional encoding can be provided, which defaults to hex encoding.
str-Stringto hashencoding- optionalencodingof the string
const result = util.hash('Hello')util.hashDID(did, [encoding])
blake2b hashes a DID with an optional encoding, which defaults to hex. The difference between this and hash is that this function takes care of extracting the identifer portion of the DID prior to hashing.
did-DIDto hashencoding- optionalencodingof theDID
const hash = util.hashDID('did:ara:14078363f2d9aa0d269827261544e598d8bf11c66f88e49d05e85bd3d181ec8e')util.getIdentifier(did)
Returns the identifier portion of a DID.
did-DIDto parse
const identifier = util.getIdentifier('did:ara:14078363f2d9aa0d269827261544e598d8bf11c66f88e49d05e85bd3d181ec8e') // 14078363f2d9aa0d269827261544e598d8bf11c66f88e49d05e85bd3d181ec8eutil.transform.toBuffer(input, [encoding])
Converts a string to a buffer.
input-stringto convertencoding- Encoding of string for conversion
const buf = util.web3.toBuffer('hi')
// <Buffer 68 69>util.transform.toHexString(input, [opts])
Converts the input to a hex string. Can optionally prepend 0x for compatability with EVM.
input-String,Number, orBufferto be convertedoptsethify- Should the result be prepended by a0xencoding- The type of encoding of the input
const str = util.transform.toHexString('ef61059258414a65bf2d94a4fd3b503b5fee8b48', { encoding: 'hex', ethify: true })
// 0xef61059258414a65bf2d94a4fd3b503b5fee8b48async util.web3.account.load(opts)
Loads the Ethereum account associated with an Ara identity, returning the loaded account object.
optsdid-DIDto load account frompassword- Identity's password
const did = 'did:ara:14078363f2d9aa0d269827261544e598d8bf11c66f88e49d05e85bd3d181ec8e'
const password = 'myPass'
const loadedAccount = await util.web3.account.load({ did, password })async util.web3.call(opts)
Makes a function call to a deployed Ethereum contract.
optsabi-ABIof the compiled Solidity contract to calladdress- Ethereum address where contract has been deployedfunctionName- Name of the function on the contract to call
Returns the result of the call.
const { abi } = require('./build/contracts/MyContract.json')
const contractAddress = '0xef61059258414a65bf2d94a4fd3b503b5fee8b48'
const result = await call({ abi, address: contractAddress, functionName: 'myContractFunction' })util.web3.isAddress(address)
Validates whether a hex string is a valid Ethereum address.
address- string to validate
let isAddress = util.web3.isAddress('0xef61059258414a65bf2d94a4fd3b503b5fee8b48') // true
isAddress = util.web3.isAddress('Hello') // falseutil.web3.sha3(params, [abiEncode = true])
SHA3 hashes given parameters. By default, ABI encodes parameters before hashing.
params- Parameters of any type or object containing parametersabiEncode- Whether toABIencode parameters prior to hashing. Defaults to true.
const result = util.web3.sha3({ param1: 1, param2: 2 })util.web3.getContext([provider])
Creates and returns an Ara context object containing a Web3 instance.
provider- optionalBooleanflag indicating whether a new Web3 provider should be created. Defaults totrue
const ctxProvider = util.web3.getContext()
await ctxProvider.ready() // wait for provider to be ready
let { web3 } = ctxProvider
ctxProvider.close() // always close context if provider was created
const ctxNoProvider = util.web3.getContext(false);
({ web3 } = ctxNoProvider) // no need to wait for provider or close contextasync util.web3.contract.deploy(opts)
Deploys a contract to the network of the current Web3 provider. This returns an instance of the deployed Contract as well as the gasLimit that was used for deploying this contract.
optsabi-ABIof the compiled Solidity contract to deploybytecode- Bytecode of compiled contractaccount- Ethereum account to deploy from
const { abi, bytecode } = require('./build/contracts/MyContract.json')
const account = await util.web3.account.load({ did, password })
const { contractAddress, gasLimit } = await deploy({ account, abi, bytecode })async util.web3.contract.estimateGas(tx, opts)
Estimates the gas cost of a transaction.
tx- TheEthereumTxobjectcreated from util.web3.tx.create()opts- contract options
const transaction = await util.web3.tx.create({
account,
to: '0xef61059258414a65bf2d94a4fd3b503b5fee8b48',
data: {
abi,
functionName: 'setNumbers',
values: [1, 2, 3]
}
})
const estimate = util.web3.contract.estimateGas(transaction, opts)util.web3.contract.get(abi, address)
Gets a contract instance based on its ABI and deployed address
abi-ABIof compiled Solidity contractaddress- Deployed address of the contract
Returns object
contract- Thecontractinstancectx- The Ara contextobject
const address = '0xef61059258414a65bf2d94a4fd3b503b5fee8b48'
const { contract, ctx } = util.web3.contract.get(abi, address)
ctx.close()async util.web3.tx.create(opts, [signTx])
Creates an EthereumTx object that can be published to the current network. Signs the transaction with the account's privateKey by default.
optsaccount- the account that will be initiating the transactionto- the address of the contract of the transactiondata- function data to encode as part of the transactionabi- contract ABIfunctionName- name of function that's being calledvalues- function argument values as an array
gasPrice- optional Gas price to use for this transactiongasLimit- optional Gas limit to use for this transaction
signTx- should this transaction be signed
Returns object:
tx- The transactionobjectcreatedctx- The Ara contextobject
const account = await util.web3.account.load({ did, password })
const { tx: signedTx, ctx: ctx1 } = await util.web3.tx.create({ account, to: '0xef61059258414a65bf2d94a4fd3b503b5fee8b48' })
ctx1.close()
const { tx: unsignedTx, ctx: ctx2 } = await util.web3.tx.create({ account, to: '0xef61059258414a65bf2d94a4fd3b503b5fee8b48' }, false)
ctx2.close()
const contractAddress = '0xef61059258414a65bf2d94a4fd3b503b5fee8b48'
const { tx: anotherTx, ctx3 } = await util.web3.tx.create({
account,
to: contractAddress,
data: {
abi,
functionName: 'setPrice',
values: [100]
},
gasLimit: 100000
})
ctx3.close()async util.web3.tx.sendSignedTransaction(tx[, { onhash, onreceipt, onconfirmation, onerror, onmined }])
Sends a signed transaction to the current network.
tx- The signedEthereumTxobjectto publish to the networkonhash- Optional callback fired right after the transaction is sent and a transaction hashstringis availableonreceipt- Optional callback fired when the transaction receiptObjectis availableonconfirmation- Optional callback fired for every confirmation up to the 12th confirmation. Receives the confirmation number as the first and the receiptObjectas the second argument. Fired from confirmation 0 on, which is the block where its mined.onerror- Optional callback fired if an error occurs during sending. If an out of gas error, the second parameter is the receipt.onmined- Optional callback fired when receiptObjectis available
Returns the transaction receipt object.
const account = await util.web3.account.load({ did, password })
const { tx: signedTx, ctx } = await util.web3.tx.create({ account, to: '0xef61059258414a65bf2d94a4fd3b503b5fee8b48' })
const receipt = await util.web3.tx.sendSignedTransaction(signedTx)
ctx.close()util.web3.tx.estimateCost(tx, [denomination])
Estimates the cost of a transaction. The returned value can be a particular denomination, defaults to ether. You can find valid denomination values here.
tx-EthereumTxobjectof the transactiondenomination- Unit to return the cost in
const costInEth = util.web3.tx.estimateCost(tx)
const costInFinney = util.web3.tx.estimateCost(tx, 'finney')util.web3.tx.sign(tx, privateKey)
Signs a transaction object with an account's privateKey.
tx-EthereumTxobjectof the transactionprivateKey- The Ethereum account'sprivateKeysigning the transaction
const { tx: unsignedTx, ctx } = await util.web3.tx.create({ account, to: '0xef61059258414a65bf2d94a4fd3b503b5fee8b48' }, false)
const { privateKey } = account
const signedTx = util.web3.tx.sign(unsignedTx, privateKey)
ctx.close()util.web3.tx.getTransactionReceipt(hash)
Returns the transaction receipt object for a mined transaction. Returns null for pending transactions.
hash- The transaction hash of a mined transaction
const receipt = await util.web3.tx.getTransactionReceipt(hash)util.web3.abi.encodeFunctionCall(abi, functionName, values)
Encodes a function call to its ABI signature, required for sending signed transactions that require a function on a deployed contract.
abi-ABIof the compiled Solidity contractfunctionName- name of the function from theABIto encodevalues- array of arguments for the function to encode
Returns the encoded function call as a String.
const { abi } = require('./build/contracts/MyContract.json')
const encoded = util.web3.abi.encodeFunctionCall(abi, 'myFunctionName', ['arg1', 'arg2'])util.web3.abi.encodeParameter(type, parameter)
Encodes a function parameter to its ABI signature.
type- Type of parameter (see Solidity types)parameter- Value of parameter
Returns the encoded parameter as a String.
const encoded = util.web3.abi.encodeParameter('bytes', '0xFF')util.web3.abi.encodeParameters(typesArray, parameters)
Encodes multiple function parameters to their ABI signatures.
typesArray- Array of types to encode (see Solidity types)parameters- Array of parameter values to encode
Returns the encoded parameters as a String.
const encoded = util.web3.abi.encodeParameter(['bytes', 'string'], ['0xFF', 'Hello'])Contributing
See Also
License
LGPL-3.0