Browse Source

Introduce bigIntToBytes method to compact code

xno
codesoap 1 year ago
parent
commit
4c6fbf2a46
  1. 13
      address.go
  2. 24
      block.go
  3. 3
      send.go
  4. 4
      util.go

13
address.go

@ -27,12 +27,11 @@ func getAddress(privateKey *big.Int) (string, error) { @@ -27,12 +27,11 @@ func getAddress(privateKey *big.Int) (string, error) {
publicKey := derivePublicKey(privateKey)
base32PublicKey := base32Encode(publicKey)
publicKeyBytes := make([]byte, 32, 32)
publicKey.FillBytes(publicKeyBytes)
hasher, err := blake2b.New(5, nil)
if err != nil {
return "", err
}
publicKeyBytes := bigIntToBytes(publicKey, 32)
if _, err := hasher.Write(publicKeyBytes); err != nil {
return "", err
}
@ -46,19 +45,15 @@ func getAddress(privateKey *big.Int) (string, error) { @@ -46,19 +45,15 @@ func getAddress(privateKey *big.Int) (string, error) {
}
func getPrivateKey(seed *big.Int, index uint32) *big.Int {
seedBytes := make([]byte, 32, 32)
seed.FillBytes(seedBytes)
indexBytes := make([]byte, 4, 4)
big.NewInt(int64(index)).FillBytes(indexBytes)
seedBytes := bigIntToBytes(seed, 32)
indexBytes := bigIntToBytes(big.NewInt(int64(index)), 4)
in := append(seedBytes, indexBytes...)
privateKeyBytes := blake2b.Sum256(in)
return big.NewInt(0).SetBytes(privateKeyBytes[:])
}
func derivePublicKey(privateKey *big.Int) *big.Int {
privateKeyBytes := make([]byte, 32, 32)
privateKey.FillBytes(privateKeyBytes)
hashBytes := blake2b.Sum512(privateKeyBytes)
hashBytes := blake2b.Sum512(bigIntToBytes(privateKey, 32))
scalar := edwards25519.NewScalar().SetBytesWithClamping(hashBytes[:32])
publicKeyBytes := edwards25519.NewIdentityPoint().ScalarBaseMult(scalar).Bytes()
return big.NewInt(0).SetBytes(publicKeyBytes)

24
block.go

@ -39,13 +39,11 @@ func (b *block) sign(privateKey *big.Int) error { @@ -39,13 +39,11 @@ func (b *block) sign(privateKey *big.Int) error {
b.Hash = fmt.Sprintf("%064X", hash)
signature := make([]byte, 64, 64)
privateKeyBytes := make([]byte, 32, 32)
privateKey.FillBytes(privateKeyBytes)
h, err := blake2b.New512(nil)
if err != nil {
return err
}
h.Write(privateKeyBytes)
h.Write(bigIntToBytes(privateKey, 32))
var digest1, messageDigest, hramDigest [64]byte
h.Sum(digest1[:0])
@ -64,9 +62,7 @@ func (b *block) sign(privateKey *big.Int) error { @@ -64,9 +62,7 @@ func (b *block) sign(privateKey *big.Int) error {
h.Reset()
h.Write(encodedR[:])
publicKeyBytes := make([]byte, 32, 32)
publicKey.FillBytes(publicKeyBytes)
h.Write(publicKeyBytes)
h.Write(bigIntToBytes(publicKey, 32))
h.Write(hash)
h.Sum(hramDigest[:0])
@ -85,9 +81,7 @@ func (b *block) hash(publicKey *big.Int) ([]byte, error) { @@ -85,9 +81,7 @@ func (b *block) hash(publicKey *big.Int) ([]byte, error) {
msg[31] = 0x6 // block preamble
publicKeyBytes := make([]byte, 32, 32)
publicKey.FillBytes(publicKeyBytes)
copy(msg[32:64], publicKeyBytes)
copy(msg[32:64], bigIntToBytes(publicKey, 32))
previous, err := hex.DecodeString(b.Previous)
if err != nil {
@ -96,20 +90,16 @@ func (b *block) hash(publicKey *big.Int) ([]byte, error) { @@ -96,20 +90,16 @@ func (b *block) hash(publicKey *big.Int) ([]byte, error) {
copy(msg[64:96], previous)
representative, err := getPublicKeyFromAddress(b.Representative)
representativeBytes := make([]byte, 32, 32)
representative.FillBytes(representativeBytes)
if err != nil {
return []byte{}, err
}
copy(msg[96:128], representativeBytes)
copy(msg[96:128], bigIntToBytes(representative, 32))
balance, ok := big.NewInt(0).SetString(b.Balance, 10)
if !ok {
return []byte{}, fmt.Errorf("cannot parse '%s' as an integer", b.Balance)
}
balanceBytes := make([]byte, 16, 16)
balance.FillBytes(balanceBytes)
copy(msg[128:144], balanceBytes)
copy(msg[128:144], bigIntToBytes(balance, 16))
link, err := hex.DecodeString(b.Link)
if err != nil {
@ -125,9 +115,7 @@ func (b *block) addWork(workThreshold uint64, privateKey *big.Int) error { @@ -125,9 +115,7 @@ func (b *block) addWork(workThreshold uint64, privateKey *big.Int) error {
var hash string
if b.Previous == "0000000000000000000000000000000000000000000000000000000000000000" {
publicKey := derivePublicKey(privateKey)
publicKeyBytes := make([]byte, 32, 32)
publicKey.FillBytes(publicKeyBytes)
hash = fmt.Sprintf("%064X", publicKeyBytes)
hash = fmt.Sprintf("%064X", bigIntToBytes(publicKey, 32))
} else {
hash = b.Previous
}

3
send.go

@ -46,12 +46,11 @@ func sendFundsToAccount(info accountInfo, amount, recipient string, privateKey * @@ -46,12 +46,11 @@ func sendFundsToAccount(info accountInfo, amount, recipient string, privateKey *
if err != nil {
return err
}
recipientBytes := make([]byte, 32, 32)
recipientNumber, err := getPublicKeyFromAddress(recipient)
if err != nil {
return err
}
recipientNumber.FillBytes(recipientBytes)
recipientBytes := bigIntToBytes(recipientNumber, 32)
block := block{
Type: "state",
Account: address,

4
util.go

@ -90,6 +90,10 @@ func base32Decode(in string) (*big.Int, error) { @@ -90,6 +90,10 @@ func base32Decode(in string) (*big.Int, error) {
return out, nil
}
func bigIntToBytes(x *big.Int, n int) []byte {
return x.FillBytes(make([]byte, n, n))
}
func revertBytes(in []byte) []byte {
for i := 0; i < len(in)/2; i++ {
in[i], in[len(in)-1-i] = in[len(in)-1-i], in[i]

Loading…
Cancel
Save