Thor DevKit
Typescript library to aid DApp development on VeChain Thor
Installation
npm i --save thor-devkit
Usage
import all components or some of them
import {
cry,
abi,
RLP,
Transaction,
Certificate,
Bloom
} from 'thor-devkit'
Crypto methods
they are under cry
namespace
Hashing
let hash = cry.blake2b256('hello world')
console.log(hash.toString('hex'))
hash = cry.keccak256('hello world')
console.log(hash.toString('hex'))
Secp256k1
let privKey = cry.secp256k1.generatePrivateKey()
let pubKey = cry.secp256k1.derivePublicKey(privKey)
let addr = cry.publicKeyToAddress(pubKey)
let signature = cry.secp256k1.sign(cry.keccak256('hello world'), privKey)
let recoveredPubKey = cry.secp256k1.recover(cry.keccak256('hello world'), signature)
Mnemonic & Keystore
let words = cry.mnemonic.generate()
let privateKey = cry.mnemonic.derivePrivateKey(words)
let ok = cry.mnemonic.validate(words)
let keystore = await cry.Keystore.encrypt(privateKey, 'your password')
let recoveredPrivateKey = await cry.Keystore.decrypt(keystore, 'your password')
ok = cry.Keystore.wellFormed(keystore)
let hdnode = cry.HDNode.fromMnemonic(words)
for (let i = 0; i < 5; i++) {
let child = hdnode.derive(i)
}
const pub = Buffer.from('04dc40b4324626eb393dbf77b6930e915dcca6297b42508adb743674a8ad5c69a046010f801a62cb945a6cb137a050cefaba0572429fc4afc57df825bfca2f219a', 'hex')
const chainCode = Buffer.from('105da5578eb3228655a8abe70bf4c317e525c7f7bb333634f5b7d1f70e111a33', 'hex')
hdnode = cry.HDNode.fromPublicKey(pub, chainCode)
for (let i = 0; i < 5; i++) {
let child = hdnode.derive(i)
}
RLP
let profile: RLP.Profile = {
name: 'clause',
kind: [
{ name: 'to', kind: new RLP.NullableFixedBlobKind(20) },
{ name: 'value', kind: new RLP.NumericKind(32) },
{ name: 'data', kind: new RLP.BlobKind() }
]
}
let clause = {
to: '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed',
value: 10,
data: '0x'
}
let rlp = new RLP(profile)
let data = rlp.encode(clause)
console.log(data.toString('hex'))
let obj = rlp.decode(data)
Transaction
let clauses = [{
to: '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed',
value: 10000,
data: '0x'
}]
let gas = Transaction.intrinsicGas(clauses)
console.log(gas)
let body: Transaction.Body = {
chainTag: 0x9a,
blockRef: '0x0000000000000000',
expiration: 32,
clauses: clauses,
gasPriceCoef: 128,
gas: 21000,
dependsOn: null,
nonce: 12345678
}
let tx = new Transaction(body)
let signingHash = tx.signingHash()
tx.signature = cry.secp256k1.sign(signingHash, )
let raw = tx.encode()
let decoded = Transaction.decode(raw)
Certificate
supports client side self-signed certificate
let cert: Certificate = {
purpose: 'identification',
payload: {
type: 'text',
content: 'fyi'
},
domain: 'localhost',
timestamp: 1545035330,
signer: <<<signer-address>>>
}
let jsonStr = Certificate.encode(cert)
let signature = secp256k1.sign(cry.blake2b256(jsonStr), <<<private-key>>>)
cert.signature = '0x' + signature.toString('hex')
Certificate.verify(cert)
let id = '0x' + cry.blake2b256(Certificate.encode(cert)).toString('hex')
ABI
let fn = new abi.Function({
"constant": false,
"inputs": [
{
"name": "a1",
"type": "uint256"
},
{
"name": "a2",
"type": "string"
}
],
"name": "f1",
"outputs": [
{
"name": "r1",
"type": "address"
},
{
"name": "r2",
"type": "bytes"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
})
let data = fn.encode(1, 'foo')
License
thor-devkit is licensed under the
GNU Lesser General Public License v3.0, also included
in LICENSE file in repository.