-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtxout_c.go
173 lines (160 loc) · 6.04 KB
/
txout_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package api
import (
"encoding/hex"
"fmt"
"strconv"
"unsafe"
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"
type TxOutAmount struct {
Value uint64
TokenID uint64
}
func MCTxOutGetAmount(maskedAmountStr, maskedTokenIDStr string, version int64, publicKeyStr, viewPrivateKeyStr string) (*TxOutAmount, error) {
masked_amount, err := strconv.ParseUint(maskedAmountStr, 10, 64)
if err != nil {
return nil, err
}
masked_token_id_buf := account.HexToBytes(maskedTokenIDStr)
masked_token_id_bytes := C.CBytes(masked_token_id_buf)
defer C.free(masked_token_id_bytes)
masked_token_id := &C.McBuffer{
buffer: (*C.uint8_t)(masked_token_id_bytes),
len: C.size_t(len(masked_token_id_buf)),
}
tx_out_masked_amount := (*C.McTxOutMaskedAmount)(C.malloc(C.sizeof_McTxOutMaskedAmount))
defer C.free(unsafe.Pointer(tx_out_masked_amount))
tx_out_masked_amount.masked_value = C.uint64_t(masked_amount)
tx_out_masked_amount.masked_token_id = masked_token_id
tx_out_masked_amount.version = uint32(version)
publicKey := account.HexToPoint(publicKeyStr)
public_key_buf := publicKey.Bytes()
public_key_bytes := C.CBytes(public_key_buf)
defer C.free(public_key_bytes)
tx_out_public_key := &C.McBuffer{
buffer: (*C.uint8_t)(public_key_bytes),
len: C.size_t(len(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)),
}
out_amount := &C.McTxOutAmount{
value: C.uint64_t(0),
token_id: C.uint64_t(0),
}
var out_error *C.McError
b, err := C.mc_tx_out_get_amount(tx_out_masked_amount, tx_out_public_key, view_private_key, out_amount, &out_error)
if err != nil {
return nil, err
}
if !b && out_error != nil {
err = fmt.Errorf("mc_tx_out_get_amount failed: [%d] %s", out_error.error_code, C.GoString(out_error.error_description))
C.mc_error_free(out_error)
return nil, err
}
return &TxOutAmount{
Value: uint64(out_amount.value),
TokenID: uint64(out_amount.token_id),
}, nil
}
func MCTxOutReconstructCommitment(maskedAmountStr, maskedTokenIDStr string, version int64, publicKeyStr, viewPrivateKeyStr string) (string, error) {
masked_amount, err := strconv.ParseUint(maskedAmountStr, 10, 64)
if err != nil {
return "", err
}
masked_token_id_buf := account.HexToBytes(maskedTokenIDStr)
masked_token_id_bytes := C.CBytes(masked_token_id_buf)
defer C.free(masked_token_id_bytes)
masked_token_id := &C.McBuffer{
buffer: (*C.uint8_t)(masked_token_id_bytes),
len: C.size_t(len(masked_token_id_buf)),
}
tx_out_masked_amount := (*C.McTxOutMaskedAmount)(C.malloc(C.sizeof_McTxOutMaskedAmount))
defer C.free(unsafe.Pointer(tx_out_masked_amount))
tx_out_masked_amount.masked_value = C.uint64_t(masked_amount)
tx_out_masked_amount.masked_token_id = masked_token_id
tx_out_masked_amount.version = uint32(version)
publicKey := account.HexToPoint(publicKeyStr)
public_key_buf := publicKey.Bytes()
public_key_bytes := C.CBytes(public_key_buf)
defer C.free(public_key_bytes)
tx_out_public_key := &C.McBuffer{
buffer: (*C.uint8_t)(public_key_bytes),
len: C.size_t(len(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)),
}
out_commitment_buf := make([]byte, 32)
out_commitment_bytes := C.CBytes(out_commitment_buf)
defer C.free(out_commitment_bytes)
out_commitment := &C.McMutableBuffer{
buffer: (*C.uint8_t)(out_commitment_bytes),
len: C.size_t(len(out_commitment_buf)),
}
var out_error *C.McError
b, err := C.mc_tx_out_reconstruct_commitment(tx_out_masked_amount, tx_out_public_key, view_private_key, out_commitment, &out_error)
if err != nil {
return "", err
}
if !b && out_error != nil {
err = fmt.Errorf("mc_tx_out_reconstruct_commitment failed: [%d] %s", out_error.error_code, C.GoString(out_error.error_description))
C.mc_error_free(out_error)
return "", err
}
return hex.EncodeToString(C.GoBytes(out_commitment_bytes, 32)), nil
}
func McTxOutGetSharedSecret(publicKeyStr, viewPrivateKeyStr string) (string, error) {
publicKey := account.HexToPoint(publicKeyStr)
public_key_buf := publicKey.Bytes()
public_key_bytes := C.CBytes(public_key_buf)
defer C.free(public_key_bytes)
tx_out_public_key := &C.McBuffer{
buffer: (*C.uint8_t)(public_key_bytes),
len: C.size_t(len(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)),
}
out_shared_secret_buf := make([]byte, 32)
out_shared_secret_bytes := C.CBytes(out_shared_secret_buf)
defer C.free(out_shared_secret_bytes)
out_shared_secret := &C.McMutableBuffer{
buffer: (*C.uint8_t)(out_shared_secret_bytes),
len: C.size_t(len(out_shared_secret_buf)),
}
var out_error *C.McError
b, err := C.mc_tx_out_get_shared_secret(view_private_key, tx_out_public_key, out_shared_secret, &out_error)
if err != nil {
return "", err
}
if !b && out_error != nil {
err = fmt.Errorf("mc_tx_out_get_shared_secret failed: [%d] %s", out_error.error_code, C.GoString(out_error.error_description))
C.mc_error_free(out_error)
return "", err
}
return hex.EncodeToString(C.GoBytes(out_shared_secret_bytes, 32)), nil
}