Conversation
Quick and dirty tool for generating address test vectors
First working version, still WIP
Getting there
cmd/testvectors/main.go
Outdated
There was a problem hiding this comment.
In the signing package we have signing.NewEdSigner() that already creates a random key:
sig, err := signing.NewEdSigner()
_ = sig.PrivateKey()
_ = sig.PublicKey()
cmd/testvectors/main.go
Outdated
There was a problem hiding this comment.
It is not a different format - signing.Pubkey is a wrapper around ed25519.Pubkey to add additional methods. The conversion to bytes and then ed25519.Pubkey isn't necessary since the later is a field in the wrapper:
edPubKeys := make([]ed25519.PublicKey, n)
edPubKeys[0] = pub.PublicKeyThe real problem is core.PublicKey why is it defined as types.Hash32? A key isn't a hash, and types.Hash32 is an array, not a slice so every conversion between a "real" key and this custom type requires a copy of the value.
This line:
core.PublicKey(types.BytesToHash(pub2.Bytes()))copies the key to an array and then the function using the core.PubicKey copies it back into a slice when using it 😕
|
Here's an initial set of vectors produced by the script: |
cmd/testvectors/main.go
Outdated
There was a problem hiding this comment.
Argument is unused.
Instead of passing the same key 4 times I suggest you just pass the *signing.EdSigner created with signing.NewEdSigner() and access the public and private key as needed with:
pub := edSigner.PublicKey() // type signing.PublicKey - a wrapper around `ed25519.PublicKey` that can be accessed with
edPub := pub.PublicKey
// core.PublicKey is a weird type that should be removed in the future -
// it requires copying the key twice from a slice to an array and back to be used
// which is inefficient - for now convert it from
corePub := types.BytesToHash(edSigner.PublicKey().PublicKey)
priv := edSigner.PrivateKey() // type signing.PrivateKey which is an alias to `ed25519.PrivateKey` so a cast should never be necessary| pubkeysCore []core.PublicKey, | ||
| pubkeysEd []ed25519.PublicKey, | ||
| privkeys []ed25519.PrivateKey, | ||
| m, n uint8, |
cmd/testvectors/main.go
Outdated
There was a problem hiding this comment.
See my other comment. I feel this could be simpler. getKey isn't needed at all if signing.EdSigner() is used instead:
| func generateKeys(n int) ([]signing.PublicKey, []core.PublicKey, []ed25519.PublicKey, []ed25519.PrivateKey) { | |
| // generate the required set of keypairs | |
| // frustratingly, we need the same list of pubkeys in multiple formats | |
| // https://github.com/spacemeshos/go-spacemesh/issues/6061 | |
| pubkeysSigning := make([]signing.PublicKey, n) | |
| pubkeysCore := make([]core.PublicKey, n) | |
| pubkeysEd := make([]ed25519.PublicKey, n) | |
| privkeys := make([]signing.PrivateKey, n) | |
| for i := 0; i < n; i++ { | |
| pubkeysEd[i], privkeys[i] = getKey() | |
| pubkeysCore[i] = types.BytesToHash(pubkeysEd[i]) | |
| pubkeysSigning[i] = signing.PublicKey{PublicKey: pubkeysEd[i]} | |
| } | |
| return pubkeysSigning, pubkeysCore, pubkeysEd, privkeys | |
| } | |
| func generateKeys(n int) []*signing.EdSigner { | |
| identities := make([]*signing.EdSigner, 0, n) | |
| for range n { | |
| identity, err := signing.NewEdSigner() | |
| log.With().Fatal("failed creating identity", log.Err(err)) | |
| identities = append(identities, identity) | |
| } | |
| return identities | |
| } |
Adds two scripts for generating test vectors: addresses, and transactions.