-
Notifications
You must be signed in to change notification settings - Fork 16
/
ckb_swappable_signatures.h
55 lines (49 loc) · 1.77 KB
/
ckb_swappable_signatures.h
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
#ifndef CKB_C_STDLIB_CKB_SWAPPABLE_SIGNATURES_H_
#define CKB_C_STDLIB_CKB_SWAPPABLE_SIGNATURES_H_
#include "ckb_dlfcn.h"
#ifdef CKB_SWAPPABLE_SIGNATURES_DEBUG
#define DEBUG(s) ckb_debug(s)
#else
#define DEBUG(s)
#endif /* CKB_SWAPPABLE_SIGNATURES_DEBUG */
typedef int (*verify_function)(void *, const uint8_t *, size_t, const uint8_t *,
size_t, uint8_t *, size_t *);
typedef struct {
uint8_t *code_buffer;
uint64_t code_buffer_size;
uint8_t *prefilled_data_buffer;
size_t prefilled_buffer_size;
verify_function verify_func;
} CkbSwappableSignatureInstance;
int ckb_initialize_swappable_signature(
const uint8_t *code_hash, uint8_t hash_type,
CkbSwappableSignatureInstance *instance) {
void *handle = NULL;
size_t consumed_size = 0;
int ret = ckb_dlopen2(code_hash, hash_type, instance->code_buffer,
instance->code_buffer_size, &handle, &consumed_size);
if (ret != CKB_SUCCESS) {
return ret;
}
instance->code_buffer_size = consumed_size;
int (*load_prefilled_data_func)(void *, size_t *);
*(void **)(&load_prefilled_data_func) =
ckb_dlsym(handle, "load_prefilled_data");
if (load_prefilled_data_func == NULL) {
DEBUG("Error loading load prefilled data func!");
return CKB_INVALID_DATA;
}
ret = load_prefilled_data_func(instance->prefilled_data_buffer,
&instance->prefilled_buffer_size);
if (ret != CKB_SUCCESS) {
DEBUG("Error loading prefilled data!");
return ret;
}
*(void **)(&instance->verify_func) = ckb_dlsym(handle, "validate_signature");
if (instance->verify_func == NULL) {
DEBUG("Error loading validate signature func!");
return CKB_INVALID_DATA;
}
return CKB_SUCCESS;
}
#endif /* CKB_C_STDLIB_CKB_SWAPPABLE_SIGNATURES_H_ */