mirror of
https://github.com/arnaucube/go-blindsecp256k1.git
synced 2026-02-07 11:36:42 +01:00
15
parsers.go
15
parsers.go
@@ -4,6 +4,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// swapEndianness swaps the order of the bytes in the slice.
|
// swapEndianness swaps the order of the bytes in the slice.
|
||||||
@@ -108,6 +110,19 @@ func NewPublicKeyFromBytes(b []byte) (*PublicKey, error) {
|
|||||||
return &pk, nil
|
return &pk, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPublicKeyFromECDSA returns a *PublicKey from a serialized/marshaled array
|
||||||
|
// of bytes generated by the ethereum/standard ECDSA PubKey implementation.
|
||||||
|
func NewPublicKeyFromECDSA(b []byte) (*PublicKey, error) {
|
||||||
|
pub, err := crypto.UnmarshalPubkey(b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pk := new(PublicKey)
|
||||||
|
pk.X = pub.X
|
||||||
|
pk.Y = pub.Y
|
||||||
|
return pk, nil
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json marshaler for the Signature
|
// MarshalJSON implements the json marshaler for the Signature
|
||||||
func (sig Signature) MarshalJSON() ([]byte, error) {
|
func (sig Signature) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(&struct {
|
return json.Marshal(&struct {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -92,3 +93,16 @@ func TestBytes(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, &sig, sig2)
|
assert.Equal(t, &sig, sig2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImportECDSApubKey(t *testing.T) {
|
||||||
|
// Generate an ECDSA key
|
||||||
|
k, err := crypto.GenerateKey()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// Import the ECDSA Public key bytes into a PublicKey type
|
||||||
|
pk, err := NewPublicKeyFromECDSA(crypto.FromECDSAPub(&k.PublicKey))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
// Set the ECDSA Private key point as a blindsecp256k1 PrivateKey type
|
||||||
|
bk := PrivateKey(*k.D)
|
||||||
|
// Compare both public keys
|
||||||
|
assert.Equal(t, bk.Public().Bytes(), pk.Bytes())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user