Skip to content
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions internal/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"crypto/ecdsa"
"crypto/rand"

"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"reflect"



"github.com/SIGBlockchain/project_aurum/internal/hashing"
"github.com/SIGBlockchain/project_aurum/internal/publickey"
)
Expand Down Expand Up @@ -55,7 +57,7 @@ func New(version uint16, sender *ecdsa.PrivateKey, recipient []byte, value uint6

if version == 0 {
return nil, errors.New("Invalid version; must be >= 1")
}
}

c := Contract{
Version: version,
Expand Down Expand Up @@ -274,4 +276,34 @@ func (mc *JSONContract) Unmarshal() (Contract, error) {
mc.StateNonce,
}
return c, nil

}



func GenerateRandomContract() (*Contract, error){
b := make([]byte, 32)
_, err :=rand.Read(b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will this return a non-nil error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The read function from rand is a helper function that uses io.readfull function from. The readFull function reads the length of the bytes and returns an error if fewer bytes were read. So, if for some reason not all the bytes were read, it would return an error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no identifiable reason that all the bytes will be read, then ignore the error. I don't want to approve a generate random contract function that returns any errors because that is impractical.


genRecipPubKeyHash := b
genVersion := binary.LittleEndian.Uint16(b[0:])
genValue := binary.LittleEndian.Uint64(b[1:])
genStateNonce := binary.LittleEndian.Uint64(b[2:])
//checks if rand.Read return nil. if err equals nil
if err != nil {
fmt.Println("error:", err)
return nil, err
}

c:= &Contract{
RecipPubKeyHash: genRecipPubKeyHash,
Version: genVersion,
Value: genValue,
StateNonce: genStateNonce,
}
return c, nil

}