This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func RegisterCallbacks() { | |
js.Global().Set("reward", js.FuncOf(reward)) | |
js.Global().Set("buyCommodity", js.FuncOf(buyCommodity)) | |
js.Global().Set("networth", js.FuncOf(networth)) | |
js.Global().Set("changeUser", js.FuncOf(changeUser)) | |
js.Global().Set("sendSliderValToWasm", js.FuncOf(sendSliderValToWasm)) | |
} | |
var ( | |
merchant string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" type="text/css" href="styles.css" media="screen" /> | |
<script src="wasm_exec.js"></script> | |
<script> | |
const go = new Go(); | |
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {go.run(result.instance);}); | |
</script> | |
<script src="https://code.jquery.com/jquery-3.4.1.min.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// renderPage reloads elements of the page when change to wallet occurs | |
func RenderPage() { | |
wlt := *GetWallet() | |
document := js.Global().Get("document") | |
chainView := (*wlt.GetBlockchain()).Draw() | |
if chn := document.Call("getElementById", "chain"); chn != js.Null() { | |
document.Call("getElementById", "blockchain-placeholder").Call("removeChild", chn) | |
} | |
document.Call("getElementById", "blockchain-placeholder").Call("appendChild", chainView) | |
networthLabel := document.Call("getElementById", "networth") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var NotifyListeners func() | |
// Wallet is the interface struct to the blockchain for the user | |
type Wallet struct { | |
username string | |
chain *Blockchain | |
} | |
// NewWallet creates a Wallet struct | |
func NewWallet(username string) Wallet { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var wg sync.WaitGroup | |
func main() { | |
wg.Add(1) | |
web.RegisterCallbacks() | |
wg.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Transaction struct { | |
From string `json:"from"` | |
To string `json:"to"` | |
Amount float64 `json:"amount"` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Block is the building entity for the blockchain | |
type Block struct { | |
id uint64 | |
lastBlockHash [32]byte | |
nonce uint64 | |
payload Transaction | |
difficulty uint8 | |
timestamp uint64 | |
hash [32]byte | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (blk *Block) doHash() { | |
type toBeHashed struct { | |
id uint64 | |
lastBlockHash [32]byte | |
nonce uint64 | |
payload Transaction | |
difficulty uint8 | |
timestamp uint64 | |
} | |
h := sha256.New() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (blk *Block) hashValid() bool { | |
hash := blk.hash | |
// put in mind Little Endian | |
// converting the 8 most significant bytes of hash to one number int | |
hashAsInt := uint64(0) | |
for i := uint8(1); i <= uint8(8); i++ { | |
hashAsInt = uint64(hash[32-i])<<((8-i)*8) + hashAsInt | |
} | |
diff := (uint64(1)<<63)>>(blk.difficulty-1) - 1 | |
return hashAsInt <= diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Blockchain struct { | |
chain []Block | |
} | |
func listener(blkchn *Blockchain, msg string) { | |
// mining happen creating new block | |
// string payload shall be converted to transaction struct | |
var trMsg TransactionMessage | |
trMsg.Deserialize(msg) | |
var blk Block |
OlderNewer