Skip to content

Commit

Permalink
Design code
Browse files Browse the repository at this point in the history
  • Loading branch information
joii2020 committed Sep 4, 2022
1 parent b4085cb commit 8759fe9
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 19 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


*.o
*/.DS_Store
.DS_Store

.vscode/
build/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "deps/ckb-c-stdlib"]
path = deps/ckb-c-stdlib
url = https://github.com/nervosnetwork/ckb-c-stdlib.git
32 changes: 24 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
TARGET := riscv64-unknown-linux-gnu-
# TARGET := riscv64-unknown-linux-gnu-

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
CFLAGS := -fPIC -O3 -Wall -Werror -Wno-nonnull -Wno-unused-function -g -fno-builtin-printf -fno-builtin-memcmp -fvisibility=hidden -fdata-sections -ffunction-sections
# CFLAGS := $(CFLAGS) -nostdlib -nostdinc -nostartfiles -Wno-nonnull-compare
CFLAGS := $(CFLAGS) -I src
# CFLAGS := $(CFLAGS) -I deps/ckb-c-stdlib -I deps/ckb-c-stdlib/libc -I deps/ckb-c-stdlib/molecule

LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
LDFLAGS :=
# LDFLAGS := $(LDFLAGS) -Wl,--gc-sections -fdata-sections -ffunction-sections -Wl,-static


all:
all: clean build/test
mkdir -p build

clean: FORCE
rm -rf build/*

build/test: src/test/test_base.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^


.PHONY: FORCE
FORCE:


SRC = $(wildcard src/*.c) \
$(wildcard src/test/*.c)
OBJ = $(patsubst %.c,build/%.o,$(notdir ${SRC}))

build/%.o: src/%.c
$(CC) $(CFLAGS) -c $< -o $@

build/%.o: src/test/%.c
$(CC) $(CFLAGS) -c $< -o $@

build/test: $(OBJ)
$(CC) $(LDFLAGS) -o $@ $^


73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# EIP-712 design documentation

* Provides C code for getting hash according to eip-712
* Provides examples
* Complete test case

## Interface

```
int get_eip712_hash(eip712_data* data, uint8_t* out_hash);
```

Get the hash according to eip-712 through the passed parameters.

* Arg1 data: Some parameters defined according to requirements.
* Arg2 out_hash: Output the generated hash, the pointer to a buffer of length 32.
* Result: If success return 0, else return non-zero.

### Struct
```
typedef struct _eip712_domain {
uint8_t chain_id[32];
char* name;
uint8_t* verifying_contract;
char* version;
} eip712_domain;
typedef struct _eip712_active {
char* action;
char* params;
} eip712_active;
typedef struct _eip712_data {
eip712_domain doamin;
eip712_active active;
char* cell_extra_data;
char* transaction_das_message;
} eip712_data;
```

Definition of ```eip712_data* data``` parameter


## Implement

### Modifications based on this project: [eip712tool](https://github.com/markrypt0/eip712tool)

This project is developed in C language and basically realizes all the functions of EIP-712.
But it needs to be ported to risc-v. And because his parameter transfer uses Json, we will see if the situation is directly deleted later, and directly use the C structure to pass the parameters (In this, the ```types``` of eip-712 is determined).


### Verification

A verification file has been completed using [npm eip-712](https://www.npmjs.com/package/eip-712) that can be used later to verify that the results are correct.

### Test

#### A nodejs demo to generate a real EIP-712 hash
Correct the developed version as a real version of the demo:
```tools/nodejs_test/test_eip_712.js```. You can use the command: '''./tools/nodejs_test/test_eip_712.js ./tools/nodejs_test/data/data_dotbit.json'''

Output:
```
Domain hash: 0xad2ee3583cd6cfef2e2fda4f9c27bbd67b1a00408d58e4684e148fd4dc7326cf
Message hash: 0xa0096e1245f02a3563ebd75da920592ef533678e2ed5a06138c2f2cfcdb87b1f
Last message hash: 0xcce661e249e03e2e0c581e1763fa1432491863c625a4128dd966822cc5f1d2be
```
#### C testcases
Verify that the C code is executed correctly and check for memory problems, Also need to write fuzzing tests.
#### Contract Testcases
Test cases that are actually executed in the contract, use rust.
1 change: 1 addition & 0 deletions deps/ckb-c-stdlib
Submodule ckb-c-stdlib added at 20578d
1 change: 1 addition & 0 deletions src/eip712.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "eip712.h"
38 changes: 30 additions & 8 deletions src/eip712.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@
#ifndef _SRC_EIP712_H_
#define _SRC_EIP712_H_

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

typedef struct _eip712_domain {
uint8_t chain_id[32];
char* name;
uint8_t* verifying_contract;
char* version;
} eip712_domain;

typedef struct _eip712_active {
char* action;
char* params;
} eip712_active;

typedef struct _eip712_data {
eip712_domain doamin;
eip712_active active;
char* cell_extra_data;
char* transaction_das_message;
} eip712_data;

void init_eip712(eip712_data *data);

#define GEN_EIP712_DATA(data, type, primary_type, domain, message) \
eip712_data ##data; \
init_eip712(&(##data));
int get_eip712_hash(eip712_data* data, uint8_t* out_hash);

#define GEN_EIP712_TYPE()
// Get eip712 template
// out_data : buffer of output data; the data is json
// out_data_len : input out_data length, and output it length
// Result : if success is 0
int get_eip712_template(char* out_data, size_t* out_data_len);

#define GEN_EIP712_DOMAIN()
// Get hash of domain
int get_eip712_domain_hash(const char* data, size_t len, uint8_t* hash);

#define GEN_EIP712_MESSAGE()
// Get hash of message
int get_eip712_message_hash(const char* data, size_t len, uint8_t* hash);

#endif // _SRC_EIP712_H_
12 changes: 9 additions & 3 deletions src/test/test_base.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include <assert.h>

#include "eip712.h"

void test_base() {
// eip712_data data;

}

void test() {
GEN_EIP712_DATA(ddd, GEN_EIP712_TYPE(), "Mail", GEN_EIP712_DOMAIN(),
GEN_EIP712_MESSAGE());
int main() {
test_base();
return 0;
}
3 changes: 3 additions & 0 deletions tools/nodejs_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
package-lock.json
package.json
1 change: 1 addition & 0 deletions tools/nodejs_test/data/data_dotbit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"types":{"EIP712Domain":[{"name":"chainId","type":"uint256"},{"name":"name","type":"string"},{"name":"verifyingContract","type":"address"},{"name":"version","type":"string"}],"Action":[{"name":"action","type":"string"},{"name":"params","type":"string"}],"Cell":[{"name":"capacity","type":"string"},{"name":"lock","type":"string"},{"name":"type","type":"string"},{"name":"data","type":"string"},{"name":"extraData","type":"string"}],"Transaction":[{"name":"DAS_MESSAGE","type":"string"},{"name":"inputsCapacity","type":"string"},{"name":"outputsCapacity","type":"string"},{"name":"fee","type":"string"},{"name":"action","type":"Action"},{"name":"inputs","type":"Cell[]"},{"name":"outputs","type":"Cell[]"},{"name":"digest","type":"bytes32"}]},"primaryType":"Transaction","domain":{"chainId":"1","name":"da.systems","verifyingContract":"0x0000000000000000000000000000000020210722","version":"1"},"message":{"DAS_MESSAGE":"TRANSFER FROM 0x9176acd39a3a9ae99dcb3922757f8af4f94cdf3c(551.39280335 CKB) TO 0x9176acd39a3a9ae99dcb3922757f8af4f94cdf3c(551.39270335 CKB)","inputsCapacity":"551.39280335 CKB","outputsCapacity":"551.39270335 CKB","fee":"0.0001 CKB","digest":"0xa71c9bf1cb1686b35a6c2ee4593202bc13279aae96e6ea274d919444f1e3749f","action":{"action":"withdraw_from_wallet","params":"0x00"},"inputs":[],"outputs":[]}}
17 changes: 17 additions & 0 deletions tools/nodejs_test/test_eip_712.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

const fs = require('fs');
const keccak256 = require('keccak256');
const eip712 = require('eip-712');

const g_data = JSON.parse(fs.readFileSync(process.argv[2]));

let domain_hash = eip712.getStructHash(g_data, "EIP712Domain", g_data.domain);
let message_hash = eip712.getStructHash(g_data, g_data.primaryType, g_data.message);

console.log("Domain hash: 0x" + Buffer.from(domain_hash).toString("hex"));
console.log("Message hash: 0x" + Buffer.from(message_hash).toString("hex"));

let data1 = new Uint8Array([0x19, 0x01]);
let last_message = keccak256(Buffer.concat([data1, domain_hash, message_hash]));
console.log("Last message hash: 0x" + Buffer.from(last_message).toString("hex"));

0 comments on commit 8759fe9

Please sign in to comment.