Exemplo n.º 1
0
  api.checkWork = function (work, blockHash) {
    var t = (0, _functions.hex_uint8)(MAIN_NET_WORK_THRESHOLD);
    var context = blake.blake2bInit(8, null);
    blake.blake2bUpdate(context, (0, _functions.hex_uint8)(work).reverse());
    blake.blake2bUpdate(context, (0, _functions.hex_uint8)(blockHash));
    var threshold = blake.blake2bFinal(context).reverse();

    if (threshold[0] == t[0]) if (threshold[1] == t[1]) if (threshold[2] == t[2]) if (threshold[3] >= t[3]) return true;
    return false;
  };
 /**
  * Returns calculated hash or Builds the request and calculates the hash
  *
  * @throws An exception if missing parameters or invalid parameters
  * @type {Hexadecimal64Length}
  * @readonly
  */
 get hash () {
   if (this.transaction === null) throw new Error('transaction is not set.')
   if (!this.transaction.destination) throw new Error('transaction destination is not set.')
   if (!this.transaction.amount) throw new Error('transaction amount is not set.')
   const context = super.hash()
   let account = Utils.hexToUint8(Utils.keyFromAccount(this.transaction.destination))
   blake.blake2bUpdate(context, account)
   let amount = Utils.hexToUint8(Utils.decToHex(this.transaction.amount, 16))
   blake.blake2bUpdate(context, amount)
   return Utils.uint8ToHex(blake.blake2bFinal(context))
 }
Exemplo n.º 3
0
 /**
  * Returns calculated hash or Builds the request and calculates the hash
  *
  * @throws An exception if missing parameters or invalid parameters
  * @type {Hexadecimal64Length}
  * @readonly
  */
 get hash () {
   if (this.transaction === null) throw new Error('transaction is not set.')
   if (this.tokenFee === null) throw new Error('token fee is not set.')
   const context = super.hash()
   for (let transaction of this.transactions) {
     blake.blake2bUpdate(context, Utils.hexToUint8(Utils.keyFromAccount(transaction.destination)))
     blake.blake2bUpdate(context, Utils.hexToUint8(Utils.decToHex(transaction.amount, 16)))
   }
   blake.blake2bUpdate(context, Utils.hexToUint8(Utils.decToHex(this.tokenFee, 16)))
   return Utils.uint8ToHex(blake.blake2bFinal(context))
 }
Exemplo n.º 4
0
const checkWork = (work, previousHash, testNet) => {
  let t = hexToUint8(MAIN_NET_WORK_THRESHOLD)
  if (testNet) t = hexToUint8(TEST_NET_WORK_THRESHOLD)
  const context = blake.blake2bInit(8, null)
  blake.blake2bUpdate(context, hexToUint8(work).reverse())
  blake.blake2bUpdate(context, hexToUint8(previousHash))
  const threshold = blake.blake2bFinal(context).reverse()
  if (testNet && threshold[0] === t[0]) return true
  if (!testNet && threshold[0] === t[0] && threshold[1] === t[1] && threshold[2] === t[2] && threshold[3] >= t[3]) return true
  return false
}
 /**
  * Creates a Blake2b Context for the request
  * @returns {context} - Blake2b Context
  */
 hash () {
   if (!this.tokenID) throw new Error('TokenID is not set.')
   let context = super.hash()
   let tokenID = Utils.hexToUint8(this.tokenID)
   blake.blake2bUpdate(context, tokenID)
   return context
 }
Exemplo n.º 6
0
 /**
  * Returns calculated hash or Builds the request and calculates the hash
  *
  * @throws An exception if missing parameters or invalid parameters
  * @type {Hexadecimal64Length}
  * @readonly
  */
 get hash () {
   if (this.amount === null) throw new Error('Amount is not set.')
   const context = super.hash()
   let amount = Utils.hexToUint8(Utils.decToHex(this.amount, 16))
   blake.blake2bUpdate(context, amount)
   return Utils.uint8ToHex(blake.blake2bFinal(context))
 }
Exemplo n.º 7
0
  api.checkWork = function (work) {
    var blockHash = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

    if (blockHash === false) {
      blockHash = api.getPrevious();
    }

    var t = (0, _functions.hex_uint8)(MAIN_NET_WORK_THRESHOLD);
    var context = blake.blake2bInit(8, null);
    blake.blake2bUpdate(context, (0, _functions.hex_uint8)(work).reverse());
    blake.blake2bUpdate(context, (0, _functions.hex_uint8)(blockHash));
    var threshold = blake.blake2bFinal(context).reverse();

    if (threshold[0] == t[0]) if (threshold[1] == t[1]) if (threshold[2] == t[2]) if (threshold[3] >= t[3]) return true;
    return false;
  };
Exemplo n.º 8
0
 /**
  * Return the token id
  * @type {string}
  */
 get tokenID () {
   if (this._tokenID) {
     return this._tokenID
   } else {
     if (!this.origin) throw new Error('Origin account is not set.')
     if (!this.previous) throw new Error('Previous is not set.')
     if (!this.symbol) throw new Error('Symbol is not set.')
     if (!this.name) throw new Error('Name is not set.')
     const context = blake.blake2bInit(32, null)
     blake.blake2bUpdate(context, Utils.hexToUint8(this.origin))
     blake.blake2bUpdate(context, Utils.hexToUint8(this.previous))
     blake.blake2bUpdate(context, Utils.hexToUint8(Utils.stringToHex(this.symbol + this.name)))
     let tokenID = Utils.uint8ToHex(blake.blake2bFinal(context))
     this.tokenID = tokenID
     return this.tokenID
   }
 }
Exemplo n.º 9
0
  api.newKeyFromSeed = function () {
    if (seed.length != 32) throw "Seed should be set first.";

    var index = lastKeyFromSeed + 1;
    index = (0, _functions.hex_uint8)((0, _functions.dec2hex)(index, 4));

    var context = blake.blake2bInit(32);
    blake.blake2bUpdate(context, seed);
    blake.blake2bUpdate(context, index);

    var newKey = blake.blake2bFinal(context);

    lastKeyFromSeed++;

    logger.log("New key generated");
    api.addSecretKey((0, _functions.uint8_hex)(newKey));

    return (0, _functions.accountFromHexKey)((0, _functions.uint8_hex)(nacl.sign.keyPair.fromSecretKey(newKey).publicKey));
  };
Exemplo n.º 10
0
  api.pack = function () {
    var pack = {};
    var tempKeys = [];
    for (var i in keys) {
      var aux = {};
      aux.priv = (0, _functions.uint8_hex)(keys[i].priv);
      aux.pub = (0, _functions.uint8_hex)(keys[i].pub);
      aux.account = keys[i].account;
      aux.balance = keys[i].balance.toString();
      aux.pendingBalance = keys[i].pendingBalance.toString();
      aux.lastBlock = keys[i].lastBlock;
      aux.pendingBlocks = [];
      aux.chain = [];
      aux.representative = keys[i].representative;
      aux.label = keys[i].label;

      for (var j in keys[i].chain) {
        aux.chain.push(keys[i].chain[j].getEntireJSON());
      }
      tempKeys.push(aux);
    }
    pack.readyBlocks = [];

    for (var _i7 in readyBlocks) {
      pack.readyBlocks.push(readyBlocks[_i7].getEntireJSON());
    }
    pack.keys = tempKeys;
    pack.seed = (0, _functions.uint8_hex)(seed);
    pack.last = lastKeyFromSeed;
    pack.recent = recentTxs;
    pack.remoteWork = remoteWork;
    pack.autoWork = autoWork;
    pack.minimumReceive = minimumReceive.toString();

    pack = JSON.stringify(pack);
    pack = (0, _functions.stringToHex)(pack);
    pack = new Buffer(pack, 'hex');

    var context = blake.blake2bInit(32);
    blake.blake2bUpdate(context, pack);
    checksum = blake.blake2bFinal(context);

    var salt = new Buffer(nacl.randomBytes(16));
    var key = pbkdf2.pbkdf2Sync(passPhrase, salt, iterations, 32, 'sha1');

    var options = { mode: AES.CBC, padding: Iso10126 };
    var encryptedBytes = AES.encrypt(pack, key, salt, options);

    var payload = Buffer.concat([new Buffer(checksum), salt, encryptedBytes]);
    return payload.toString('hex');
  };
Exemplo n.º 11
0
  api.build = function () {

    switch (type) {
      case 'send':
        data = "";
        data += MAGIC_NUMBER + VERSION_MAX + VERSION_USING + VERSION_MIN + (0, _functions.uint8_hex)(blockID[type]) + EXTENSIONS;
        data += previous;
        data += destination;
        data += balance;

        var context = blake.blake2bInit(32, null);
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(previous));
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(destination));
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(balance));
        hash = (0, _functions.uint8_hex)(blake.blake2bFinal(context));
        break;

      case 'receive':
        data = "";
        data += MAGIC_NUMBER + VERSION_MAX + VERSION_USING + VERSION_MIN + (0, _functions.uint8_hex)(blockID[type]) + EXTENSIONS;
        data += previous;
        data += source;

        var context = blake.blake2bInit(32, null);
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(previous));
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(source));
        hash = (0, _functions.uint8_hex)(blake.blake2bFinal(context));
        break;

      case 'open':
        data = "";
        data += MAGIC_NUMBER + VERSION_MAX + VERSION_USING + VERSION_MIN + (0, _functions.uint8_hex)(blockID[type]) + EXTENSIONS;
        data += source;
        data += representative;
        data += account;

        var context = blake.blake2bInit(32, null);
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(source));
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(representative));
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(account));
        hash = (0, _functions.uint8_hex)(blake.blake2bFinal(context));
        break;

      case 'change':
        data = "";
        data += MAGIC_NUMBER + VERSION_MAX + VERSION_USING + VERSION_MIN + (0, _functions.uint8_hex)(blockID[type]) + EXTENSIONS;
        data += previous;
        data += representative;

        var context = blake.blake2bInit(32, null);
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(previous));
        blake.blake2bUpdate(context, (0, _functions.hex_uint8)(representative));
        hash = (0, _functions.uint8_hex)(blake.blake2bFinal(context));
        break;

      default:
        throw "Block parameters need to be set first.";
    }

    return hash;
  };
Exemplo n.º 12
0
  api.load = function (data) {
    var bytes = new Buffer(data, 'hex');
    checksum = bytes.slice(0, 32);
    var salt = bytes.slice(32, 48);
    var payload = bytes.slice(48);
    var key = pbkdf2.pbkdf2Sync(passPhrase, salt, iterations, 32, 'sha1');

    var options = {};
    options.padding = options.padding || Iso10126;
    var decryptedBytes = AES.decrypt(payload, key, salt, options);

    var context = blake.blake2bInit(32);
    blake.blake2bUpdate(context, decryptedBytes);
    var hash = (0, _functions.uint8_hex)(blake.blake2bFinal(context));

    if (hash != checksum.toString('hex').toUpperCase()) throw "Wallet is corrupted or has been tampered.";

    var walletData = JSON.parse(decryptedBytes.toString('utf8'));

    seed = (0, _functions.hex_uint8)(walletData.seed);
    lastKeyFromSeed = walletData.last;
    recentTxs = walletData.recent;
    remoteWork = [];
    autoWork = walletData.autoWork;
    readyBlocks = [];
    minimumReceive = walletData.minimumReceive != undefined ? bigInt(walletData.minimumReceive) : bigInt("1");

    for (var i in walletData.readyBlocks) {
      var blk = new Block();
      blk.buildFromJSON(walletData.readyBlocks[i]);
      readyBlocks.push(blk);
    }

    for (var _i8 in walletData.keys) {
      var aux = {};

      aux.chain = [];
      for (var j in walletData.keys[_i8].chain) {
        var _blk = new Block();
        _blk.buildFromJSON(walletData.keys[_i8].chain[j]);
        aux.chain.push(_blk);
      }

      aux.priv = (0, _functions.hex_uint8)(walletData.keys[_i8].priv);
      aux.pub = (0, _functions.hex_uint8)(walletData.keys[_i8].pub);
      aux.account = walletData.keys[_i8].account;
      aux.balance = bigInt(walletData.keys[_i8].balance ? walletData.keys[_i8].balance : 0);
      aux.lastBlock = aux.chain.length > 0 ? aux.chain[aux.chain.length - 1].getHash(true) : "";
      aux.lastPendingBlock = aux.lastBlock;
      aux.pendingBalance = bigInt(walletData.keys[_i8].pendingBalance ? walletData.keys[_i8].pendingBalance : 0);
      aux.pendingBlocks = [];
      aux.representative = walletData.keys[_i8].representative != undefined ? walletData.keys[_i8].representative : aux.account;
      aux.label = walletData.keys[_i8].label != undefined ? walletData.keys[_i8].label : "";

      keys.push(aux);
      if (lastPendingBlock.length == 64) api.workPoolAdd(lastPendingBlock, aux.account, true);
    }
    api.useAccount(keys[0].account);
    api.recalculateWalletBalances();
    ciphered = false;
    return walletData;
  };
Exemplo n.º 13
0
  /**
   * Returns calculated hash or Builds the request and calculates the hash
   *
   * @throws An exception if missing parameters or invalid parameters
   * @type {Hexadecimal64Length}
   * @readonly
   */
  get hash () {
    // Validate Symbol
    if (!this.symbol) throw new Error('Symbol is not set.')
    if (Utils.byteCount(this.symbol) > 8) throw new Error('Token Symbol - Invalid Size. Max Size 8 Bytes')
    if (!Utils.isAlphanumeric(this.symbol)) throw new Error('Token Symbol - Non-alphanumeric characters')

    // Validate Name
    if (!this.name) throw new Error('Name is not set.')
    if (Utils.byteCount(this.name) > 32) throw new Error('Token Name - Invalid Size. Max Size 32 Bytes')
    if (!Utils.isAlphanumericExtended(this.name)) throw new Error('Token Name - Non-alphanumeric characters')

    // Validate Total Supply
    if (!this.totalSupply) throw new Error('Total Supply is not set.')
    if (bigInt(this.totalSupply).gt(bigInt(Utils.MAXUINT128))) throw new Error(`Invalid Total Supply - Maximum supply is ${Utils.MAXUINT128}`)

    // Validate Fee Type
    if (!this.feeType) throw new Error('Fee Type is not set.')
    if (this.feeType !== 'flat' && this.feeType !== 'percentage') throw new Error('Token Fee Type - Invalid Fee Type use "flat" or "percentage"')

    // Validate Fee Rate
    if (!this.feeRate) throw new Error('Fee Rate is not set.')
    if (this.feeType === 'percentage' && bigInt(this.feeRate).greater(bigInt('100'))) throw new Error('Fee Type is percentage and exceeds the maximum of 100')

    // Validate Settings
    if (!this.settings) throw new Error('Settings is not set.')
    this.validateSettings()

    // Controllers are validated in the controller hash loop saves some time....
    if (!this.controllers) throw new Error('Controllers is not set.')

    // Validate Issuer Info
    if (this.issuerInfo === null) throw new Error('IssuerInfo is not set.')
    if (Utils.byteCount(this.issuerInfo) > 512) throw new Error('Issuer Info - Invalid Size. Max Size 512 Bytes')

    let context = super.hash()

    let tokenID = Utils.hexToUint8(this.tokenID)
    blake.blake2bUpdate(context, tokenID)

    let symbol = Utils.hexToUint8(Utils.stringToHex(this.symbol))
    blake.blake2bUpdate(context, symbol)

    let name = Utils.hexToUint8(Utils.stringToHex(this.name))
    blake.blake2bUpdate(context, name)

    let totalSupply = Utils.hexToUint8(Utils.decToHex(this.totalSupply, 16))
    blake.blake2bUpdate(context, totalSupply)

    let feeType = Utils.hexToUint8(Utils.decToHex(+(this.feeType === 'flat'), 1))
    blake.blake2bUpdate(context, feeType)

    let feeRate = Utils.hexToUint8(Utils.decToHex(this.feeRate, 16))
    blake.blake2bUpdate(context, feeRate)

    let settings = Utils.hexToUint8(Utils.changeEndianness(Utils.decToHex(parseInt(this.getObjectBits(this.settings), 2), 8)))
    blake.blake2bUpdate(context, settings)

    let accounts = []
    for (let controller of this.controllers) {
      this.validateController(controller)
      let account = Utils.hexToUint8(Utils.keyFromAccount(controller.account))
      if (accounts.includes(account)) throw new Error('Duplicate Controllers are not allowed')
      accounts.push(account)
      blake.blake2bUpdate(context, account)

      let privileges = Utils.hexToUint8(Utils.changeEndianness(Utils.decToHex(parseInt(this.getObjectBits(controller.privileges), 2), 8)))
      blake.blake2bUpdate(context, privileges)
    }

    let issuerInfo = Utils.hexToUint8(Utils.stringToHex(this.issuerInfo))
    blake.blake2bUpdate(context, issuerInfo)

    return Utils.uint8ToHex(blake.blake2bFinal(context))
  }