commit ab2a39a70407d5da514f332f26906cc7ce0bcc43 Author: Mark Spanbroek Date: Mon Feb 22 15:32:48 2021 +0100 Nitro state, including ABI encoding and hashing diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1963f8d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ +[*] +indent_style = space +insert_final_newline = true +indent_size = 2 +trim_trailing_whitespace = true \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..becd7f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +* +!*/ +!*.* diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..9ed7760 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +nim 1.4.2 diff --git a/nitro.nim b/nitro.nim new file mode 100644 index 0000000..bcb56ab --- /dev/null +++ b/nitro.nim @@ -0,0 +1,3 @@ +import ./nitro/state + +export state diff --git a/nitro.nimble b/nitro.nimble new file mode 100644 index 0000000..c0f49e9 --- /dev/null +++ b/nitro.nimble @@ -0,0 +1,9 @@ +version = "0.1.0" +author = "Nim Nitro developers" +license = "MIT" +description = "Nitro state channels" + +requires "nim >= 1.2.6 & < 2.0.0" +requires "nimcrypto >= 0.5.4 & < 0.6.0" +requires "stint" +requires "stew" diff --git a/nitro/abi.nim b/nitro/abi.nim new file mode 100644 index 0000000..f64b0e0 --- /dev/null +++ b/nitro/abi.nim @@ -0,0 +1,101 @@ +import pkg/stew/endians2 +import pkg/stint + +type + Abi* = object + AbiWriter* = object + bytes: seq[byte] + tuples: seq[Tuple] + Tuple = object + postponed: seq[Split] + Split = object + head: Slice[int] + tail: seq[byte] + +proc isStatic*(_: type Abi, t: type SomeUnsignedInt): bool = true +proc isStatic*(_: type Abi, t: type StUint): bool = true +proc isStatic*(_: type Abi, t: type bool): bool = true +proc isStatic*(_: type Abi, t: type enum): bool = true +proc isStatic*[T](_: type Abi, t: type seq[T]): bool = false +proc isStatic*[I, T](_: type Abi, t: type array[I, T]): bool = Abi.isStatic(T) + +proc encode*[T](_: type Abi, value: T): seq[byte] + +proc pad(writer: var AbiWriter, len: int) = + let padlen = (32 - len mod 32) mod 32 + for _ in 0..