message.js

/*
 * Copyright (c) 2018-2019 Zippie Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
const shajs = require('sha.js')
const secp256k1 = require('secp256k1')

/**
 * @protected
 * @param {*} message 
 */
function AuthedMessage(message, opts) {
  opts = opts || {}

  const _serialized = JSON.stringify(message)
  const _message = JSON.parse(_serialized)

  let origin = null
  let signature = null
  let timestamp = null

  return {
    get message () { return _message },
    get serialized () { return _serialized },

    sign (key) {
      const tstamp = opts.expires || Date.now()
      const digest = shajs('sha256')
          .update(_serialized)
          .update(tstamp.toString())
          .digest()

      origin = secp256k1.publicKeyCreate(key, false).toString('hex')

      signature = secp256k1.sign(digest, key)
      signature = {
        signature: signature.signature.toString('hex'),
        recovery: signature.recovery
      }
      timestamp = tstamp
      return this
    },

    async signWithVault (vault, __derive, opts) {
      opts = opts || {}

      const tstamp = opts.expires || Date.now()
      const digest = shajs('sha256')
          .update(_serialized)
          .update(tstamp.toString())
          .digest()

      origin = (await vault.secp256k1.keyInfo(__derive)).pubkey
      timestamp = tstamp
      signature = await vault.secp256k1.sign(__derive, digest)
          .then(r => ({ signature: r.signature, recovery: r.recovery }))

      return this
    },

    toJSON () {
      return {
        message,
        origin,
        signature,
        timestamp,
      }
    },

    serialize () {
      return {
        message: _serialized,
        origin,
        signature,
        timestamp,
      }
    }
  }
}

module.exports = AuthedMessage