Skip to content

Instantly share code, notes, and snippets.

View gist:88c479bacbece14e2a48b161569bbf0e
Verifying my Blockstack ID is secured with the address 1DkrieQuUHrV4WZdNYqi1nCk7NVgwhh5V6 https://explorer.blockstack.org/address/1DkrieQuUHrV4WZdNYqi1nCk7NVgwhh5V6
@beber89
beber89 / node.go
Created Sep 24, 2019
mine-dapp-wasm/chainfabric/node/Connect
View node.go
// Connect connects node to remote server via TCP
func (nd *Node) Connect() bool {
// connect to this socket
nd.ws = js.Global().Get("WebSocket").New(fmt.Sprintf("ws://%s:%d/ws", nd.toIP, nd.toPort))
nd.ws.Call("addEventListener", "open", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return nil
}))
nd.ws.Call("addEventListener", "message", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
message := args[0].Get("data").String()
@beber89
beber89 / node.go
Created Sep 24, 2019
mine-dapp-wasm/chainfabric/node/struct
View node.go
// Node represents a user in the network
// toIP is IP address of server to connect to
// toPort is Port address of server to connect to
// response is value unassignable by user, only read
type Node struct {
toIP string
toPort uint16
response string
ws js.Value
newTransactionCallback func(string)
@beber89
beber89 / blockchain.go
Created Sep 24, 2019
miner-dapp-wasm/blockchain/blockchain
View blockchain.go
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
@beber89
beber89 / block.go
Created Sep 24, 2019
miner-dapp-wasm/blockchain/block/hashValid
View block.go
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
@beber89
beber89 / block.go
Created Sep 24, 2019
miner-dapp-wasm/blockchain/block/doHash
View block.go
func (blk *Block) doHash() {
type toBeHashed struct {
id uint64
lastBlockHash [32]byte
nonce uint64
payload Transaction
difficulty uint8
timestamp uint64
}
h := sha256.New()
@beber89
beber89 / block.go
Last active Sep 24, 2019
miner-dapp-wasm/blockchain/block/struct
View block.go
// 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
}
@beber89
beber89 / transaction.go
Last active Sep 24, 2019
miner-dapp-wasm/blockchain/transaction
View transaction.go
type Transaction struct {
From string `json:"from"`
To string `json:"to"`
Amount float64 `json:"amount"`
}
@beber89
beber89 / main.go
Last active Sep 17, 2019
miner-dapp-wasm/main.go
View main.go
var wg sync.WaitGroup
func main() {
wg.Add(1)
web.RegisterCallbacks()
wg.Wait()
}
@beber89
beber89 / wallet.go
Created Sep 17, 2019
blockchain/wallet.go
View wallet.go
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 {