-
Notifications
You must be signed in to change notification settings - Fork 51
/
ECDSA.purs
73 lines (64 loc) · 2.32 KB
/
ECDSA.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- | A module that implements crypto primitives that match CIP-49 SECP256k1
-- | ECDSA spec.
module Contract.Crypto.Secp256k1.ECDSA
( verifyEcdsaSecp256k1Signature
, signEcdsaSecp256k1
, deriveEcdsaSecp256k1PublicKey
, module X
, mkECDSAPublicKey
, unECDSAPublicKey
, mkMessageHash
, unMessageHash
) where
import Prelude
import Contract.Crypto.Secp256k1 (Secp256k1PrivateKey)
import Data.ByteArray (ByteArray)
import Data.Maybe (Maybe)
import Data.Newtype (unwrap)
import Effect.Aff (Aff)
import Noble.Secp256k1.ECDSA (ECDSAPublicKey, ECDSASignature, MessageHash) as X
import Noble.Secp256k1.ECDSA
( ECDSAPublicKey
, ECDSASignature
, MessageHash
, signECDSA
, verifyECDSA
)
import Noble.Secp256k1.ECDSA
( getECDSAPublicKey
, mkECDSAPublicKey
, mkMessageHash
, unECDSAPublicKey
, unMessageHash
) as ECDSA
-- | Verify ECDSA signature for a message hash.
-- |
-- | This function matches CIP-49 definition:
-- |
-- | https://github.com/cardano-foundation/CIPs/blob/master/CIP-0049/README.md
verifyEcdsaSecp256k1Signature
:: ECDSAPublicKey -> MessageHash -> ECDSASignature -> Boolean
verifyEcdsaSecp256k1Signature publicKey messageHash signature =
verifyECDSA signature messageHash publicKey
-- | Sign a message hash with a private key, producing a signature compatible
-- | with `verifyEcdsaSecp256k1Signature`.
signEcdsaSecp256k1 :: Secp256k1PrivateKey -> MessageHash -> Aff ECDSASignature
signEcdsaSecp256k1 privateKey messageHash =
signECDSA messageHash (unwrap privateKey) false
-- | Derive a public key from a private key. Uses `SECP256K1_EC_COMPRESSED`
-- | format (compatible with CIP-49).
deriveEcdsaSecp256k1PublicKey :: Secp256k1PrivateKey -> ECDSAPublicKey
deriveEcdsaSecp256k1PublicKey = unwrap >>> flip ECDSA.getECDSAPublicKey true
-- | Construct a public key from its byte representation.
mkECDSAPublicKey :: ByteArray -> Maybe ECDSAPublicKey
mkECDSAPublicKey = ECDSA.mkECDSAPublicKey
unECDSAPublicKey :: ECDSAPublicKey -> ByteArray
unECDSAPublicKey = ECDSA.unECDSAPublicKey
-- | Construct a message hash from its byte representation.
-- |
-- | This function **DOES NOT** compute the hash.
-- | Use `Contract.Crypto.Utils.hashMessageSha256` for that.
mkMessageHash :: ByteArray -> Maybe MessageHash
mkMessageHash = ECDSA.mkMessageHash
unMessageHash :: MessageHash -> ByteArray
unMessageHash = ECDSA.unMessageHash