-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.go
200 lines (175 loc) · 5.02 KB
/
functions.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
/*
#cgo CFLAGS: -I"/usr/include/postgresql/16/server" -fpic
#cgo LDFLAGS: -shared
#include "postgres.h"
#include "fmgr.h"
#include "pgtime.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/timestamp.h"
#include "utils/array.h"
#include "utils/elog.h"
#include "executor/spi.h"
#include "parser/parse_type.h"
#include "commands/trigger.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/lsyscache.h"
#include "utils/jsonb.h"
#include "funcapi.h"
// Declarations of auxiliary functions
//
// These functions are defined in the .c file.
//
// We need these auxiliary wrappers to overcome the CGO limitation of not being able to reference macros.
// These macros are wrapped within functions in order to be able to access the functionality.
void elog_notice(char* string);
void elog_error(char* string);
*/
import "C"
import (
"bytes"
"crypto/sha512"
"encoding/binary"
"errors"
"reflect"
"unsafe"
"github.com/algorand/go-algorand-sdk/crypto"
"github.com/algorand/go-algorand-sdk/types"
)
func main() {}
//export AddressTxt2Bin
func AddressTxt2Bin(
p *C.char,
out *byte, // len is 32 by convention
) {
// turn the input C string into a Go string
var data string = C.GoString(p)
// attempt to decode an address from it
addr, err := types.DecodeAddress(data)
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
return
}
// write to C memory
array := unsafe.Slice(out, 32)
copy(array, addr[:])
}
//export AddressBin2Txt
func AddressBin2Txt(
in *byte,
out *byte,
) {
// cast the input pointer into []byte
data := unsafe.Slice(in, 32)
// attempt to encode an address out of it
addr, err := types.EncodeAddress(data)
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
return
}
// write to C memory
array := unsafe.Slice(out, 58)
copy(array, []byte(addr))
}
//export GetNFDSigNameLSIG
func GetNFDSigNameLSIG(
pNfdName *C.char,
registryAppID int64,
out *byte,
) {
// turn the input C string into a Go string
var nfdName string = C.GoString(pNfdName)
// look up the address associated with the NFD name
lsig, err := getLookupLSIG("name/", nfdName, uint64(registryAppID))
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
}
addr, err := lsig.Address()
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
}
// write to C memory
array := unsafe.Slice(out, 58)
copy(array, addr[:])
}
//export GetNFDSigRevAddressLSIG
func GetNFDSigRevAddressLSIG(
pPointedToAddress *C.char,
registryAppID int64,
out *byte,
) {
// turn the input C string into a Go string
var pointedToAddress string = C.GoString(pPointedToAddress)
lsig, err := getLookupLSIG("address/", pointedToAddress, uint64(registryAppID))
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
}
addr, err := lsig.Address()
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
}
// write to C memory
array := unsafe.Slice(out, 58)
copy(array, addr[:])
}
//export GetNFDSigRevAddressBinLSIG
func GetNFDSigRevAddressBinLSIG(
pPointedToAddressBin *byte,
registryAppID int64,
out *byte,
) {
// cast the input pointer into []byte
pointedToAddressBin := unsafe.Slice(pPointedToAddressBin, 32)
var p2addr types.Address
copy(p2addr[:], pointedToAddressBin[:sha512.Size256])
lsig, err := getLookupLSIG("address/", p2addr.String(), uint64(registryAppID))
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
}
addr, err := lsig.Address()
if err != nil {
cp := C.CString(err.Error())
defer C.free(unsafe.Pointer(cp))
C.elog_error(cp)
}
// write to C memory
array := unsafe.Slice(out, 58)
copy(array, addr[:])
}
func getLookupLSIG(prefixBytes, lookupBytes string, registryAppID uint64) (crypto.LogicSigAccount, error) {
sigLookupByteCode := []byte{
0x05, 0x20, 0x01, 0x01, 0x80, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x17, 0x35, 0x00, 0x31, 0x18, 0x34, 0x00, 0x12, 0x31, 0x10,
0x81, 0x06, 0x12, 0x10, 0x31, 0x19, 0x22, 0x12, 0x31, 0x19, 0x81, 0x00,
0x12, 0x11, 0x10, 0x40, 0x00, 0x01, 0x00, 0x22, 0x43, 0x26, 0x01,
}
contractSlice := sigLookupByteCode[6:14]
if !reflect.DeepEqual(contractSlice, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}) {
return crypto.LogicSigAccount{}, errors.New("Lookup template doesn't match expectation")
}
binary.BigEndian.PutUint64(contractSlice, registryAppID)
bytesToAppend := bytes.Join([][]byte{[]byte(prefixBytes), []byte(lookupBytes)}, nil)
uvarIntBytes := make([]byte, binary.MaxVarintLen64)
nBytes := binary.PutUvarint(uvarIntBytes, uint64(len(bytesToAppend)))
composedBytecode := bytes.Join([][]byte{sigLookupByteCode, uvarIntBytes[:nBytes], bytesToAppend}, nil)
logicSig := crypto.MakeLogicSigAccountEscrow(composedBytecode, [][]byte{})
return logicSig, nil
}