aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorTaylor Gerring <taylor.gerring@gmail.com>2015-03-27 05:42:46 +0800
committerTaylor Gerring <taylor.gerring@gmail.com>2015-03-27 05:42:46 +0800
commit6bd1f6cc49acd459e61559e5af515da2db2481e5 (patch)
tree3e4f7a51ca8e3cb03d24fbe1898578d88fd7456c /crypto
parentbb12dbe233db2e064715b329b7ba987c76ba3bfa (diff)
parentb0b0939879b9fb8453ec1c8fa2ceb522e56df3bc (diff)
downloaddexon-6bd1f6cc49acd459e61559e5af515da2db2481e5.tar.gz
dexon-6bd1f6cc49acd459e61559e5af515da2db2481e5.tar.zst
dexon-6bd1f6cc49acd459e61559e5af515da2db2481e5.zip
Merge remote-tracking branch 'origin' into rpcargs
Conflicts: rpc/args.go
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto.go7
-rw-r--r--crypto/key.go18
2 files changed, 18 insertions, 7 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index c3d47b629..442942c6c 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -9,6 +9,7 @@ import (
"crypto/sha256"
"fmt"
"io"
+ "io/ioutil"
"os"
"encoding/hex"
@@ -139,6 +140,12 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
return ToECDSA(buf), nil
}
+// SaveECDSA saves a secp256k1 private key to the given file with restrictive
+// permissions
+func SaveECDSA(file string, key *ecdsa.PrivateKey) error {
+ return ioutil.WriteFile(file, FromECDSA(key), 0600)
+}
+
func GenerateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(S256(), rand.Reader)
}
diff --git a/crypto/key.go b/crypto/key.go
index 9dbf37467..0b84bfec1 100644
--- a/crypto/key.go
+++ b/crypto/key.go
@@ -85,6 +85,16 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) {
return err
}
+func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {
+ id := uuid.NewRandom()
+ key := &Key{
+ Id: id,
+ Address: PubkeyToAddress(privateKeyECDSA.PublicKey),
+ PrivateKey: privateKeyECDSA,
+ }
+ return key
+}
+
func NewKey(rand io.Reader) *Key {
randBytes := make([]byte, 64)
_, err := rand.Read(randBytes)
@@ -97,11 +107,5 @@ func NewKey(rand io.Reader) *Key {
panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
}
- id := uuid.NewRandom()
- key := &Key{
- Id: id,
- Address: PubkeyToAddress(privateKeyECDSA.PublicKey),
- PrivateKey: privateKeyECDSA,
- }
- return key
+ return NewKeyFromECDSA(privateKeyECDSA)
}