-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for loading private key from hex string (#118)
* Add support for loading private key from hex string --------- Signed-off-by: Saurabh Singh <[email protected]>
- Loading branch information
1 parent
7230165
commit ed9af41
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ed25519 | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"encoding/hex" | ||
"fmt" | ||
) | ||
|
||
// NewPrivateKeyFromBytes creates an ED25519 PrivateKey from raw bytes | ||
func NewPrivateKeyFromBytes(key []byte) (PrivateKey, error) { | ||
// Check if the key size matches the expected ED25519 private key size | ||
if len(key) != ed25519.PrivateKeySize { | ||
return PrivateKey{}, fmt.Errorf("wrong key size: expected %v bytes, got %v bytes", ed25519.PrivateKeySize, len(key)) | ||
} | ||
|
||
privateKey := ed25519.PrivateKey(key) | ||
return PrivateKey{ | ||
key: privateKey, | ||
}, nil | ||
} | ||
|
||
// NewPrivateKeyFromHex creates an ED25519 PrivateKey from a hex string | ||
func NewPrivateKeyFromHex(key string) (PrivateKey, error) { | ||
// Validate hex string length (128 hex characters = 64 bytes for ED25519 private key) | ||
if len(key) != ed25519.PrivateKeySize*2 { | ||
return PrivateKey{}, fmt.Errorf("invalid hex string length: expected %v characters, got %v", ed25519.PrivateKeySize*2, len(key)) | ||
} | ||
|
||
b, err := hex.DecodeString(key) | ||
if err != nil { | ||
return PrivateKey{}, fmt.Errorf("failed to decode hex string: %v", err) | ||
} | ||
|
||
return NewPrivateKeyFromBytes(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package secp256k1 | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/decred/dcrd/dcrec/secp256k1/v4" | ||
) | ||
|
||
// NewPrivateKeyFromBytes creates a secp256k1 PrivateKey from raw bytes | ||
func NewPrivateKeyFromBytes(key []byte) (PrivateKey, error) { | ||
// Validate key length (32 bytes for secp256k1) | ||
if len(key) != 32 { | ||
return PrivateKey{}, fmt.Errorf("invalid private key length: expected 32 bytes, got %d", len(key)) | ||
} | ||
|
||
privateKey := secp256k1.PrivKeyFromBytes(key) | ||
|
||
return PrivateKey{ | ||
key: privateKey, | ||
}, nil | ||
} | ||
|
||
// NewPrivateKeyFromHex creates a secp256k1 PrivateKey from a hex string | ||
func NewPrivateKeyFromHex(key string) (PrivateKey, error) { | ||
// Validate hex string length (64 hex characters for secp256k1) | ||
if len(key) != 64 { | ||
return PrivateKey{}, fmt.Errorf("invalid private key hex length: expected 64 characters, got %d", len(key)) | ||
} | ||
|
||
// Decode the hex string into bytes | ||
b, err := hex.DecodeString(key) | ||
if err != nil { | ||
return PrivateKey{}, fmt.Errorf("failed to decode hex: %v", err) | ||
} | ||
|
||
return NewPrivateKeyFromBytes(b) | ||
} |