anonymous No title
No License JavaScript
2022年03月23日
Copy Clone
class BlockChain {

  constructor(nemesisHash, timestamp, message) {
    this.chain = [{
      height: 0,
      hash: nemesisHash,
      timestamp: 0,
      epochAjustment: timestamp,
      message: message,
    }];
    console.log("Start BlockChain");
    console.log(JSON.stringify(this.chain[0], null, 2));
  }

  static __init__() {
    const nemesisMessage = "hello block chain Symbol !!";
    const nemesisHashRow = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, nemesisMessage);
    const nemesisHash = nemesisHashRow.map(n => ("0" + ((n < 0) ? 256 + n : n).toString(16)).slice(-2)).join("").toUpperCase();
    const timestamp = new Date().getTime();
    return new BlockChain(nemesisHash, timestamp, nemesisMessage);
  }

  add() {
    const message = new Date().toLocaleString() + " Blockを追加しました";
    const lastBlock = this.chain[this.chain.length - 1];
    const epochAjustment = this.chain[0].epochAjustment;
    const date = new Date().getTime();
    if (this.hashSum() === true) {
      const hashRow = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, `${lastBlock.hash}${message}`);
      const hash = hashRow.map(n => ("0" + ((n < 0) ? 256 + n : n).toString(16)).slice(-2)).join("").toUpperCase();
      const block = { height: this.chain.length, hash, timestamp: date - epochAjustment, message, epochAjustment: undefined };
      console.log(JSON.stringify(block, null, 2))
      this.chain.push(block);
    } else {
      throw new Error("データの改竄があった為、停止しました");
    }
  }

  hashSum() {
    if (this.chain.length === 1) {
      return true;
    }
    for (let i = 1; i < this.chain.length; i++) {
      const prev = this.chain[i - 1];
      const current = this.chain[i];
      const currentHashRow = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, `${prev.hash}${current.message}`);
      const currentHash = currentHashRow.map(n => ("0" + ((n < 0) ? 256 + n : n).toString(16)).slice(-2)).join("").toUpperCase();
      if (currentHash !== current.hash) return false;
    }
    return true;
  }

}

function test() {
  const Chain = BlockChain.__init__();
  for (let i = 0; i < 50; i++) {
    Utilities.sleep(1000);
    Chain.add();
    // 改竄
    if(i === 10){
      console.log("データを改竄しました");
      Chain.chain[5].message = "改竄します";
    }
  }
}
class BlockChain {

  constructor(nemesisHash, timestamp, message) {
    this.chain = [{
      height: 0,
      hash: nemesisHash,
      timestamp: 0,
      epochAjustment: timestamp,
      message: message,
    }];
    console.log("Start BlockChain");
    console.log(JSON.stringify(this.chain[0], null, 2));
  }

  static __init__() {
    const nemesisMessage = "hello block chain Symbol !!";
    const nemesisHashRow = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, nemesisMessage);
    const nemesisHash = nemesisHashRow.map(n => ("0" + ((n < 0) ? 256 + n : n).toString(16)).slice(-2)).join("").toUpperCase();
    const timestamp = new Date().getTime();
    return new BlockChain(nemesisHash, timestamp, nemesisMessage);
  }

  add() {
    const message = new Date().toLocaleString() + " Blockを追加しました";
    const lastBlock = this.chain[this.chain.length - 1];
    const epochAjustment = this.chain[0].epochAjustment;
    const date = new Date().getTime();
    if (this.hashSum() === true) {
      const hashRow = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, `${lastBlock.hash}${message}`);
      const hash = hashRow.map(n => ("0" + ((n < 0) ? 256 + n : n).toString(16)).slice(-2)).join("").toUpperCase();
      const block = { height: this.chain.length, hash, timestamp: date - epochAjustment, message, epochAjustment: undefined };
      console.log(JSON.stringify(block, null, 2))
      this.chain.push(block);
    } else {
      throw new Error("データの改竄があった為、停止しました");
    }
  }

  hashSum() {
    if (this.chain.length === 1) {
      return true;
    }
    for (let i = 1; i < this.chain.length; i++) {
      const prev = this.chain[i - 1];
      const current = this.chain[i];
      const currentHashRow = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, `${prev.hash}${current.message}`);
      const currentHash = currentHashRow.map(n => ("0" + ((n < 0) ? 256 + n : n).toString(16)).slice(-2)).join("").toUpperCase();
      if (currentHash !== current.hash) return false;
    }
    return true;
  }

}

function test() {
  const Chain = BlockChain.__init__();
  for (let i = 0; i < 50; i++) {
    Utilities.sleep(1000);
    Chain.add();
    // 改竄
    if(i === 10){
      console.log("データを改竄しました");
      Chain.chain[5].message = "改竄します";
    }
  }
}
No one still commented. Please first comment.
Output