Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Cobuild integration and Solana support #3

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ CC := $(TARGET)-gcc
LD := $(TARGET)-gcc
OBJCOPY := $(TARGET)-objcopy
CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -I deps/secp256k1/src -I deps/secp256k1 -I deps/ckb-c-std-lib -I deps/ckb-c-std-lib/libc -I deps/ckb-c-std-lib/molecule -I c -I build -Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -g
LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
LDFLAGS := -nostdlib -nostartfiles -fno-builtin -Wl,-static -Wl,--gc-sections
XuJiandong marked this conversation as resolved.
Show resolved Hide resolved
SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context.h


OMNI_LOCK_CFLAGS :=$(subst ckb-c-std-lib,ckb-c-stdlib-20210801,$(CFLAGS)) -I deps/sparse-merkle-tree/c
OMNI_LOCK_CFLAGS := $(subst secp256k1,secp256k1-20210801,$(OMNI_LOCK_CFLAGS))
# enable log
# OMNI_LOCK_CFLAGS += -DCKB_C_STDLIB_PRINTF -DCKB_C_STDLIB_PRINTF_BUFFER_SIZE=1024


PROTOCOL_HEADER := c/blockchain.h
PROTOCOL_SCHEMA := c/blockchain.mol
Expand Down Expand Up @@ -54,14 +57,16 @@ ${PROTOCOL_SCHEMA}:
curl -L -o $@ ${PROTOCOL_URL}

ALL_C_SOURCE := $(wildcard c/omni_lock.c c/omni_lock_acp.h c/omni_lock_time_lock.h \
tests/omni_lock/omni_lock_sim.c tests/omni_lock/ckb_syscall_omni_lock_sim.h tests/omni_lock/omni_lock_supply.h)
tests/omni_lock/omni_lock_sim.c tests/omni_lock/ckb_syscall_omni_lock_sim.h tests/omni_lock/omni_lock_supply.h\
c/blake2b_decl_only.h c/cobuild.h c/cobuild.c)

fmt:
docker run --rm -v `pwd`:/code ${CLANG_FORMAT_DOCKER} bash -c "cd code && clang-format -i -style=Google $(ALL_C_SOURCE)"
git diff --exit-code $(ALL_C_SOURCE)

mol:
make omni_lock_mol
make cobuild_mol

c/xudt_rce_mol.h: c/xudt_rce.mol
${MOLC} --language c --schema-file $< > $@
Expand All @@ -76,17 +81,27 @@ omni_lock_mol:
${MOLC} --language - --schema-file c/omni_lock.mol --format json > build/omni_lock_mol2.json
moleculec-c2 --input build/omni_lock_mol2.json | clang-format -style=Google > c/omni_lock_mol2.h

build/omni_lock: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h c/secp256k1_lock.h build/secp256k1_data_info_20210801.h $(SECP256K1_SRC_20210801) c/ckb_identity.h
$(CC) $(OMNI_LOCK_CFLAGS) $(LDFLAGS) -o $@ $<
build/cobuild.o: c/cobuild.c c/cobuild.h
$(CC) -c $(OMNI_LOCK_CFLAGS) -o $@ $<

build/omni_lock.o: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h c/secp256k1_lock.h build/secp256k1_data_info_20210801.h $(SECP256K1_SRC_20210801) c/ckb_identity.h
$(CC) -c $(OMNI_LOCK_CFLAGS) -o $@ $<

build/omni_lock: build/omni_lock.o build/cobuild.o
$(CC) $(LDFLAGS) -o $@ $^
cp $@ [email protected]
$(OBJCOPY) --strip-debug --strip-all $@

cobuild_mol:
${MOLC} --language rust --schema-file c/basic.mol | rustfmt > tests/omni_lock_rust/src/schemas/basic.rs
${MOLC} --language rust --schema-file c/top_level.mol | rustfmt > tests/omni_lock_rust/src/schemas/top_level.rs

clean:
rm -rf build/secp256k1_data_info_20210801.h build/dump_secp256k1_data_20210801
rm -rf build/secp256k1_data_20210801
rm -rf build/*.debug
rm -f build/omni_lock
rm -f build/*.o
cd deps/secp256k1-20210801 && [ -f "Makefile" ] && make clean

install-tools:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ cd tests/omni_lock_rust && cargo test
## Deployment

See [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0042-omnilock/0042-omnilock.md)

81 changes: 81 additions & 0 deletions c/basic.mol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import blockchain;

array Hash [byte; 32];
vector String <byte>; // UTF-8 encoded
option Uint32Opt (Uint32);

table Action {
script_info_hash: Byte32, // script info
script_hash: Byte32, // script
data: Bytes, // action data
}

vector ActionVec <Action>;

table Message {
actions: ActionVec,
}

table ScriptInfo {
// The dapp name and domain the script belongs to
name: String,
url: String,

// Script info.
// schema: script action schema
// message_type: the entry action type used in WitnessLayout
script_hash: Byte32,
schema: String,
message_type: String,
}

vector ScriptInfoVec <ScriptInfo>;

table ResolvedInputs {
outputs: CellOutputVec,
XuJiandong marked this conversation as resolved.
Show resolved Hide resolved
outputs_data: BytesVec,
}

table BuildingPacketV1 {
message: Message,
payload: Transaction,
resolved_inputs: ResolvedInputs,
change_output: Uint32Opt,
script_infos: ScriptInfoVec,
lock_actions: ActionVec,
}

union BuildingPacket {
BuildingPacketV1,
}

table SighashAll {
message: Message,
seal: Bytes,
}

table SighashAllOnly {
seal: Bytes,
}

table SealPair {
script_hash: Byte32,
seal: Bytes,
}
vector SealPairVec <SealPair>;

table OtxStart {
start_input_cell: Uint32,
start_output_cell: Uint32,
start_cell_deps: Uint32,
start_header_deps: Uint32,
}

table Otx {
input_cells: Uint32,
output_cells: Uint32,
cell_deps: Uint32,
header_deps: Uint32,
message: Message,
seals: SealPairVec,
}
61 changes: 61 additions & 0 deletions c/blake2b_decl_only.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

#ifndef __BLAKE2B_DECL_ONLY_H__
#define __BLAKE2B_DECL_ONLY_H__

#include <stddef.h>
#include <stdint.h>

#define BLAKE2_PACKED(x) x __attribute__((packed))

enum blake2b_constant {
BLAKE2B_BLOCKBYTES = 128,
BLAKE2B_OUTBYTES = 64,
BLAKE2B_KEYBYTES = 64,
BLAKE2B_SALTBYTES = 16,
BLAKE2B_PERSONALBYTES = 16
};
BLAKE2_PACKED(struct blake2b_param__ {
uint8_t digest_length; /* 1 */
uint8_t key_length; /* 2 */
uint8_t fanout; /* 3 */
uint8_t depth; /* 4 */
uint32_t leaf_length; /* 8 */
uint32_t node_offset; /* 12 */
uint32_t xof_length; /* 16 */
uint8_t node_depth; /* 17 */
uint8_t inner_length; /* 18 */
uint8_t reserved[14]; /* 32 */
uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
});

typedef struct blake2b_param__ blake2b_param;

typedef struct blake2b_state__ {
uint64_t h[8];
uint64_t t[2];
uint64_t f[2];
uint8_t buf[BLAKE2B_BLOCKBYTES];
size_t buflen;
size_t outlen;
uint8_t last_node;
} blake2b_state;

/* Streaming API */
int ckb_blake2b_init(blake2b_state *S, size_t outlen);
int blake2b_init(blake2b_state *S, size_t outlen);
int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
size_t keylen);
int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
int blake2b_final(blake2b_state *S, void *out, size_t outlen);
/* Simple API */
int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
const void *key, size_t keylen);

/* This is simply an alias for blake2b */
int blake2(void *out, size_t outlen, const void *in, size_t inlen,
const void *key, size_t keylen);

int blake2b_init_param(blake2b_state *S, const blake2b_param *P);

#endif
8 changes: 8 additions & 0 deletions c/ckb_identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ typedef int (*validate_signature_t)(void *prefilled_data, const uint8_t *sig,
typedef int (*convert_msg_t)(const uint8_t *msg, size_t msg_len,
uint8_t *new_msg, size_t new_msg_len);

bool g_cobuild_enabled = false;
uint8_t g_cobuild_signing_message_hash[32];

static void bin_to_hex(const uint8_t *source, uint8_t *dest, size_t len) {
const static uint8_t HEX_TABLE[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
Expand Down Expand Up @@ -375,6 +378,11 @@ int validate_signature_eos(void *prefilled_data, const uint8_t *sig,
}

int generate_sighash_all(uint8_t *msg, size_t msg_len) {
if (g_cobuild_enabled) {
memcpy(msg, g_cobuild_signing_message_hash, BLAKE2B_BLOCK_SIZE);
return 0;
}

int ret;
uint64_t len = 0;
unsigned char temp[MAX_WITNESS_SIZE];
Expand Down
Loading