Browse Source

Add go main

golang
garritfra 6 years ago
parent
commit
7cc28f2b31
  1. 2
      .gitattributes
  2. 80
      .gitignore
  3. 12
      gulpfile.js
  4. 7
      main.go
  5. 8912
      package-lock.json
  6. 31
      package.json
  7. 30
      src/daemon/app.ts
  8. 59
      src/model/Block.ts
  9. 21
      src/model/Blockchain.ts
  10. 11
      src/model/Data.ts
  11. 4
      src/model/RequestType.ts
  12. 11
      src/model/Transaction.ts
  13. 9
      tsconfig.json

2
.gitattributes vendored

@ -1,2 +0,0 @@
# Auto detect text files and perform LF normalization
* text=auto

80
.gitignore vendored

@ -1,79 +1 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless
# FuseBox cache
.fusebox/
# build
dist/
blockchain-project

12
gulpfile.js

@ -1,12 +0,0 @@
const gulp = require("gulp");
const ts = require("gulp-typescript");
gulp.task("typescript", function() {
return gulp
.src("./src/**/*.ts")
.pipe(ts())
.pipe(gulp.dest("dist"));
});
gulp.task("build", ["typescript"]);
gulp.task("default", ["build"]);

7
main.go

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}

8912
package-lock.json generated

File diff suppressed because it is too large Load Diff

31
package.json

@ -1,31 +0,0 @@
{
"name": "blockchain-project",
"version": "1.0.0",
"description": "Blockchain",
"scripts": {
"start": "npm run build && node ./dist/app.js",
"build": "gulp build"
},
"author": "@garritfra",
"license": "ISC",
"dependencies": {
"@types/body-parser": "^1.17.0",
"@types/cors": "^2.8.4",
"@types/crypto-js": "^3.1.43",
"@types/express": "^4.16.0",
"@types/socket.io": "^2.1.0",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"crypto-js": "^3.1.9-1",
"express": "^4.16.4",
"gulp-typescript": "^5.0.0-alpha.3",
"parcel": "^1.10.3",
"parcel-bundler": "^1.10.3",
"socket.io": "^2.1.1",
"ts-node": "^7.0.1"
},
"devDependencies": {
"gulp": "^3.9.1",
"typescript": "^3.1.6"
}
}

30
src/daemon/app.ts

@ -1,30 +0,0 @@
import * as net from "net";
import Blockchain from "../model/Blockchain";
const blockchain = new Blockchain();
const server = net.createServer(c => {
//'connection' listener
console.log("client connected");
c.on("end", () => {
console.log("client disconnected");
});
c.on("data", data => {
try {
console.log(JSON.parse(data.toString()));
} catch (e) {
console.log("Request was not a JSON Object!");
}
});
c.write("Hi!");
c.pipe(c);
});
server.listen(42000, () => {
//'listening' listener
console.log("server bound");
});

59
src/model/Block.ts

@ -1,59 +0,0 @@
import { SHA256 } from "crypto-js";
import Transaction from "./Transaction";
import { Hash } from "crypto";
export default class Block {
id: number;
timestamp: Date;
data: Transaction[];
previousHash: string;
hash: string;
/**
* Construct a Block object
* @constructor
* @param {number} id
* @param {string} previousHash
*/
constructor(id: number, previousHash: string) {
this.id = id;
this.timestamp = new Date();
this.data = [];
this.previousHash = previousHash;
this.hash = this.calculateHash();
}
/**
* Concatenates the timestamp, previous hash and transaction data of a block and
* calculates the according SHA256 Hash
* @returns hash
*/
calculateHash() {
let hashStr = this.timestamp + this.previousHash + this.data;
let hash = SHA256(hashStr).toString();
return hash;
}
/**
* Creates the first block in a blockchain.
* It has a stub transaction with an amount of 0 coins.
*
* Called automatically upon blockchain creation
*
* @returns genesisBlock
*/
static createGenesisBlock() {
let date = new Date();
let genesisTransaction = new Transaction("Genesis", "Genesis", 0);
let genesisBlock = new Block(0, "0");
genesisBlock.addTransaction(genesisTransaction);
return genesisBlock;
}
/**
* Pushes a transaction to the Block
* @param {Transaction} transaction
*/
addTransaction(transaction: Transaction) {
this.data.push(transaction);
}
}

21
src/model/Blockchain.ts

@ -1,21 +0,0 @@
import Block from "./Block";
export default class Blockchain {
blocks: any[];
genesisBlock: Block;
constructor() {
this.blocks = [];
this.genesisBlock = Block.createGenesisBlock();
this.addBlock(this.genesisBlock);
}
addBlock(block: Block) {
this.blocks.push(block);
}
getLastBlock() {
let lastBlock = this.blocks[this.blocks.length - 1];
return lastBlock;
}
}

11
src/model/Data.ts

@ -1,11 +0,0 @@
import { RequestType } from "./RequestType";
export default class Data {
requestType: RequestType;
data: any;
constructor(requestType: RequestType, data: any) {
this.requestType = requestType;
this.data = data;
}
}

4
src/model/RequestType.ts

@ -1,4 +0,0 @@
export enum RequestType {
CREATE_BLOCK,
GET_BLOCKS
}

11
src/model/Transaction.ts

@ -1,11 +0,0 @@
export default class Transaction {
sender: string;
receiver: string;
amount: number;
constructor(sender: string, receiver: string, amount: number) {
this.sender = sender;
this.receiver = receiver;
this.amount = amount;
}
}

9
tsconfig.json

@ -1,9 +0,0 @@
{
"compilerOptions": {
"module": "system",
"sourceMap": true,
"noImplicitAny": true,
"rootDir": "src",
"moduleResolution": "node"
}
}
Loading…
Cancel
Save