-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccount_c.go
111 lines (97 loc) · 4.17 KB
/
account_c.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
99
100
101
102
103
104
105
106
107
108
109
110
111
package api
import (
"encoding/hex"
"errors"
account "github.com/MixinNetwork/mobilecoin-account"
)
// #cgo CFLAGS: -I${SRCDIR}/include
// #cgo darwin LDFLAGS: ${SRCDIR}/include/libmobilecoin.a -framework Security -framework Foundation
// #cgo linux LDFLAGS: ${SRCDIR}/include/libmobilecoin_linux.a -lm -ldl
// #include <stdio.h>
// #include <stdlib.h>
// #include <errno.h>
// #include "libmobilecoin.h"
import "C"
func MCAccountKeyGetSubAddressPrivateKeys(viewPrivateKeyStr, spendPrivateKeyStr string, index uint) (string, string, error) {
viewPrivateKey := account.HexToScalar(viewPrivateKeyStr)
view_private_key_buf := viewPrivateKey.Bytes()
view_private_key_bytes := C.CBytes(view_private_key_buf)
defer C.free(view_private_key_bytes)
view_private_key := &C.McBuffer{
buffer: (*C.uint8_t)(view_private_key_bytes),
len: C.size_t(len(view_private_key_buf)),
}
spendPrivateKey := account.HexToScalar(spendPrivateKeyStr)
spend_private_key_buf := spendPrivateKey.Bytes()
spend_private_key_bytes := C.CBytes(spend_private_key_buf)
defer C.free(spend_private_key_bytes)
spend_private_key := &C.McBuffer{
buffer: (*C.uint8_t)(spend_private_key_bytes),
len: C.size_t(len(spend_private_key_buf)),
}
out_subaddress_view_private_buf := make([]byte, 32)
out_subaddress_view_private_bytes := C.CBytes(out_subaddress_view_private_buf)
defer C.free(out_subaddress_view_private_bytes)
out_subaddress_view_private := &C.McMutableBuffer{
buffer: (*C.uint8_t)(out_subaddress_view_private_bytes),
len: C.size_t(len(out_subaddress_view_private_buf)),
}
out_subaddress_spend_private_buf := make([]byte, 32)
out_subaddress_spend_private_bytes := C.CBytes(out_subaddress_spend_private_buf)
defer C.free(out_subaddress_spend_private_bytes)
out_subaddress_spend_private := &C.McMutableBuffer{
buffer: (*C.uint8_t)(out_subaddress_spend_private_bytes),
len: C.size_t(len(out_subaddress_spend_private_buf)),
}
b, err := C.mc_account_key_get_subaddress_private_keys(view_private_key, spend_private_key, C.uint64_t(index), out_subaddress_view_private, out_subaddress_spend_private)
if err != nil {
return "", "", err
}
if !b {
return "", "", errors.New("invalid private key")
}
return hex.EncodeToString(C.GoBytes(out_subaddress_view_private_bytes, 32)), hex.EncodeToString(C.GoBytes(out_subaddress_spend_private_bytes, 32)), nil
}
func MCTxOutMatchesSubaddress(txOutTargetKeyStr, txOutPublicKeyStr, viewPrivateKeyStr, subaddressSpendPrivateKeyStr string) (bool, error) {
txOutTargetKey := account.HexToPoint(txOutTargetKeyStr)
tx_out_target_key_buf := txOutTargetKey.Bytes()
tx_out_target_key_bytes := C.CBytes(tx_out_target_key_buf)
defer C.free(tx_out_target_key_bytes)
tx_out_target_key := &C.McBuffer{
buffer: (*C.uint8_t)(tx_out_target_key_bytes),
len: C.size_t(len(tx_out_target_key_buf)),
}
txOutPublicKey := account.HexToScalar(txOutPublicKeyStr)
tx_out_public_key_buf := txOutPublicKey.Bytes()
tx_out_public_key_bytes := C.CBytes(tx_out_public_key_buf)
defer C.free(tx_out_public_key_bytes)
tx_out_public_key := &C.McBuffer{
buffer: (*C.uint8_t)(tx_out_public_key_bytes),
len: C.size_t(len(tx_out_public_key_buf)),
}
viewPrivateKey := account.HexToScalar(viewPrivateKeyStr)
view_private_key_buf := viewPrivateKey.Bytes()
view_private_key_bytes := C.CBytes(view_private_key_buf)
defer C.free(view_private_key_bytes)
view_private_key := &C.McBuffer{
buffer: (*C.uint8_t)(view_private_key_bytes),
len: C.size_t(len(view_private_key_buf)),
}
subaddressSpendPrivateKey := account.HexToScalar(subaddressSpendPrivateKeyStr)
subadress_spend_private_key_buf := subaddressSpendPrivateKey.Bytes()
subadress_spend_private_key_bytes := C.CBytes(subadress_spend_private_key_buf)
defer C.free(subadress_spend_private_key_bytes)
subadress_spend_private_key := &C.McBuffer{
buffer: (*C.uint8_t)(subadress_spend_private_key_bytes),
len: C.size_t(len(subadress_spend_private_key_buf)),
}
var result bool
b, err := C.mc_tx_out_matches_subaddress(tx_out_target_key, tx_out_public_key, view_private_key, subadress_spend_private_key, (*C.bool)(&result))
if err != nil {
return false, err
}
if !b {
return false, errors.New("invalid private key")
}
return result, nil
}