0.0.6 • Published 8 years ago

chroma-chain v0.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

a bag of functions

Chain = require('chroma-chain')
Chain =
  publicKey:
    create: secp256k1.publicKeyCreate
    verify: secp256k1.publicKeyVerify
    recover: secp256k1.recover
  privateKey:
    verify: secp256k1.privateKeyVerify
  signature:
    create: secp256k1.sign
    verify: secp256k1.verify
    recover: (sig, message, recover) ->
      pubKey = Chain.publicKey.recover(message, sig, recover)
      throw new Error "bad recovered key #{pubKey.toString('hex')}" unless Chain.publicKey.verify pubKey
      pubKey
  wallet:
    create: (seed, options) ->
      new HDKey.fromMasterSeed binString seed, (options || in: 'hex')
  hash: hash
  address: require('./address')
  json:
    serialize: (json) -> binString stringify(json), in: 'utf8'
    deserialize: (data) -> JSON.parse data.toString()
    hash: (payload) -> hash.sha256 stringify(payload)
    sign: (payload, key) ->
      key = binString key, in: 'hex'
      throw new Error "bad privateKey " + key unless Chain.privateKey.verify key
      data = Chain.json.serialize payload
      message = hash.sha256 data
      sigObj = Chain.signature.create(message, key)
      encoded: binString sigObj.signature, out: 'base64'
      recover: sigObj.recovery
    verify: (payload, signature) ->
      sig = binString signature.encoded, in: 'base64'
      data = Chain.json.serialize payload
      message = hash.sha256 data
      pubKey = Chain.signature.recover sig, message, signature.recover
      if Chain.signature.verify(message, sig, pubKey) then pubKey else undefined