项目作者: vechain

项目描述 :
DEPRECATED! try https://github.com/vechain/thor-devkit.js instead
高级语言: TypeScript
项目地址: git://github.com/vechain/thor-model-kit.git
创建时间: 2018-06-09T08:01:18Z
项目社区:https://github.com/vechain/thor-model-kit

开源协议:GNU Lesser General Public License v3.0

下载


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

  1. npm i --save thor-model-kit

Usage

All widgets are as below:

  1. import {
  2. Address,
  3. Bytes32,
  4. BigInt,
  5. blake2b256,
  6. keccak256,
  7. Secp256k1,
  8. Mnemonic,
  9. Keystore,
  10. Transaction
  11. } from 'thor-model-kit'

Basic types

  1. let addr = Address.fromHex('0x7567d83b7b8d80addcb281a71d54fc7b3364ffed', '0x' /* defaults to '0x' */)
  2. console.log(addr.toString('0x' /* defaults to '0x' */))
  3. // 0x7567d83b7b8d80addcb281a71d54fc7b3364ffed
  4. console.log(addr.toString('vx:'))
  5. // vx:7567d83b7b8d80addcb281a71d54fc7b3364ffed
  6. let b32 = Bytes32.fromHex('0xda90eaea52980bc4bb8d40cb2ff84d78433b3b4a6e7d50b75736c5e3e77b71ec', '0x' /* defaults to '0x' */)
  7. console.log(b32.toString('0x' /* defaults to '0x' */))
  8. // 0xda90eaea52980bc4bb8d40cb2ff84d78433b3b4a6e7d50b75736c5e3e77b71ec
  9. let bi = BigInt.from(123)
  10. console.log(bi.toString(10))
  11. // 123

Crypto methods

  1. let hash = blake2b256('hello world')
  2. console.log(hash.toString())
  3. // 0x256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610
  4. hash = keccak256('hello world')
  5. console.log(hash.toString())
  6. // 0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad
  7. // Secp256k1
  8. let privKey = Secp256k1.generatePrivateKey()
  9. let pubKey = Secp256k1.derivePublicKey(privKey)
  10. let addr = Secp256k1.deriveAddress(pubKey)
  11. let signature = Secp256k1.sign(keccak256('hello world'), privKey)
  12. let recoveredPubKey = Secp256k1.recover(keccak256('hello world'), signature)

Mnemonic & Keystore

  1. // generate BIP39 mnemonic words, default to 12 words(128bit strength)
  2. let words = Mnemonic.generate()
  3. // derive private key from mnemonic words according to BIP32, using the path `m/44'/818'/0'/0/0`.
  4. // defined for VET at https://github.com/satoshilabs/slips/blob/master/slip-0044.md
  5. let privateKey = Mnemonic.derivePrivateKey(words)
  6. // in recovery process, validation is recommended
  7. let ok = Mnemonic.validate(words)
  8. // encrypt/decrypt private key using Ethereum's keystore scheme
  9. let keystore = await Keystore.encrypt(privateKey, 'your password')
  10. // throw for wrong password
  11. let recoveredPrivateKey = await Keystore.decrypt(keystore, 'your password')
  12. // roughly check keystore format
  13. let ok = Keystore.wellFormed(keystore)

Transaction codec

  1. let clauses = clauses: [{
  2. to: Address.fromHex('7567d83b7b8d80addcb281a71d54fc7b3364ffed', ''),
  3. value: BigInt.from(10000),
  4. data: Buffer.alloc(0)
  5. }]
  6. // calc intrinsic gas
  7. let gas = Transaction.intrinsicGas(clauses)
  8. console.log(gas)
  9. // 21000
  10. let body: Transaction.Body = {
  11. chainTag: 0x9a,
  12. blockRef: Buffer.from('0000000000000000', 'hex'),
  13. expiration: 32,
  14. clauses: clauses,
  15. gasPriceCoef: 128,
  16. gas: BigInt.from(21000),
  17. dependsOn: null,
  18. nonce: BigInt.from(12345678),
  19. reserved: []
  20. }
  21. let tx = new Transaction(body)
  22. tx.signature = Secp256k1.sign(tx.signingHash, /* your private key */)
  23. let raw = tx.encode()
  24. 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.