-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sign.go
45 lines (37 loc) · 1.22 KB
/
sign.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
package bitcoin
import (
"bytes"
"encoding/base64"
"github.com/bitcoinsv/bsvd/chaincfg/chainhash"
"github.com/bitcoinsv/bsvd/wire"
"github.com/libsv/go-bk/bec"
)
// SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding
// sigRefCompressedKey bool determines whether the signature will reference a compressed or uncompresed key
// Spec: https://docs.moneybutton.com/docs/bsv-message.html
func SignMessage(privateKey string, message string, sigRefCompressedKey bool) (string, error) {
if len(privateKey) == 0 {
return "", ErrPrivateKeyMissing
}
var buf bytes.Buffer
var err error
if err = wire.WriteVarString(&buf, 0, hBSV); err != nil {
return "", err
}
if err = wire.WriteVarString(&buf, 0, message); err != nil {
return "", err
}
// Create the hash
messageHash := chainhash.DoubleHashB(buf.Bytes())
// Get the private key
var ecdsaPrivateKey *bec.PrivateKey
if ecdsaPrivateKey, err = PrivateKeyFromString(privateKey); err != nil {
return "", err
}
// Sign
var sigBytes []byte
if sigBytes, err = bec.SignCompact(bec.S256(), ecdsaPrivateKey, messageHash, sigRefCompressedKey); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(sigBytes), nil
}