github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/examples/xo_javascript/xo_payload.js (about)

     1  /**
     2   * Copyright 2018 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ------------------------------------------------------------------------------
    16   */
    17  
    18  'use strict'
    19  
    20  const { InvalidTransaction } = require('sawtooth-sdk/processor/exceptions')
    21  
    22  class XoPayload {
    23    constructor (name, action, space) {
    24      this.name = name
    25      this.action = action
    26      this.space = space
    27    }
    28  
    29    static fromBytes (payload) {
    30      payload = payload.toString().split(',')
    31      if (payload.length === 3) {
    32        let xoPayload = new XoPayload(payload[0], payload[1], payload[2])
    33        if (!xoPayload.name) {
    34          throw new InvalidTransaction('Name is required')
    35        }
    36        if (xoPayload.name.indexOf('|') !== -1) {
    37          throw new InvalidTransaction('Name cannot contain "|"')
    38        }
    39  
    40        if (!xoPayload.action) {
    41          throw new InvalidTransaction('Action is required')
    42        }
    43        return xoPayload
    44      } else {
    45        throw new InvalidTransaction('Invalid payload serialization')
    46      }
    47    }
    48  }
    49  
    50  module.exports = XoPayload