Code Monkey home page Code Monkey logo

thor-model-kit's Introduction

Thor Model Kit

Deprecated, use thor-devkit instead.

Typescript library defines VeChain Thor data models, to aid DApp development.

Gitter

NPM Version Build Status Coverage Status

Installation

npm i --save thor-model-kit

Usage

All widgets are as below:

import { 
    Address,
    Bytes32,
    BigInt,
    blake2b256,
    keccak256,
    Secp256k1,
    Mnemonic,
    Keystore,
    Transaction
} from 'thor-model-kit'

Basic types

let addr = Address.fromHex('0x7567d83b7b8d80addcb281a71d54fc7b3364ffed', '0x' /* defaults to '0x' */)
console.log(addr.toString('0x' /* defaults to '0x' */))
// 0x7567d83b7b8d80addcb281a71d54fc7b3364ffed
console.log(addr.toString('vx:'))
// vx:7567d83b7b8d80addcb281a71d54fc7b3364ffed

let b32 = Bytes32.fromHex('0xda90eaea52980bc4bb8d40cb2ff84d78433b3b4a6e7d50b75736c5e3e77b71ec', '0x' /* defaults to '0x' */)
console.log(b32.toString('0x' /* defaults to '0x' */))
// 0xda90eaea52980bc4bb8d40cb2ff84d78433b3b4a6e7d50b75736c5e3e77b71ec

let bi = BigInt.from(123)
console.log(bi.toString(10))
// 123

Crypto methods

let hash = blake2b256('hello world')
console.log(hash.toString())
// 0x256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610

hash = keccak256('hello world')
console.log(hash.toString())
// 0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad

// Secp256k1
let privKey = Secp256k1.generatePrivateKey()
let pubKey = Secp256k1.derivePublicKey(privKey)
let addr = Secp256k1.deriveAddress(pubKey)
let signature = Secp256k1.sign(keccak256('hello world'), privKey)
let recoveredPubKey = Secp256k1.recover(keccak256('hello world'), signature)

Mnemonic & Keystore

// generate BIP39 mnemonic words, default to 12 words(128bit strength)
let words = Mnemonic.generate()

// derive private key from mnemonic words according to BIP32, using the path `m/44'/818'/0'/0/0`.
// defined for VET at https://github.com/satoshilabs/slips/blob/master/slip-0044.md
let privateKey = Mnemonic.derivePrivateKey(words)

// in recovery process, validation is recommended
let ok = Mnemonic.validate(words)

// encrypt/decrypt private key using Ethereum's keystore scheme
let keystore = await Keystore.encrypt(privateKey, 'your password')

// throw for wrong password
let recoveredPrivateKey = await Keystore.decrypt(keystore, 'your password')

// roughly check keystore format
let ok = Keystore.wellFormed(keystore)

Transaction codec

let clauses = clauses: [{
    to: Address.fromHex('7567d83b7b8d80addcb281a71d54fc7b3364ffed', ''),
    value: BigInt.from(10000),
    data: Buffer.alloc(0)
}]

// calc intrinsic gas
let gas = Transaction.intrinsicGas(clauses)
console.log(gas)
// 21000

let body: Transaction.Body = {
    chainTag: 0x9a,
    blockRef: Buffer.from('0000000000000000', 'hex'),
    expiration: 32,
    clauses: clauses,
    gasPriceCoef: 128,
    gas: BigInt.from(21000),
    dependsOn: null,
    nonce: BigInt.from(12345678),
    reserved: []
}

let tx = new Transaction(body)
tx.signature = Secp256k1.sign(tx.signingHash, /* your private key */)

let raw = tx.encode()
let decoded = Transaction.decode(raw)

License

thor-model-kit is licensed under the GNU Lesser General Public License v3.0, also included in LICENSE file in repository.

thor-model-kit's People

Contributors

kbrizzle avatar libotony avatar qianbin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.