You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.6 KiB

1 year ago
package main
import (
"encoding/hex"
"encoding/json"
1 year ago
"fmt"
"math/big"
"golang.org/x/crypto/blake2b"
)
type block struct {
Type string `json:"type"`
Account string `json:"account"`
Previous string `json:"previous"`
Representative string `json:"representative"`
Balance string `json:"balance"`
Link string `json:"link"`
Signature string `json:"signature"`
Work string `json:"work"`
Hash string `json:"-"`
}
type workGenerateResponse struct {
Error string `json:"error"`
Work string `json:"work"`
1 year ago
}
func (b *block) sign(privateKey *big.Int) error {
publicKey := derivePublicKey(privateKey)
hash, err := b.hash(publicKey)
if err != nil {
return err
}
b.Hash = fmt.Sprintf("%064X", hash)
signature, err := sign(privateKey, hash)
1 year ago
if err != nil {
return err
}
b.Signature = fmt.Sprintf("%0128X", signature)
return nil
}
func (b *block) hash(publicKey *big.Int) ([]byte, error) {
// Look at https://nanoo.tools/block for a reference.
1 year ago
msg := make([]byte, 176, 176)
msg[31] = 0x6 // block preamble
copy(msg[32:64], bigIntToBytes(publicKey, 32))
1 year ago
previous, err := hex.DecodeString(b.Previous)
if err != nil {
return []byte{}, err
}
copy(msg[64:96], previous)
representative, err := getPublicKeyFromAddress(b.Representative)
if err != nil {
return []byte{}, err
}
copy(msg[96:128], bigIntToBytes(representative, 32))
1 year ago
balance, ok := big.NewInt(0).SetString(b.Balance, 10)
if !ok {
return []byte{}, fmt.Errorf("cannot parse '%s' as an integer", b.Balance)
}
copy(msg[128:144], bigIntToBytes(balance, 16))
1 year ago
link, err := hex.DecodeString(b.Link)
if err != nil {
return []byte{}, err
}
copy(msg[144:176], link)
hash := blake2b.Sum256(msg)
return hash[:], nil
}
func (b *block) addWork(workThreshold uint64, privateKey *big.Int) error {
var hash string
1 year ago
if b.Previous == "0000000000000000000000000000000000000000000000000000000000000000" {
publicKey := derivePublicKey(privateKey)
hash = fmt.Sprintf("%064X", bigIntToBytes(publicKey, 32))
1 year ago
} else {
hash = b.Previous
1 year ago
}
requestBody := fmt.Sprintf(`{`+
`"action": "work_generate",`+
`"hash": "%s",`+
`"difficulty": "%016x"`+
`}`, string(hash), workThreshold)
responseBytes, err := doRPC(requestBody)
1 year ago
if err != nil {
return err
1 year ago
}
var response workGenerateResponse
if err = json.Unmarshal(responseBytes, &response); err != nil {
return err
1 year ago
}
// Need to check pending.Error because of
// https://github.com/nanocurrency/nano-node/issues/1782.
if response.Error != "" {
return fmt.Errorf("could not get work for block: %s", response.Error)
1 year ago
}
b.Work = response.Work
return nil
1 year ago
}