-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cshared/,examples/c-app): Support compiling wallet SDK as a C library #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iurimatias
wants to merge
1
commit into
master
Choose a base branch
from
compile_c_lib
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -28,3 +28,7 @@ go.work.sum | |
# Editor/IDE | ||
# .idea/ | ||
# .vscode/ | ||
|
||
build/** | ||
|
||
examples/c-app/bin/** |
This file contains hidden or 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,34 @@ | ||
GOOS := $(shell go env GOOS) | ||
LIBNAME := libgowalletsdk | ||
BUILD_DIR := build | ||
|
||
ifeq ($(GOOS),darwin) | ||
LIBEXT := .dylib | ||
else | ||
LIBEXT := .so | ||
endif | ||
|
||
.PHONY: build-c-lib clean check-go | ||
|
||
check-go: | ||
@current=$$(go version | awk '{print $$3}' | sed 's/go//'); \ | ||
required=1.23; \ | ||
if [ -z "$$current" ]; then \ | ||
echo "Unable to detect Go version. Please install Go $$required or newer."; \ | ||
exit 1; \ | ||
fi; \ | ||
# Compare versions using sort -V | ||
if [ $$(printf '%s\n' "$$required" "$$current" | sort -V | head -n1) != "$$required" ]; then \ | ||
echo "Go $$required or newer is required. Found $$current"; \ | ||
echo "Tip: brew install go (or ensure PATH uses a recent Go)"; \ | ||
exit 1; \ | ||
fi | ||
|
||
build-c-lib: check-go | ||
mkdir -p $(BUILD_DIR) | ||
go build -buildmode=c-shared -o $(BUILD_DIR)/$(LIBNAME)$(LIBEXT) ./cshared | ||
@echo "Built $(BUILD_DIR)/$(LIBNAME)$(LIBEXT) and header $(BUILD_DIR)/$(LIBNAME).h" | ||
|
||
clean: | ||
rm -f $(BUILD_DIR)/$(LIBNAME).so $(BUILD_DIR)/$(LIBNAME).dylib $(BUILD_DIR)/$(LIBNAME).h | ||
|
This file contains hidden or 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,131 @@ | ||
package main | ||
|
||
/* | ||
#include <stdlib.h> | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"unsafe" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
gethrpc "github.com/ethereum/go-ethereum/rpc" | ||
|
||
sdkethclient "github.com/status-im/go-wallet-sdk/pkg/ethclient" | ||
) | ||
|
||
var ( | ||
clientsMutex sync.RWMutex | ||
nextHandle uint64 = 1 | ||
clients = map[uint64]*sdkethclient.Client{} | ||
) | ||
|
||
func storeClient(c *sdkethclient.Client) uint64 { | ||
clientsMutex.Lock() | ||
defer clientsMutex.Unlock() | ||
h := nextHandle | ||
nextHandle++ | ||
clients[h] = c | ||
return h | ||
} | ||
|
||
func getClient(handle uint64) *sdkethclient.Client { | ||
clientsMutex.RLock() | ||
defer clientsMutex.RUnlock() | ||
return clients[handle] | ||
} | ||
|
||
func deleteClient(handle uint64) { | ||
clientsMutex.Lock() | ||
defer clientsMutex.Unlock() | ||
delete(clients, handle) | ||
} | ||
|
||
//export GoWSK_NewClient | ||
func GoWSK_NewClient(rpcURL *C.char, errOut **C.char) C.ulonglong { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably want to "namespace" our function names a bit more, call this something like |
||
if rpcURL == nil { | ||
if errOut != nil { | ||
*errOut = C.CString("rpcURL is NULL") | ||
} | ||
return 0 | ||
} | ||
url := C.GoString(rpcURL) | ||
rpcClient, err := gethrpc.Dial(url) | ||
if err != nil { | ||
if errOut != nil { | ||
*errOut = C.CString(err.Error()) | ||
} | ||
return 0 | ||
} | ||
client := sdkethclient.NewClient(rpcClient) | ||
handle := storeClient(client) | ||
return C.ulonglong(handle) | ||
} | ||
|
||
//export GoWSK_CloseClient | ||
func GoWSK_CloseClient(handle C.ulonglong) { | ||
h := uint64(handle) | ||
c := getClient(h) | ||
if c != nil { | ||
c.Close() | ||
deleteClient(h) | ||
} | ||
} | ||
|
||
//export GoWSK_ChainID | ||
func GoWSK_ChainID(handle C.ulonglong, errOut **C.char) *C.char { | ||
c := getClient(uint64(handle)) | ||
if c == nil { | ||
if errOut != nil { | ||
*errOut = C.CString("invalid client handle") | ||
} | ||
return nil | ||
} | ||
id, err := c.EthChainId(context.Background()) | ||
if err != nil { | ||
if errOut != nil { | ||
*errOut = C.CString(err.Error()) | ||
} | ||
return nil | ||
} | ||
return C.CString(id.String()) | ||
} | ||
|
||
//export GoWSK_GetBalance | ||
func GoWSK_GetBalance(handle C.ulonglong, address *C.char, errOut **C.char) *C.char { | ||
c := getClient(uint64(handle)) | ||
if c == nil { | ||
if errOut != nil { | ||
*errOut = C.CString("invalid client handle") | ||
} | ||
return nil | ||
} | ||
if address == nil { | ||
if errOut != nil { | ||
*errOut = C.CString("address is NULL") | ||
} | ||
return nil | ||
} | ||
addr := common.HexToAddress(C.GoString(address)) | ||
bal, err := c.EthGetBalance(context.Background(), addr, nil) | ||
if err != nil { | ||
if errOut != nil { | ||
*errOut = C.CString(err.Error()) | ||
} | ||
return nil | ||
} | ||
return C.CString(bal.String()) | ||
} | ||
|
||
// frees C strings returned by GoWSK functions to prevent memory leaks. | ||
// | ||
//export GoWSK_FreeCString | ||
func GoWSK_FreeCString(s *C.char) { | ||
if s != nil { | ||
C.free(unsafe.Pointer(s)) | ||
} | ||
} | ||
|
||
func main() {} |
This file contains hidden or 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 hidden or 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,30 @@ | ||
CC := cc | ||
ROOT := ../.. | ||
BUILD_DIR := $(ROOT)/build | ||
LIB := $(BUILD_DIR)/libgowalletsdk | ||
BIN_DIR := bin | ||
|
||
UNAME_S := $(shell uname -s) | ||
ifeq ($(UNAME_S),Darwin) | ||
LIBEXT := .dylib | ||
RPATH := -Wl,-rpath,@executable_path/../bin | ||
else | ||
LIBEXT := .so | ||
RPATH := -Wl,-rpath,$(BIN_DIR) | ||
endif | ||
|
||
LIBFILE = $(LIB)$(LIBEXT) | ||
|
||
.PHONY: run build clean | ||
|
||
build: | ||
mkdir -p $(BIN_DIR) | ||
$(MAKE) -C $(ROOT) build-c-lib | ||
cp -f $(LIBFILE) $(BIN_DIR)/ | ||
$(CC) -I$(BUILD_DIR) -L$(BIN_DIR) -o $(BIN_DIR)/c-app main.c -lgowalletsdk $(RPATH) | ||
|
||
run: build | ||
$(BIN_DIR)/c-app | ||
|
||
clean: | ||
rm -rf $(BIN_DIR) |
This file contains hidden or 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,22 @@ | ||
# C example using the Go Wallet SDK shared library | ||
|
||
Build steps: | ||
- From repo root: make build-c-lib | ||
- Then: cd examples/c-app && make build | ||
|
||
Run the example: | ||
```bash | ||
make | ||
cd bin/ | ||
./c-app | ||
``` | ||
|
||
Notes: | ||
- The build generates build/libgowalletsdk.(so|dylib) and header build/libgowalletsdk.h at the repo root. | ||
- On macOS, the example copies the dylib next to the executable and sets rpath for convenience. | ||
- Exported functions: | ||
- GoWSK_NewClient(const char* rpcURL, char** errOut) -> unsigned long long | ||
- GoWSK_CloseClient(unsigned long long handle) | ||
- GoWSK_ChainID(unsigned long long handle, char** errOut) -> char* | ||
- GoWSK_GetBalance(unsigned long long handle, const char* address, char** errOut) -> char* | ||
- GoWSK_FreeCString(char* s) |
This file contains hidden or 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,42 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// Header generated by Go build -buildmode=c-shared | ||
#include "libgowalletsdk.h" | ||
|
||
int main(int argc, char** argv) { | ||
const char* url = "https://ethereum-rpc.publicnode.com"; | ||
const char* addr = "0x0000000000000000000000000000000000000000"; | ||
|
||
char* err = NULL; | ||
unsigned long long h = GoWSK_NewClient((char*)url, &err); | ||
if (h == 0) { | ||
fprintf(stderr, "Failed to create client: %s\n", err ? err : "unknown error"); | ||
if (err) GoWSK_FreeCString(err); | ||
return 1; | ||
} | ||
|
||
char* chain = GoWSK_ChainID(h, &err); | ||
if (chain == NULL) { | ||
fprintf(stderr, "ChainID error: %s\n", err ? err : "unknown error"); | ||
if (err) GoWSK_FreeCString(err); | ||
GoWSK_CloseClient(h); | ||
return 1; | ||
} | ||
printf("ChainID: %s\n", chain); | ||
GoWSK_FreeCString(chain); | ||
|
||
char* balance = GoWSK_GetBalance(h, (char*)addr, &err); | ||
if (balance == NULL) { | ||
fprintf(stderr, "GetBalance error: %s\n", err ? err : "unknown error"); | ||
if (err) GoWSK_FreeCString(err); | ||
GoWSK_CloseClient(h); | ||
return 1; | ||
} | ||
printf("Balance(wei): %s\n", balance); | ||
GoWSK_FreeCString(balance); | ||
|
||
GoWSK_CloseClient(h); | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't expect to have logic in this layer. Expected to have C bindings to expose wallet SDK functionality to a client. In that case, a client would take care of concurrency, storing ethcleints and so on.
for example: