-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
170 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
|
||
*.o | ||
*/.DS_Store | ||
.DS_Store | ||
|
||
.vscode/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 $@ $^ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Submodule ckb-c-stdlib
added at
20578d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "eip712.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
package-lock.json | ||
package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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":[]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |