/
exchange.go
89 lines (83 loc) · 2.38 KB
/
exchange.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
package bitcoin
//----------------------------------------------------------------------
// This file is part of Gospel.
// Copyright (C) 2011-2023 Bernd Fix >Y<
//
// Gospel is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Gospel is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
//----------------------------------------------------------------------
import (
"bytes"
"errors"
"fmt"
)
// ExportPrivateKey returns a private key in SIPA format
func ExportPrivateKey(k *PrivateKey, testnet bool) string {
var exp []byte
if testnet {
exp = append(exp, 0xEF)
} else {
exp = append(exp, 0x80)
}
exp = append(exp, k.Bytes()...)
cs := Hash256(exp)
exp = append(exp, cs[:4]...)
return Base58Encode(exp)
}
// ImportPrivateKey imports a private key in SIPA format
func ImportPrivateKey(keydata string, testnet bool) (*PrivateKey, error) {
// decode and check data
data, err := Base58Decode(keydata)
if err != nil {
return nil, err
}
if testnet {
if data[0] != 0xEF {
msg := fmt.Sprintf("Invalid key version: %d (testnet)\n", int(data[0]))
return nil, errors.New(msg)
}
} else {
if data[0] != 0x80 {
msg := fmt.Sprintf("Invalid key version: %d\n", int(data[0]))
return nil, errors.New(msg)
}
}
// copy key data
var k, c []byte
if len(data) == 37 {
// uncompressed public key
k = data[1:33]
c = data[33:37]
} else if len(data) == 38 {
// compressed public key
k = data[1:34]
c = data[34:38]
if data[33] != 1 {
return nil, fmt.Errorf("invalid key compression indicator: %d", int(data[33]))
}
} else {
return nil, errors.New("invalid key format")
}
// recompute and verify checksum
var t []byte
t = append(t, data[0])
t = append(t, k...)
cs := Hash256(t)
if !bytes.Equal(c, cs[:4]) {
return nil, errors.New("invalid key data")
}
// return key
return PrivateKeyFromBytes(k)
}