Make identity accept nil keys. Add createRandomIdentity helper
@@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/loomnetwork/go-loom"
"github.com/loomnetwork/go-loom/auth"
+ "golang.org/x/crypto/ed25519"
)
type Identity struct {
@@ -21,15 +22,28 @@ type Identity struct {
LoomAddr loom.Address
}
+func CreateRandomIdentity(chainID string) (*Identity, error) {
+ return CreateIdentity(nil, nil, chainID)
+}
+
func CreateIdentity(hexKeyOrECDSA interface{}, signerOrKey interface{}, chainID string) (*Identity, error) {
var mainnetPrivKey *ecdsa.PrivateKey
var signer auth.Signer
var err error
+ var loomAddr loom.Address
+ var ethAddr common.Address
// Convert hex key to crypto.ECDSA key
- switch signerOrKey.(type) {
+ if hexKeyOrECDSA == nil {
+ mainnetPrivKey, err = crypto.GenerateKey()
+ if err != nil {
+ return nil, err
+ }
+ } else {
+ switch hexKeyOrECDSA.(type) {
case *ecdsa.PrivateKey:
mainnetPrivKey = hexKeyOrECDSA.(*ecdsa.PrivateKey)
+ break
case string:
mainnetPrivKey, err = crypto.HexToECDSA(strings.TrimPrefix(hexKeyOrECDSA.(string), "0x"))
if err != nil {
@@ -39,34 +53,46 @@ func CreateIdentity(hexKeyOrECDSA interface{}, signerOrKey interface{}, chainID
default:
return nil, errors.New("Invalid mainnet key/signer type")
}
+ }
+ ethAddr = crypto.PubkeyToAddress(mainnetPrivKey.PublicKey)
- // Convert dappchain key to crypto.ECDSA key
+ // Convert dappchain key to signer
+ if signerOrKey == nil {
+ _, priv, err := ed25519.GenerateKey(nil)
+ if err != nil {
+ return nil, err
+ }
+ signer = auth.NewEd25519Signer(priv)
+ if err != nil {
+ return nil, err
+ }
+ } else {
switch signerOrKey.(type) {
case auth.Signer:
signer = signerOrKey.(auth.Signer)
+ break
case string:
privKey, err := base64.StdEncoding.DecodeString(signerOrKey.(string))
if err != nil {
return nil, err
}
-
signer = auth.NewEd25519Signer(privKey)
- if err != nil {
- return nil, err
- }
+
break
default:
return nil, errors.New("Invalid dappchain key/signer type")
}
+ }
+ loomAddr = loom.Address{
+ ChainID: chainID,
+ Local: loom.LocalAddressFromPublicKey(signer.PublicKey()),
+ }
identity := &Identity{
MainnetPrivKey: mainnetPrivKey,
- MainnetAddr: crypto.PubkeyToAddress(mainnetPrivKey.PublicKey),
+ MainnetAddr: ethAddr,
LoomSigner: signer,
- LoomAddr: loom.Address{
- ChainID: chainID,
- Local: loom.LocalAddressFromPublicKey(signer.PublicKey()),
- },
+ LoomAddr: loomAddr,
}
return identity, nil
}
