Package Exports
- sotez
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sotez) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Sotez - A JS Library for Tezos
Getting Started
npm install sotez
import sotez from 'sotez';
sotez.node.query('/chains/main/blocks/head')
.then(response => console.log(response));
Or import individule modules
import { node, rpc } from 'sotez';
node.query('/chains/main/blocks/head')
.then(response => console.log(response));
Documentation
Sotez is exported as a collection of modules. Each module and the respective functions are detailed below:
Node
setDebugMode
Sets the output mode of node functions. Allows for easier debugging of node functions.
node.setDebugMode(debugMode)
Arguments:
debugMode:
Type: Boolean
Description: Whether to enable more verbose logging
Ex:
node.setDebugMode(true)
setProvider
Sets the rpc address to use when interacting with the Tezos node.
node.setProvider(provider)
Arguments:
provider:
Type: String
Description: The RPC address to use when querying.
Ex:
node.setProvider('http://rpc-mytezoswallet.com')
resetProvider
Resets the rpc address to the default value (http://rpc-mytezoswallet.com).
node.resetProvider()
Ex:
node.resetProvider()
query
Given a valid RPC path, the query function will return a promise containing the response from the RPC server. For a complete list of available RPC paths, visit the Tezos RPC API Documentation.
query(rpcPath, postParams, httpMethod)
Arguments:
rpcPath:
Type: String
Description: The path of the RPC
postParams: [Optional (default: undefined)]
Type: Object || Array
Description: The POST parameters
httpMethod: [Optional (default: undefined)]
Type: String
Description: To explicitly set the HTTP Method to either 'GET' or 'POST'
Ex:
node.query('/chains/main/blocks/head')
.then(response => console.log(response))
node.query('/chains/main/blocks/head/helpers/forge/operations', {
branch: head.hash,
contents: ops,
}).then(response => console.log(response))
RPC
account
Originate a new account (KT1...) for an implicit account (tz1...). Returns a promise containing the successfully injected operation hash.
rpc.account(accountObject)
Arguments:
accountObject: {
keys: Object,
amount: Number,
spendable: Boolean, [Optional (default: undefined)]
delegatable: Boolean, [Optional (default: undefined)]
delegate: String, [Optional (default: undefined)]
fee: Number, [Optional (default: 0.05)]
}
Ex.
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
rpc.account({
keys,
amount: 10,
spendable: true,
delegatable: true,
delegate: 'tz1...',
fee: 0.05,
}).then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
getBalance
Retrieve the balance of an originated or implicit account.
rpc.getBalance(account)
Arguments:
account:
Type: String
Description: The address of the implicit or originated account
Ex:
rpc..getBalance('tz1...').then(response => console.log(response));
getDelegate
Retrieve the delegate of an originated or implicit account.
rpc.getDelegate(account)
Arguments:
account:
Type: String
Description: The address of the implicit or originated account
Ex:
rpc.getDelegate('tz1...').then(response => console.log(response));
getHead
Retrieve the latest block from the node.
rpc.getHead()
Ex:
rpc.getHead().then(response => console.log(response));
getHeadHash
Retrieve the hash of the latest block from the node.
rpc.getHeadHash()
Ex:
rpc.getHeadHash().then(response => console.log(response));
call
Similar to node.query, rpc.call essentially wraps node.query. Given a valid RPC path, the query function will return a promise containing the response from the RPC server. For a complete list of available RPC paths, visit the Tezos RPC API Documentation.
rpc.call(rpcPath, postParams)
Arguments:
rpcPath:
Type: String
Description: The path of the RPC
postParams: [Optional (default: undefined)]
Type: Object || Array
Description: The POST parameters
sendOperation
Given a contructed operation object, sendOperation will attempt to inject the provided operation(s) into the node.
rpc.sendOperation({ from, operation, keys }, { useLedger, curve, path })
Arguments:
from:
Type: String
Description: The account address
operation:
Type: Object || Array
Description: Operation(s) to be injected
keys:
Type: Object
Description: An object with keys ('pk', 'pkh', 'sk') pertaining to an account
useLedger:
Type: Boolean
Description: Use ledger to sign operation, Default: false
curve:
Type: One of [0x00, 0x01, 0x02]
Description: Signing curve. Default: 0x00
path:
Type: Object
Description: The derivation path. Default: "44'/1729'/0'/0'"
Ex:
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
rpc.sendOperation({ from: 'tz1...', operation, keys })
.then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
transfer
Generate and inject a transfer transaction into the node. Can either transfer tez from one account to the other or be used to call Tezos smart contracts with a parameter.
rpc.transfer(transferObject, { useLedger, curve, path })
Arguments:
transferObject: {
from: String,
keys: Object,
to: String,
amount: Number,
fee: Number, [Optional (default: 0.05)]
parameter: String || Object, [Optional (default: false)]
gasLimit: Number, [Optional (default: 200)]
storageLimit: Number = 0, [Optional (default: 0)]
mutez: Boolean = false, [Optional (default: false)] (If true, dont convert amount to tez, else use tez)
rawParam: Boolean [Optional (default: false)] (If true, accept JSON params, else use standard CLI params)
},
{
useLedger:
Type: Boolean
Description: Use ledger to sign operation, Default: false
curve:
Type: One of [0x00, 0x01, 0x02]
Description: Signing curve. Default: 0x00
path:
Type: Object
Description: The derivation path. Default: "44'/1729'/0'/0'"
}
Ex.
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
rpc.transfer({
from: 'tz1...',
keys,
to: 'tz1...',
amount: 10,
fee: 0.05,
}).then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
originate
Originate a new smart contract for an implicit account. The contract is initialized with code to generate a smart contract.
rpc.originate(originateObject)
Arguments:
originateObject: {
keys: Object,
amount: Number,
code,
init,
fee, [Optional (default: 0.05)]
spendable: Boolean, [Optional (default: false)],
delegatable: Boolean, [Optional (default: false)],
delegate: String, [Optional (default: undefined)],
gasLimit: Number, [Optional (default: 10000)]
storageLimit: Number [Optional (default: 10000)]
}
Ex.
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
rpc.originate({
keys,
amount: 10,
fee: 0.05,
code: '<code>',
init: '<init>',
}).then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
setDelegate
Sets the delegate for a given originated account.
rpc.setDelegate({ from, keys, delegate, fee, gasLimit, storageLimit })
Arguments:
from:
Type: String
Description: The originated address to set the delegate for
keys:
Type: Object
Description: An object with keys ('pk', 'pkh', 'sk') pertaining to an account
delegate:
Type: String
Description: The implicit account to set the delegate to
fee: [Optional (default: 0.05)]
Type: Number
Description: The fee for the transaction
gasLimit: [Optional (default: 0)]
Type: Number
Description: The gas limit for this transaction
storageLimit: [Optional (default: 0)]
Type: Number
Description: The storage limit for this transaction
Ex:
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
rpc.setDelegate({ from: 'KT1...', keys, delegate: 'tz1...' })
.then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
registerDelegate
Sets the delegate for a given originated account.
rpc.registerDelegate({ from, keys, delegate, gasLimit, storageLimit })
Arguments:
from:
Type: String
Description: The originated address to set the delegate for
keys:
Type: Object
Description: An object with keys ('pk', 'pkh', 'sk') pertaining to an account
delegate:
Type: String
Description: The implicit account to set the delegate to
gasLimit: [Optional (default: 0)]
Type: Number
Description: The gas limit for this transaction
storageLimit: [Optional (default: 0)]
Type: Number
Description: The storage limit for this transaction
Ex:
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
rpc.registerDelegate({ from: 'KT1...', keys, delegate: 'tz1...' })
.then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
typecheckCode
Typechecks the provided code.
rpc.typecheckCode(code)
packData
Serializes a piece of data to a binary representation.
rpc.packData(data, type)
typecheckData
Typechecks data against a type.
rpc.typecheckData(data, type)
runCode
Runs or traces code against an input and storage
rpc.runCode(code, amount, input, storage, trace)
Contract
hash
Runs or traces code against an input and storage
contract.hash(operationHash, ind)
contractOriginate
Originate a new smart contract for an implicit account. The contract is initialized with code to generate a smart contract.
contract.originate(originateObject)
Arguments:
originateObject: {
keys: Object,
amount: Number,
code,
init,
fee, [Optional (default: 0.05)]
spendable: Boolean, [Optional (default: false)],
delegatable: Boolean, [Optional (default: false)],
delegate: String, [Optional (default: undefined)],
gasLimit: Number, [Optional (default: 10000)]
storageLimit: Number [Optional (default: 10000)]
}
Ex.
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
contract.originate({
keys,
amount: 10,
fee: 0.05,
code: '<code>',
init: '<init>',
}).then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
storage
Retrieve the storage of a contract.
contract.storage(contract)
Arguments:
contract:
Type: String
Description: The address of the contract
Ex:
contract.storage('KT1...').then(response => console.log(response));
load
Retrieve the storage of a contract.
contract.load(contract)
Arguments:
contract:
Type: String
Description: The address of the contract
Ex:
contract.storage('KT1...').then(response => console.log(response));
watch
Retrieve the storage of a contract.
contract.watch(contract, timeout, callback)
Arguments:
contract:
Type: String
Description: The address of the contract
timeout:
Type: Number
Description: Interval in which to check storage of the contract
callback:
Type: Function
Description: The callback to perform when the contractstorage changes
Ex:
contract.watch('KT1...', 10000, (storage) => {
console.log(storage);
});
send
Initiate a transfer to a contract. Essentially a wrapper for rpc.transfer.
contract.send(sendObject)
Arguments:
sendObject: {
to: String
from : String,
keys: Object,
amount: Number,
parameter: String || Object, [Optional (default: false)]
fee: Number, [Optional (default: 0.05)]
gasLimit: Number, [Optional (default: 200)]
storageLimit: Number = 0, [Optional (default: 0)]
mutez: Boolean = false, [Optional (default: false)] (If true, dont convert amount to tez, else use tez)
rawParam: Boolean [Optional (default: false)] (If true, accept JSON params, else use standard CLI params)
}
Ex.
const keys = {
pk: 'edpk...',
pkh: 'tz1...',
sk: 'edsk...'
}
rpc.transfer({
from: 'tz1...',
keys,
to: 'KT1...',
amount: 10,
parameters: 'Right (Right (Right (Right (Unit))))',
fee: 0.05,
}).then(({ hash, operations }) => {
console.log(hash)
console.log(operations)
})
Crypto
extractKeys
Extract keys from a secret key.
crypto.extractKeys(secretKey)
Arguments:
secretKey:
Type: String
Description: The secret key of an account
Ex:
crypto.extractKeys('edsk...')
generateMnemonic
Generate a mnemonic.
crypto.generateMnemonic()
checkAddress
Extract keys from a secret key.
crypto.checkAddress(address)
Arguments:
address:
Type: String
Description: The address to check
Ex:
crypto.checkAddress('tz1...')
generateKeysNoSeed
Extract keys without a seed.
crypto.generateKeysNoSeed()
checkAddress
Extract keys from a secret key.
crypto.checkAddress(address)
Arguments:
address:
Type: String
Description: The address to check
Ex:
crypto.checkAddress('tz1...')
generateKeysNoSeed
Generate keys without a seed.
crypto.generateKeysNoSeed()
generateKeys
Gen
crypto.generateKeys(mnemonic, passphrase)
Arguments:
mnemonic:
Type: String
Description: The mnemonic seed used to generate keys
passphrase:
Type: String
Description: The passphrase used to generate the keys
Ex:
crypto.generateKeys('mnemonic words here ...', 'passphrase')
generateKeysFromSeedMulti
Generate keys from a mnemonic seed.
crypto.generateKeysFromSeedMulti(mnemonic, passphrase, n)
sign
Sign bytes with the secret key
crypto.sign(bytes, secretKey, watermark)
Arguments:
bytes:
Type: String
Description: The bytes to sign
secretKey:
Type: String
Description: The secret key of an account
watermark:
Type: String
Description: The watermark associated with either a block, endorsement, or generic signiture
verify
verify bytes with the public key
crypto.verify(bytes, signiture, publicKey)
Arguments:
bytes:
Type: String
Description: The bytes to verify
signiture:
Type: String
Description: The signiture from the bytes
publicKey:
Type: String
Description: The public key to verify with
Development
npm install
npm run build
License
MIT