-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogeconnect.go
98 lines (85 loc) · 3.17 KB
/
dogeconnect.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package dogeconnectgo
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
)
// SignPaymentRequest creates a signed ConnectEnvelope from a ConnectPayment.
func SignPaymentRequest(payment ConnectPayment, privKey []byte) (ConnectEnvelope, error) {
// Derive the public key from the private key.
priv, pub := btcec.PrivKeyFromBytes(privKey)
defer priv.Zero()
// Encode the ConnectPayment into JSON (encoded UTF-8 bytes)
payload, err := json.Marshal(&payment)
if err != nil {
return ConnectEnvelope{}, err
}
// Double-SHA256 the encoded JSON UTF-8 bytes.
hash1 := sha256.Sum256(payload)
hash := sha256.Sum256(hash1[:])
// BIP-340 Schnorr signature algorithm, sign using private key.
sig, err := schnorr.Sign(priv, hash[:])
if err != nil {
return ConnectEnvelope{}, err
}
// Envelope wraps the Base64-encoded JSON payload with hex-encoded "X-only Public Key"
// and hex-encoded BIP-340 signature.
env := ConnectEnvelope{
Version: EnvelopeVersion,
Payload: base64.StdEncoding.EncodeToString(payload), // Base64-encoded JSON payload
PubKey: hex.EncodeToString(pub.SerializeCompressed()[1:]), // BIP-340 X-only public key (32 bytes)
Signature: hex.EncodeToString(sig.Serialize()), // BIP-340 Schnorr signature (64 bytes)
}
return env, nil
}
// VerifyPaymentRequest decodes and verifies a signed ConnectPayment in a ConnectEnvelope.
// pubKeyHash is the `h` (hash) element from a valid DogeConnect URL.
func VerifyPaymentRequest(env ConnectEnvelope, pubKeyHash []byte) (ConnectPayment, error) {
if env.Version != EnvelopeVersion {
return ConnectPayment{}, fmt.Errorf("invalid envelope: wrong version")
}
pub, err := hex.DecodeString(env.PubKey)
if err != nil {
return ConnectPayment{}, fmt.Errorf("invalid envelope: malformed pubkey hex")
}
sig_b, err := hex.DecodeString(env.Signature)
if err != nil {
return ConnectPayment{}, fmt.Errorf("invalid envelope: malformed signature hex")
}
payload, err := base64.StdEncoding.DecodeString(env.Payload)
if err != nil {
return ConnectPayment{}, fmt.Errorf("invalid envelope: malformed base64 payload")
}
// SHA256 the public key.
pubSha := sha256.Sum256(pub)
if !bytes.Equal(pubKeyHash, pubSha[0:15]) {
return ConnectPayment{}, fmt.Errorf("invalid envelope: wrong public key")
}
// Double-SHA256 the encoded JSON payload bytes.
hash1 := sha256.Sum256(payload)
hash := sha256.Sum256(hash1[:])
// BIP-340 X-only pubkey (lift_x function)
pubkey, err := schnorr.ParsePubKey(pub)
if err != nil {
return ConnectPayment{}, fmt.Errorf("invalid envelope: not a valid pubkey")
}
// Verify the BIP-340 Schnorr signature.
sig, err := schnorr.ParseSignature(sig_b)
if err != nil {
return ConnectPayment{}, fmt.Errorf("invalid envelope: not a valid signature")
}
if !sig.Verify(hash[:], pubkey) {
return ConnectPayment{}, fmt.Errorf("invalid envelope: incorrect signature")
}
var payment ConnectPayment
json.Unmarshal(payload, &payment)
if payment.Type != PaymentRequestType {
return ConnectPayment{}, fmt.Errorf("bad envelope: not a payment request")
}
return payment, nil
}