Skip to content

Commit 12d9018

Browse files
gtreptadwightguth
authored andcommitted
Start eth_sign implementation (#438)
* Start eth_sign implementation * Update eth_sign to new event loop convention * Draft a proper implementation of #privateKey * Fix build-node error * Change storage configuration of account keys * Check for nonexistent account in eth_sign call * web3: more specific error message
1 parent 674d8bc commit 12d9018

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ $(DEFN_DIR)/ocaml/$(MAIN_DEFN_FILE)-kompiled/plugin/semantics.$(LIBEXT): $(wildc
278278
&& cd $(dir $@) \
279279
&& ocamlfind $(OCAMLC) -c -g -I $(CURDIR)/$(DEFN_DIR)/ocaml/$(MAIN_DEFN_FILE)-kompiled \
280280
KRYPTO.ml \
281-
-package cryptokit -package secp256k1 -package bn128 -package ocaml-protoc -safe-string -thread \
281+
-package cryptokit -package hex -package secp256k1 -package bn128 -package ocaml-protoc -safe-string -thread \
282282
&& ocamlfind $(OCAMLC) -a -o semantics.$(LIBEXT) KRYPTO.$(EXT) -thread \
283283
&& ocamlfind remove ethereum-semantics-plugin-ocaml \
284284
&& ocamlfind install ethereum-semantics-plugin-ocaml $(PLUGIN_SUBMODULE)/plugin/META semantics.* *.cmi *.$(EXT)
@@ -292,7 +292,7 @@ $(DEFN_DIR)/ocaml/$(MAIN_DEFN_FILE)-kompiled/interpreter: $(DEFN_DIR)/ocaml/$(MA
292292
&& ocamlfind $(OCAMLC) -c -g -w -11-26 -package gmp -package zarith -package uuidm -package ethereum-semantics-plugin-ocaml -safe-string realdef.ml -match-context-rows 2 \
293293
&& ocamlfind $(OCAMLC) $(LIBFLAG) -o realdef.$(DLLEXT) realdef.$(EXT) \
294294
&& ocamlfind $(OCAMLC) -g -o interpreter constants.$(EXT) prelude.$(EXT) plugin.$(EXT) parser.$(EXT) lexer.$(EXT) hooks.$(EXT) run.$(EXT) interpreter.ml \
295-
-package gmp -package dynlink -package zarith -package str -package uuidm -package unix -package ethereum-semantics-plugin-ocaml -linkpkg -linkall -thread -safe-string
295+
-package gmp -package hex -package dynlink -package zarith -package str -package uuidm -package unix -package ethereum-semantics-plugin-ocaml -linkpkg -linkall -thread -safe-string
296296

297297
# Node Backend
298298

deps/plugin

krypto.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module KRYPTO
1414
- `Sha256` takes a String and returns a 64-character hex-encoded string of the 32-byte SHA2-256 hash of the string.
1515
- `RipEmd160` takes a String and returns a 40-character hex-encoded string of the 20-byte RIPEMD160 hash of the string.
1616
- `ECDSARecover` takes a 32-character byte string of a message, v, r, s of the signed message and returns the 64-character public key used to sign the message.
17+
- `ECDSASign` takes a 32-character byte string of a message hash, a 32-character byte string of a private key, and returns the 65 byte hex-encoded signature in [r,s,v] form
1718
See [this StackOverflow post](https://ethereum.stackexchange.com/questions/15766/what-does-v-r-s-in-eth-gettransactionbyhash-mean) for some information about v, r, and s.
1819

1920
In all functions above, input `String` is interpreted as byte array, e.g. it is NOT hex-encoded.
@@ -23,6 +24,7 @@ In all functions above, input `String` is interpreted as byte array, e.g. it is
2324
| ECDSARecover ( String , Int , String , String ) [function, hook(KRYPTO.ecdsaRecover)]
2425
| Sha256 ( String ) [function, hook(KRYPTO.sha256)]
2526
| RipEmd160 ( String ) [function, hook(KRYPTO.ripemd160)]
27+
| ECDSASign ( String, String ) [function, hook(KRYPTO.ecdsaSign)]
2628
// ----------------------------------------------------------------------------------------------------
2729
```
2830

web3.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module WEB3
1515
<blockchain>
1616
<chainID> $CHAINID:Int </chainID>
1717
</blockchain>
18+
<accountKeys> .Map </accountKeys>
1819
<snapshots> .List </snapshots>
1920
<web3socket> $SOCK:Int </web3socket>
2021
<web3clientsocket> 0:IOInt </web3clientsocket>
@@ -161,6 +162,8 @@ module WEB3
161162
<method> "eth_getStorageAt" </method>
162163
rule <k> #runRPCCall => #eth_getCode ... </k>
163164
<method> "eth_getCode" </method>
165+
rule <k> #runRPCCall => #eth_sign ... </k>
166+
<method> "eth_sign" </method>
164167
rule <k> #runRPCCall => #evm_snapshot ... </k>
165168
<method> "evm_snapshot" </method>
166169
rule <k> #runRPCCall => #evm_revert ... </k>
@@ -238,6 +241,25 @@ module WEB3
238241
...
239242
</account>
240243
244+
syntax KItem ::= "#eth_sign"
245+
// ----------------------------
246+
rule <k> #eth_sign => #signMessage(KEY, #hashMessage(#unparseByteStack(#parseByteStack(MESSAGE)))) ... </k>
247+
<params> [ ACCTADDR, MESSAGE, .JSONList ] </params>
248+
<accountKeys>... #parseHexWord(ACCTADDR) |-> KEY ...</accountKeys>
249+
250+
rule <k> #eth_sign => #sendResponse( "error": {"code": 3, "message": "Execution error", "data": [{ "code": 100, "message": "Account key doesn't exist, account locked!" }]} ) ... </k>
251+
<params> [ ACCTADDR, _ ] </params>
252+
<accountKeys> KEYMAP </accountKeys>
253+
requires notBool #parseHexWord(ACCTADDR) in_keys(KEYMAP)
254+
255+
syntax KItem ::= #signMessage ( String , String )
256+
// -------------------------------------------------
257+
rule <k> #signMessage(KEY, MHASH) => #sendResponse( "result" : "0x" +String ECDSASign( MHASH, KEY ) ) ... </k>
258+
259+
syntax String ::= #hashMessage ( String ) [function]
260+
// ----------------------------------------------------
261+
rule #hashMessage( S ) => #unparseByteStack(#parseHexBytes(Keccak256("\x19Ethereum Signed Message:\n" +String Int2String(lengthString(S)) +String S)))
262+
241263
syntax KItem ::= "#evm_snapshot"
242264
// --------------------------------
243265
rule <k> #evm_snapshot => #pushNetworkState ~> #sendResponse( "result" : #unparseQuantity( size ( SNAPSHOTS ) ) ) ... </k>

0 commit comments

Comments
 (0)