-
Notifications
You must be signed in to change notification settings - Fork 41
/
message.go
70 lines (57 loc) · 1.83 KB
/
message.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
package dcrlibwallet
import (
"decred.org/dcrwallet/v2/errors"
w "decred.org/dcrwallet/v2/wallet"
"github.com/decred/dcrd/txscript/v4/stdaddr"
)
func (wallet *Wallet) SignMessage(passphrase []byte, address string, message string) ([]byte, error) {
err := wallet.UnlockWallet(passphrase)
if err != nil {
return nil, translateError(err)
}
defer wallet.LockWallet()
return wallet.signMessage(address, message)
}
func (wallet *Wallet) signMessage(address string, message string) ([]byte, error) {
addr, err := stdaddr.DecodeAddress(address, wallet.chainParams)
if err != nil {
return nil, translateError(err)
}
// Addresses must have an associated secp256k1 private key and therefore
// must be P2PK or P2PKH (P2SH is not allowed).
switch addr.(type) {
case *stdaddr.AddressPubKeyEcdsaSecp256k1V0:
case *stdaddr.AddressPubKeyHashEcdsaSecp256k1V0:
default:
return nil, errors.New(ErrInvalidAddress)
}
sig, err := wallet.Internal().SignMessage(wallet.shutdownContext(), message, addr)
if err != nil {
return nil, translateError(err)
}
return sig, nil
}
func (mw *MultiWallet) VerifyMessage(address string, message string, signatureBase64 string) (bool, error) {
var valid bool
addr, err := stdaddr.DecodeAddress(address, mw.chainParams)
if err != nil {
return false, translateError(err)
}
signature, err := DecodeBase64(signatureBase64)
if err != nil {
return false, err
}
// Addresses must have an associated secp256k1 private key and therefore
// must be P2PK or P2PKH (P2SH is not allowed).
switch addr.(type) {
case *stdaddr.AddressPubKeyEcdsaSecp256k1V0:
case *stdaddr.AddressPubKeyHashEcdsaSecp256k1V0:
default:
return false, errors.New(ErrInvalidAddress)
}
valid, err = w.VerifyMessage(message, addr, signature, mw.chainParams)
if err != nil {
return false, translateError(err)
}
return valid, nil
}