You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(cshared/,examples/c-app): Support compiling wallet SDK as a C library; add simple example
feat(cshared/,examples/c-app): Support compiling wallet SDK as a C library; add simple example
feat(cshared/,examples/c-app): Support compiling wallet SDK as a C library; add simple example
initial attempt
some fixes; docs
more fixes
fix linting errors
Copy file name to clipboardExpand all lines: docs/specs.md
+89-1Lines changed: 89 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,8 @@ Go Wallet SDK is a modular Go library intended to support the development of m
11
11
|`pkg/ethclient`| Chain‑agnostic Ethereum JSON‑RPC client. It provides two method sets: a drop‑in replacement compatible with go‑ethereum’s `ethclient` and a custom implementation that follows the Ethereum JSON‑RPC specification without assuming chain‑specific types. It supports JSON‑RPC methods covering `eth_`, `net_` and `web3_` namespace |
12
12
|`pkg/common`| Shared types and constants. Such as canonical chain IDs (e.g., Ethereum Mainnet, Optimism, Arbitrum, BSC, Base). Developers use these values when configuring the SDK or examples. |
13
13
|`pkg/balance/contracts`| Solidity contracts (not part of the published source) used by the balance fetcher when interacting with on‑chain balance scanning contracts. |
14
-
|`examples/`| Demonstrations of SDK usage. Includes `balance-fetcher-web` (a web interface for batch balance fetching) and `ethclient‑usage` (an example that exercises the Ethereum client across multiple RPC endpoints). ||
14
+
|`cshared/`| C shared library bindings that expose core SDK functionality to C applications. |
15
+
|`examples/`| Demonstrations of SDK usage. Includes `balance-fetcher-web` (a web interface for batch balance fetching), `ethclient‑usage` (an example that exercises the Ethereum client across multiple RPC endpoints), and `c-app` (a C application demonstranting usage of the C library usage). ||
15
16
16
17
## 2. Architecture
17
18
@@ -45,6 +46,11 @@ Internally, the client stores a reference to an RPC client and implements each m
45
46
46
47
The `pkg/common` package defines shared types and enumerations. The main export is `type ChainID uint64` with constants for well‑known networks such as `EthereumMainnet`, `EthereumSepolia`, `OptimismMainnet`, `ArbitrumMainnet`, `BSCMainnet`, `BaseMainnet`, `BaseSepolia` and a custom `StatusNetworkSepolia`. These constants allow the examples to pre‑populate supported chains and label results without repeating numeric IDs.
47
48
49
+
### 2.5 C Library
50
+
51
+
At `cshared/lib.go` the library functions are exposed to be used as C bindings for core SDK functionality, enabling integration with C applications and other languages that can interface with C libraries.
52
+
The shared library is built using Go's `c-shared` build mode (e.g `go build -buildmode=c-shared -o lib.so lib.go`), which generates both the library file (`.so` on Linux, `.dylib` on macOS) and a corresponding C header file with function declarations and type definitions.
53
+
48
54
## 3. API Description
49
55
50
56
### 3.1 Balance Fetcher API (`pkg/balance/fetcher`)
@@ -255,6 +261,53 @@ Converts Go `ethereum.FilterQuery` structs into JSON-RPC filter objects:
255
261
256
262
This enables `EthGetLogs`, `EthNewFilter`, and other event filtering methods to work correctly across all EVM chains.
257
263
264
+
### 3.3 C Shared Library API (`cshared/`)
265
+
266
+
The C shared library provides a minimal but complete interface for blockchain operations from C applications. All functions use consistent patterns for error handling and memory management.
267
+
268
+
| Function | Description | Parameters | Returns |
269
+
| -------- | ----------- | ---------- | ------- |
270
+
|`GoWSK_NewClient(rpcURL, errOut)`| Creates a new Ethereum client connected to the specified RPC endpoint |`rpcURL`: null-terminated string with RPC URL; `errOut`: optional double pointer for error message | Opaque client handle (0 on failure) |
271
+
|`GoWSK_CloseClient(handle)`| Closes an Ethereum client and releases its resources |`handle`: client handle from `GoWSK_NewClient`| None |
272
+
|`GoWSK_ChainID(handle, errOut)`| Retrieves the chain ID for the connected network |`handle`: client handle; `errOut`: optional double pointer for error message | Chain ID as null-terminated string (must be freed) |
273
+
|`GoWSK_GetBalance(handle, address, errOut)`| Fetches the native token balance for an address |`handle`: client handle; `address`: hex-encoded Ethereum address; `errOut`: optional double pointer for error message | Balance in wei as null-terminated string (must be freed) |
274
+
|`GoWSK_FreeCString(s)`| Frees a string allocated by the library |`s`: string pointer returned by other functions | None |
All string returns from the library are allocated with `malloc` and must be freed using `GoWSK_FreeCString`. Also Error messages returned via `errOut` parameters must also be freed
310
+
258
311
## 4. Example Applications
259
312
260
313
### 4.1 Web‑Based Balance Fetcher
@@ -277,6 +330,27 @@ The `examples/ethclient-usage` folder shows how to use the Ethereum client acros
277
330
278
331
- **Code Structure** – The example is split into `main.go`, which loops over endpoints, and helper functions such as `testRPC()` that call various methods and handle errors.
279
332
333
+
### 4.3 C Application Example
334
+
335
+
At `examples/c-app` there is a simple app demonstrating how to use the C library.
336
+
337
+
**usage**
338
+
339
+
At the root do to create the library:
340
+
341
+
```bash
342
+
make build-c-lib
343
+
```
344
+
345
+
Run the example:
346
+
347
+
```bash
348
+
cd examples/c-app && make build
349
+
make
350
+
cd bin/
351
+
./c-app
352
+
```
353
+
280
354
## 5. Testing & Development
281
355
282
356
### 5.1 Fetching SDK
@@ -297,6 +371,20 @@ go test ./...
297
371
298
372
This executes unit tests for the balance fetcher and Ethereum client. The balance fetcher includes a `mock` package to simulate RPC responses. The repository also includes continuous integration workflows (`.github/workflows`) and static analysis configurations (`.golangci.yml`).
299
373
374
+
### 5.3 Building the C Shared Library
375
+
376
+
The SDK includes build support for creating C shared libraries that expose core functionality to non-Go applications.
377
+
378
+
To build the library run:
379
+
380
+
```bash
381
+
make build-c-lib
382
+
```
383
+
384
+
This creates:
385
+
-`build/libgowalletsdk.dylib` (macOS) or `build/libgowalletsdk.so` (Linux)
0 commit comments