Browse Source

Simplifly method signatures

This is achived by storing the public key in Blocks and by simplifying
the work threshold logic.
xno v1.3.0-gamma
codesoap 9 months ago
parent
commit
f1cf7803dc
  1. 2
      account.go
  2. 9
      account_info.go
  3. 49
      block.go
  4. 4
      cmd/atto/config.go
  5. 12
      cmd/atto/main.go

2
account.go

@ -124,7 +124,7 @@ func (a Account) verifyInfo(info AccountInfo, node string) error { @@ -124,7 +124,7 @@ func (a Account) verifyInfo(info AccountInfo, node string) error {
if info.Error != "" {
return fmt.Errorf("could not get block info: %s", info.Error)
}
if err = block.Contents.hash(info.PublicKey); err != nil {
if err = block.Contents.hash(); err != nil {
return err
}
if err = block.Contents.verifySignature(a); err == errInvalidSignature ||

9
account_info.go

@ -39,12 +39,13 @@ func (i *AccountInfo) Send(toAddr, amount string) (Block, error) { @@ -39,12 +39,13 @@ func (i *AccountInfo) Send(toAddr, amount string) (Block, error) {
Type: "state",
SubType: "send",
Account: i.Address,
PublicKey: i.PublicKey,
Previous: i.Frontier,
Representative: i.Representative,
Balance: balance.String(),
Link: fmt.Sprintf("%064X", recipientBytes),
}
err = block.hash(i.PublicKey)
err = block.hash()
i.Frontier = block.Hash
i.Balance = block.Balance
return block, err
@ -92,12 +93,13 @@ func (i *AccountInfo) Change(representative string) (Block, error) { @@ -92,12 +93,13 @@ func (i *AccountInfo) Change(representative string) (Block, error) {
Type: "state",
SubType: "change",
Account: i.Address,
PublicKey: i.PublicKey,
Previous: i.Frontier,
Representative: representative,
Balance: i.Balance,
Link: "0000000000000000000000000000000000000000000000000000000000000000",
}
err := block.hash(i.PublicKey)
err := block.hash()
i.Frontier = block.Hash
return block, err
}
@ -121,12 +123,13 @@ func (i *AccountInfo) Receive(pending Pending) (Block, error) { @@ -121,12 +123,13 @@ func (i *AccountInfo) Receive(pending Pending) (Block, error) {
Type: "state",
SubType: "receive",
Account: i.Address,
PublicKey: i.PublicKey,
Previous: i.Frontier,
Representative: i.Representative,
Balance: updatedBalance.String(),
Link: pending.Hash,
}
err := block.hash(i.PublicKey)
err := block.hash()
i.Frontier = block.Hash
i.Balance = block.Balance
return block, err

49
block.go

@ -21,17 +21,18 @@ var ErrWorkMissing = fmt.Errorf("work is missing") @@ -21,17 +21,18 @@ var ErrWorkMissing = fmt.Errorf("work is missing")
// Block represents a block in the block chain of an account.
type Block struct {
Type string `json:"type"`
SubType string `json:"-"`
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:"-"`
HashBytes []byte `json:"-"`
Type string `json:"type"`
SubType string `json:"-"`
Account string `json:"account"`
PublicKey *big.Int `json:"-"`
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:"-"`
HashBytes []byte `json:"-"`
}
type workGenerateResponse struct {
@ -40,8 +41,8 @@ type workGenerateResponse struct { @@ -40,8 +41,8 @@ type workGenerateResponse struct {
}
// Sign computes and sets the Signature of b.
func (b *Block) Sign(publicKey, privateKey *big.Int) error {
signature, err := sign(publicKey, privateKey, b.HashBytes)
func (b *Block) Sign(privateKey *big.Int) error {
signature, err := sign(b.PublicKey, privateKey, b.HashBytes)
if err != nil {
return err
}
@ -62,18 +63,22 @@ func (b *Block) verifySignature(a Account) (err error) { @@ -62,18 +63,22 @@ func (b *Block) verifySignature(a Account) (err error) {
// FetchWork uses the generate_work RPC on node to fetch and then set
// the Work of b.
func (b *Block) FetchWork(workThreshold uint64, publicKey *big.Int, node string) error {
func (b *Block) FetchWork(node string) error {
var hash string
if b.Previous == "0000000000000000000000000000000000000000000000000000000000000000" {
hash = fmt.Sprintf("%064X", bigIntToBytes(publicKey, 32))
hash = fmt.Sprintf("%064X", bigIntToBytes(b.PublicKey, 32))
} else {
hash = b.Previous
}
requestBody := fmt.Sprintf(`{`+
`"action": "work_generate",`+
`"hash": "%s",`+
`"difficulty": "%016x"`+
`}`, string(hash), workThreshold)
requestBody := fmt.Sprintf(`{"action":"work_generate", "hash":"%s"`, string(hash))
if b.SubType == "receive" {
// Receive blocks need less work, so lower the difficulity.
var receiveWorkThreshold uint64 = 0xfffffe0000000000
requestBody += fmt.Sprintf(`, "difficulty":"%016x"`, receiveWorkThreshold)
}
requestBody += `}`
responseBytes, err := doRPC(requestBody, node)
if err != nil {
return err
@ -91,14 +96,14 @@ func (b *Block) FetchWork(workThreshold uint64, publicKey *big.Int, node string) @@ -91,14 +96,14 @@ func (b *Block) FetchWork(workThreshold uint64, publicKey *big.Int, node string)
return nil
}
func (b *Block) hash(publicKey *big.Int) error {
func (b *Block) hash() error {
// See https://nanoo.tools/block for a reference.
msg := make([]byte, 176, 176)
msg[31] = 0x6 // block preamble
copy(msg[32:64], bigIntToBytes(publicKey, 32))
copy(msg[32:64], bigIntToBytes(b.PublicKey, 32))
previous, err := hex.DecodeString(b.Previous)
if err != nil {

4
cmd/atto/config.go

@ -10,8 +10,4 @@ var ( @@ -10,8 +10,4 @@ var (
node = "https://proxy.powernode.cc/proxy"
defaultRepresentative = "nano_18shbirtzhmkf7166h39nowj9c9zrpufeg75bkbyoobqwf1iu3srfm9eo3pz"
sendWorkThreshold uint64 = 0xfffffff800000000
changeWorkThreshold uint64 = 0xfffffff800000000
receiveWorkThreshold uint64 = 0xfffffe0000000000
)

12
cmd/atto/main.go

@ -149,10 +149,10 @@ func printBalance() error { @@ -149,10 +149,10 @@ func printBalance() error {
if err != nil {
return err
}
if err = block.Sign(account.PublicKey, privateKey); err != nil {
if err = block.Sign(privateKey); err != nil {
return err
}
if err = block.FetchWork(sendWorkThreshold, account.PublicKey, node); err != nil {
if err = block.FetchWork(node); err != nil {
return err
}
if err = block.Submit(node); err != nil {
@ -192,10 +192,10 @@ func changeRepresentative() error { @@ -192,10 +192,10 @@ func changeRepresentative() error {
if err != nil {
return err
}
if err = block.Sign(account.PublicKey, privateKey); err != nil {
if err = block.Sign(privateKey); err != nil {
return err
}
if err = block.FetchWork(sendWorkThreshold, account.PublicKey, node); err != nil {
if err = block.FetchWork(node); err != nil {
return err
}
if err = block.Submit(node); err != nil {
@ -233,10 +233,10 @@ func sendFunds() error { @@ -233,10 +233,10 @@ func sendFunds() error {
if err != nil {
return err
}
if err = block.Sign(account.PublicKey, privateKey); err != nil {
if err = block.Sign(privateKey); err != nil {
return err
}
if err = block.FetchWork(sendWorkThreshold, account.PublicKey, node); err != nil {
if err = block.FetchWork(node); err != nil {
return err
}
if err = block.Submit(node); err != nil {

Loading…
Cancel
Save