diff --git a/.github/workflows/ark.artifacts.yaml b/.github/workflows/ark.artifacts.yaml new file mode 100644 index 0000000..79762f0 --- /dev/null +++ b/.github/workflows/ark.artifacts.yaml @@ -0,0 +1,55 @@ +name: Build and Upload Binaries and WASM + +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21.0 + + - name: Build binaries + run: make build-all + + - name: Build WASM SDK + run: | + cd pkg/client-sdk + make build-wasm + + - name: Upload server binaries + uses: actions/upload-artifact@v4 + with: + name: server-binaries + path: ./server/build + if-no-files-found: error + retention-days: 5 + compression-level: 6 + overwrite: true + + - name: Upload client binaries + uses: actions/upload-artifact@v4 + with: + name: client-binaries + path: ./client/build + if-no-files-found: error + retention-days: 5 + compression-level: 6 + overwrite: true + + - name: Upload WASM SDK + uses: actions/upload-artifact@v4 + with: + name: wasm-sdk + path: pkg/client-sdk/build + if-no-files-found: error + retention-days: 5 + compression-level: 0 + overwrite: true \ No newline at end of file diff --git a/.github/workflows/ark.release.yaml b/.github/workflows/ark.release.yaml index b1a73b9..c504003 100755 --- a/.github/workflows/ark.release.yaml +++ b/.github/workflows/ark.release.yaml @@ -25,6 +25,7 @@ jobs: - name: Build binaries run: make build-all + # Server - name: Upload server binary (Linux, AMD64) uses: actions/upload-release-asset@v1 env: @@ -106,6 +107,16 @@ jobs: asset_name: ark-darwin-arm64 asset_content_type: application/octet-stream + # WASM SDK + - name: Upload WASM SDK + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ github.event.release.upload_url }} + asset_path: pkg/client-sdk/build/client-sdk.wasm + asset_name: client-sdk.wasm + asset_content_type: application/wasm # Docker diff --git a/.github/workflows/ark.unit.yaml b/.github/workflows/ark.unit.yaml index 34bfaa0..ca0c9a0 100755 --- a/.github/workflows/ark.unit.yaml +++ b/.github/workflows/ark.unit.yaml @@ -4,12 +4,14 @@ on: push: paths: - "server/**" + - "pkg/client-sdk/**" branches: [master] pull_request: - branches: + branches: - master paths: - "server/**" + - "pkg/client-sdk/**" jobs: test-server: @@ -38,6 +40,7 @@ jobs: - run: go get -v -t -d ./... - name: unit testing run: make test + test-sdk: name: sdk unit tests runs-on: ubuntu-latest @@ -60,4 +63,4 @@ jobs: args: '-severity high -quiet ./...' - run: go get -v -t -d ./... - name: unit testing - run: make test \ No newline at end of file + run: make test \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3800d16..c6e0414 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .DS_Store .vscode/ -main.wasm +*.wasm wasm_exec.js \ No newline at end of file diff --git a/Makefile b/Makefile index b22e353..80f7612 100644 --- a/Makefile +++ b/Makefile @@ -16,5 +16,9 @@ build-all-client: @echo "Building ark binary for all archs..." @bash ./client/scripts/build-all -build: build-server build-client -build-all: build-all-server build-all-client \ No newline at end of file +build-wasm: + @echo "Building wasm..." + @$(MAKE) -C pkg/client-sdk build-wasm + +build: build-server build-client build-wasm +build-all: build-all-server build-all-client build-wasm diff --git a/client/scripts/build b/client/scripts/build index 0ec5289..763dd94 100755 --- a/client/scripts/build +++ b/client/scripts/build @@ -1,26 +1,36 @@ #!/bin/bash - set -e +# Get the parent directory path PARENT_PATH=$(dirname $( - cd $(dirname $0) - pwd -P + cd $(dirname $0) + pwd -P )) -if [[ -z $GOOS ]]; then - GOOS=$(eval "go env GOOS") -fi +# Set VERSION (you can modify this to get the version from a file or environment variable) +VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "unknown") +# Set GOOS and GOARCH if not already set +if [[ -z $GOOS ]]; then + GOOS=$(go env GOOS) +fi if [[ -z $GOARCH ]]; then - GOARCH=$(eval "go env GOARCH") + GOARCH=$(go env GOARCH) fi echo "Building for $GOOS $GOARCH" +echo "Version: $VERSION" +# Change to the parent directory pushd $PARENT_PATH + +# Create build directory if it doesn't exist mkdir -p build +# Build the binary with version information +GO111MODULE=on go build -ldflags="-s -w -X 'main.Version=$VERSION'" -o build/ark-$GOOS-$GOARCH . -GO111MODULE=on go build -ldflags="-s -w" -o build/ark-$GOOS-$GOARCH . +echo "Build complete: build/ark-$GOOS-$GOARCH" -popd +# Return to the original directory +popd \ No newline at end of file diff --git a/client/scripts/build-all b/client/scripts/build-all index bf18a7f..b08b2fa 100755 --- a/client/scripts/build-all +++ b/client/scripts/build-all @@ -1,21 +1,41 @@ #!/bin/bash - set -e +# Get the parent directory path PARENT_PATH=$(dirname $( - cd $(dirname $0) - pwd -P + cd $(dirname $0) + pwd -P )) +# Define OS and ARCH arrays declare -a OS=("darwin" "linux") declare -a ARCH=("amd64" "arm64") +# Change to the parent directory pushd $PARENT_PATH +# Function to handle errors +handle_error() { + echo "Error occurred in build for $1 $2" + echo "Build command: GOOS=$1 GOARCH=$2 ./scripts/build" + echo "Exit code: $3" + echo "You may want to run this build manually to see more detailed error messages." +} + +# Loop through OS and ARCH combinations for os in "${OS[@]}"; do - for arch in "${ARCH[@]}"; do - GOOS=$os GOARCH=$arch ./scripts/build - done + for arch in "${ARCH[@]}"; do + echo "Building for $os $arch" + if GOOS=$os GOARCH=$arch ./scripts/build; then + echo "Build successful for $os $arch" + else + handle_error $os $arch $? + fi + echo "------------------------" + done done -popd \ No newline at end of file +# Return to the original directory +popd + +echo "All builds completed." \ No newline at end of file diff --git a/pkg/client-sdk/.gitignore b/pkg/client-sdk/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/pkg/client-sdk/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/pkg/client-sdk/Makefile b/pkg/client-sdk/Makefile index 8d718ec..000d147 100644 --- a/pkg/client-sdk/Makefile +++ b/pkg/client-sdk/Makefile @@ -18,4 +18,17 @@ vet: ## lint: lint codebase lint: @echo "Linting code..." - @golangci-lint run --fix \ No newline at end of file + @golangci-lint run --fix --verbose + +## wasm: compiles the client-sdk to wasm +WASM_DIR = wasm +BUILD_DIR = build +VERSION := $(shell git describe --tags --always --dirty) +GO_VERSION := $(shell go version | cut -d' ' -f3) + +.PHONY: build-wasm + +build-wasm: + @mkdir -p $(BUILD_DIR) + @echo "Version: $(VERSION)" + @GOOS=js GOARCH=wasm GO111MODULE=on go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(BUILD_DIR)/client-sdk.wasm $(WASM_DIR)/main.go diff --git a/pkg/client-sdk/wasm/localstorage_store_wasm.go b/pkg/client-sdk/wasm/browser/config_store.go similarity index 98% rename from pkg/client-sdk/wasm/localstorage_store_wasm.go rename to pkg/client-sdk/wasm/browser/config_store.go index a8622b3..c46fd7c 100644 --- a/pkg/client-sdk/wasm/localstorage_store_wasm.go +++ b/pkg/client-sdk/wasm/browser/config_store.go @@ -1,4 +1,7 @@ -package arksdkwasm +//go:build js && wasm +// +build js,wasm + +package browser import ( "context" diff --git a/pkg/client-sdk/wasm/ark_sdk_wasm.go b/pkg/client-sdk/wasm/browser/exports.go similarity index 98% rename from pkg/client-sdk/wasm/ark_sdk_wasm.go rename to pkg/client-sdk/wasm/browser/exports.go index 4eb5488..7112559 100644 --- a/pkg/client-sdk/wasm/ark_sdk_wasm.go +++ b/pkg/client-sdk/wasm/browser/exports.go @@ -1,4 +1,7 @@ -package arksdkwasm +//go:build js && wasm +// +build js,wasm + +package browser import ( "context" diff --git a/pkg/client-sdk/wasm/localstorage_walletstore_wasm.go b/pkg/client-sdk/wasm/browser/wallet_store.go similarity index 97% rename from pkg/client-sdk/wasm/localstorage_walletstore_wasm.go rename to pkg/client-sdk/wasm/browser/wallet_store.go index adcc7c7..795951b 100644 --- a/pkg/client-sdk/wasm/localstorage_walletstore_wasm.go +++ b/pkg/client-sdk/wasm/browser/wallet_store.go @@ -1,4 +1,7 @@ -package arksdkwasm +//go:build js && wasm +// +build js,wasm + +package browser import ( "encoding/hex" diff --git a/pkg/client-sdk/wasm/wrappers_wasm.go b/pkg/client-sdk/wasm/browser/wrappers.go similarity index 99% rename from pkg/client-sdk/wasm/wrappers_wasm.go rename to pkg/client-sdk/wasm/browser/wrappers.go index 991c891..a94ed16 100644 --- a/pkg/client-sdk/wasm/wrappers_wasm.go +++ b/pkg/client-sdk/wasm/browser/wrappers.go @@ -1,4 +1,7 @@ -package arksdkwasm +//go:build js && wasm +// +build js,wasm + +package browser import ( "context" diff --git a/pkg/client-sdk/wasm/build.go b/pkg/client-sdk/wasm/build.go new file mode 100644 index 0000000..1293aa4 --- /dev/null +++ b/pkg/client-sdk/wasm/build.go @@ -0,0 +1,43 @@ +//go:build js && wasm +// +build js,wasm + +package main + +import ( + "fmt" + "runtime/debug" +) + +var ( + Version = "dev" + CommitSHA = "unknown" + BuildTime = "unknown" +) + +func init() { + if info, available := debug.ReadBuildInfo(); available { + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + CommitSHA = setting.Value + case "vcs.time": + BuildTime = setting.Value + } + } + } +} + +// PrintBuildInfo prints the build information +func PrintBuildInfo() { + fmt.Printf("ARK SDK WebAssembly Module\n") + fmt.Printf("Version: %s\n", Version) + fmt.Printf("Commit: %s\n", CommitSHA) + fmt.Printf("Build Time: %s\n", BuildTime) +} + +// GetVersion returns the version string +func GetVersion() string { + return Version +} + +// You can add more build-related functions here as needed diff --git a/pkg/client-sdk/wasm/main.go b/pkg/client-sdk/wasm/main.go new file mode 100644 index 0000000..55658c8 --- /dev/null +++ b/pkg/client-sdk/wasm/main.go @@ -0,0 +1,20 @@ +//go:build js && wasm +// +build js,wasm + +package main + +import ( + "github.com/ark-network/ark/pkg/client-sdk/wasm/browser" +) + +func main() { + c := make(chan struct{}, 0) + println("ARK SDK WebAssembly module initialized") + browser.InitWrapper() + <-c +} + +func init() { + // You can add any additional initialization here if needed + // This runs before the main function +} diff --git a/server/scripts/build b/server/scripts/build index f62daa1..41496a6 100755 --- a/server/scripts/build +++ b/server/scripts/build @@ -1,18 +1,32 @@ - #!/bin/bash - set -e +# Get the parent directory path PARENT_PATH=$(dirname $( - cd $(dirname $0) - pwd -P + cd $(dirname $0) + pwd -P )) -OS=$(eval "go env GOOS") -ARCH=$(eval "go env GOARCH") +# Set VERSION (you can modify this to get the version from a file or environment variable) +VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "unknown") +# Get OS and ARCH +OS=$(go env GOOS) +ARCH=$(go env GOARCH) + +echo "Building arkd for $OS $ARCH" +echo "Version: $VERSION" + +# Change to the parent directory pushd $PARENT_PATH + +# Create build directory if it doesn't exist mkdir -p build -GO111MODULE=on go build -o build/arkd-$OS-$ARCH ./cmd/arkd -popd - \ No newline at end of file + +# Build the binary with version information +go build -ldflags="-s -w -X 'main.Version=$VERSION'" -o build/arkd-$OS-$ARCH ./cmd/arkd + +echo "Build complete: build/arkd-$OS-$ARCH" + +# Return to the original directory +popd diff --git a/server/scripts/build-all b/server/scripts/build-all index 56ae1a9..5d18e35 100755 --- a/server/scripts/build-all +++ b/server/scripts/build-all @@ -1,23 +1,46 @@ #!/bin/bash - set -e +# Get the parent directory path PARENT_PATH=$(dirname $( - cd $(dirname $0) - pwd -P + cd $(dirname $0) + pwd -P )) +# Set VERSION (you can modify this to get the version from a file or environment variable) +VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "unknown") + +# Define OS and ARCH arrays declare -a OS=("darwin" "linux") declare -a ARCH=("amd64" "arm64") +# Change to the parent directory pushd $PARENT_PATH -mkdir -p build +# Function to handle errors +handle_error() { + echo "Error occurred in build for $1 $2" + echo "Build command: VERSION=$VERSION GOOS=$1 GOARCH=$2 ./scripts/build" + echo "Exit code: $3" + echo "You may want to run this build manually to see more detailed error messages." +} + +echo "Starting builds for version: $VERSION" + +# Loop through OS and ARCH combinations for os in "${OS[@]}"; do - for arch in "${ARCH[@]}"; do - echo "Building for $os $arch" - GOOS=$os GOARCH=$arch go build -o build/arkd-$os-$arch cmd/arkd/main.go - done + for arch in "${ARCH[@]}"; do + echo "Building for $os $arch" + if VERSION=$VERSION GOOS=$os GOARCH=$arch ./scripts/build; then + echo "Build successful for $os $arch" + else + handle_error $os $arch $? + fi + echo "------------------------" + done done -popd \ No newline at end of file +# Return to the original directory +popd + +echo "All builds completed for version $VERSION." \ No newline at end of file