Browse Source

Move seed generation into library

xno
codesoap 8 months ago
parent
commit
26f7f1d6f7
  1. 30
      cmd/atto/main.go
  2. 13
      cmd/atto/util.go
  3. 18
      util.go

30
cmd/atto/main.go

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
package main
import (
"crypto/rand"
"flag"
"fmt"
"math/big"
@ -95,12 +94,11 @@ func main() { @@ -95,12 +94,11 @@ func main() {
}
func printNewSeed() error {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return err
seed, err := atto.GenerateSeed()
if err == nil {
fmt.Println(seed)
}
fmt.Printf("%X\n", b)
return nil
return err
}
func printAddress() error {
@ -108,7 +106,10 @@ func printAddress() error { @@ -108,7 +106,10 @@ func printAddress() error {
if err != nil {
return err
}
privateKey := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err == nil {
fmt.Println(account.Address)
@ -121,7 +122,10 @@ func printBalance() error { @@ -121,7 +122,10 @@ func printBalance() error {
if err != nil {
return err
}
privateKey := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err != nil {
return err
@ -170,7 +174,10 @@ func changeRepresentative() error { @@ -170,7 +174,10 @@ func changeRepresentative() error {
if err != nil {
return err
}
privateKey := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err != nil {
return err
@ -205,7 +212,10 @@ func sendFunds() error { @@ -205,7 +212,10 @@ func sendFunds() error {
if err != nil {
return err
}
privateKey := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err != nil {
return err

13
cmd/atto/util.go

@ -9,19 +9,14 @@ import ( @@ -9,19 +9,14 @@ import (
"strings"
)
// getSeed takes the first line of the standard input and interprets it
// as a hexadecimal representation of a 32byte seed.
func getSeed() (*big.Int, error) {
// getSeed returns the first line of the standard input.
func getSeed() (string, error) {
in := bufio.NewReader(os.Stdin)
firstLine, err := in.ReadString('\n')
if err != nil {
return nil, err
return "", err
}
seed, ok := big.NewInt(0).SetString(strings.TrimSpace(firstLine), 16)
if !ok {
return nil, fmt.Errorf("could not parse seed")
}
return seed, nil
return strings.TrimSpace(firstLine), nil
}
func rawToNanoString(raw *big.Int) string {

18
util.go

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
package atto
import (
"crypto/rand"
"fmt"
"io/ioutil"
"math/big"
@ -10,13 +11,24 @@ import ( @@ -10,13 +11,24 @@ import (
"golang.org/x/crypto/blake2b"
)
// GenerateSeed generates a new random seed.
func GenerateSeed() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
return fmt.Sprintf("%X\n", b), err
}
// NewPrivateKey creates a private key from the given seed and index.
func NewPrivateKey(seed *big.Int, index uint32) *big.Int {
seedBytes := bigIntToBytes(seed, 32)
func NewPrivateKey(seed string, index uint32) (*big.Int, error) {
seedInt, ok := big.NewInt(0).SetString(seed, 16)
if !ok {
return nil, fmt.Errorf("could not parse seed")
}
seedBytes := bigIntToBytes(seedInt, 32)
indexBytes := bigIntToBytes(big.NewInt(int64(index)), 4)
in := append(seedBytes, indexBytes...)
privateKeyBytes := blake2b.Sum256(in)
return big.NewInt(0).SetBytes(privateKeyBytes[:])
return big.NewInt(0).SetBytes(privateKeyBytes[:]), nil
}
func base32Encode(in *big.Int) string {

Loading…
Cancel
Save