Browse Source

Expose Hash Method on Block

xno
codesoap 8 months ago
parent
commit
8576a76d31
  1. 4
      account.go
  2. 12
      account_info.go
  3. 16
      block.go

4
account.go

@ -124,12 +124,12 @@ func (a Account) verifyInfo(info AccountInfo, node string) error { @@ -124,12 +124,12 @@ func (a Account) verifyInfo(info AccountInfo, node string) error {
if info.Error != "" {
return fmt.Errorf("could not get block info: %s", info.Error)
}
hash, err := block.Contents.hash()
hash, err := block.Contents.Hash()
if err != nil {
return err
}
if err = block.Contents.verifySignature(a); err == errInvalidSignature ||
info.Frontier != fmt.Sprintf("%064X", hash) ||
info.Frontier != hash ||
info.Representative != block.Contents.Representative ||
info.Balance != block.Contents.Balance {
return ErrAccountManipulated

12
account_info.go

@ -44,11 +44,11 @@ func (i *AccountInfo) Send(amount, toAddr string) (Block, error) { @@ -44,11 +44,11 @@ func (i *AccountInfo) Send(amount, toAddr string) (Block, error) {
Balance: balance.String(),
Link: fmt.Sprintf("%064X", recipientBytes),
}
hash, err := block.hash()
hash, err := block.Hash()
if err != nil {
return Block{}, err
}
i.Frontier = fmt.Sprintf("%064X", hash)
i.Frontier = hash
i.Balance = block.Balance
return block, err
}
@ -100,11 +100,11 @@ func (i *AccountInfo) Change(representative string) (Block, error) { @@ -100,11 +100,11 @@ func (i *AccountInfo) Change(representative string) (Block, error) {
Balance: i.Balance,
Link: "0000000000000000000000000000000000000000000000000000000000000000",
}
hash, err := block.hash()
hash, err := block.Hash()
if err != nil {
return Block{}, err
}
i.Frontier = fmt.Sprintf("%064X", hash)
i.Frontier = hash
return block, err
}
@ -136,11 +136,11 @@ func (i *AccountInfo) Receive(pending Pending) (Block, error) { @@ -136,11 +136,11 @@ func (i *AccountInfo) Receive(pending Pending) (Block, error) {
Balance: updatedBalance.String(),
Link: pending.Hash,
}
hash, err := block.hash()
hash, err := block.Hash()
if err != nil {
return Block{}, err
}
i.Frontier = fmt.Sprintf("%064X", hash)
i.Frontier = hash
i.Balance = block.Balance
return block, err
}

16
block.go

@ -55,7 +55,7 @@ func (b *Block) Sign(privateKey *big.Int) error { @@ -55,7 +55,7 @@ func (b *Block) Sign(privateKey *big.Int) error {
if err != nil {
return err
}
hash, err := b.hash()
hash, err := b.hashBytes()
if err != nil {
return err
}
@ -72,7 +72,7 @@ func (b *Block) verifySignature(a Account) (err error) { @@ -72,7 +72,7 @@ func (b *Block) verifySignature(a Account) (err error) {
if !ok {
return fmt.Errorf("cannot parse '%s' as an integer", b.Signature)
}
hash, err := b.hash()
hash, err := b.hashBytes()
if err != nil {
return err
}
@ -121,7 +121,17 @@ func (b *Block) FetchWork(node string) error { @@ -121,7 +121,17 @@ func (b *Block) FetchWork(node string) error {
return nil
}
func (b *Block) hash() ([]byte, error) {
// Hash calculates the block's hash and returns it's string
// representation.
func (b Block) Hash() (string, error) {
hashBytes, err := b.hashBytes()
if err != nil {
return "", err
}
return fmt.Sprintf("%064X", hashBytes), nil
}
func (b Block) hashBytes() ([]byte, error) {
// See https://nanoo.tools/block for a reference.
msg := make([]byte, 176, 176)

Loading…
Cancel
Save