Skip to content

Commit 2643e39

Browse files
Seongjae ChoiSeongjae Choi
authored andcommitted
Implement privkey to ecdsa key
1 parent 7fd9ab0 commit 2643e39

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

key/key.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package key
33
import (
44
"crypto/ecdsa"
55
"crypto/rand"
6+
"errors"
7+
"math/big"
68

79
"github.com/CodeChain-io/codechain-sdk-go/crypto"
810
"github.com/CodeChain-io/codechain-sdk-go/primitives"
@@ -61,3 +63,17 @@ func CreatePlatformAddress(key EcdsaKey, networkID string) (a primitives.Platfor
6163

6264
return primitives.PlatformAddressFromAccountID(hash, networkID)
6365
}
66+
67+
func ToECDSA(privateKey []byte) (*EcdsaKey, error) {
68+
priv := new(EcdsaKey)
69+
priv.PublicKey.Curve = crypto.S256()
70+
priv.D = new(big.Int).SetBytes(privateKey)
71+
72+
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(privateKey)
73+
74+
if priv.PublicKey.X == nil {
75+
return nil, errors.New("Invalid private key")
76+
}
77+
78+
return priv, nil
79+
}

0 commit comments

Comments
 (0)