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
Verifying my Blockstack ID is secured with the address 1DkrieQuUHrV4WZdNYqi1nCk7NVgwhh5V6 https://explorer.blockstack.org/address/1DkrieQuUHrV4WZdNYqi1nCk7NVgwhh5V6 |
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
// 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() |
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
// 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) |
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 |
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
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
// 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
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
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
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 { |
NewerOlder