Go Signer Module
A Go module for calling cryptocurrency transaction signing functions from a Rust library through CGO.
π Normal Workflow: First try importing the package normally (go mod tidy, go build). If you get architecture or linking errors, then clone the repository and use manual build (./build_manual.sh).
π Quick Start
Step 1: Try Normal Go Import (Recommended First)
// In your Go project
import signer "github.com/sidan-lab/cardano-golang-signing-module"
// Try to use the library
func main() {
signer, err := signer.NewMnemonicSigner(mnemonic, derivationPath)
// ... your code
}
# Install dependencies
go mod tidy
go build # or go run main.go
Step 2: Manual Build (If Prebuilt Libraries Don't Work)
If you get errors like "no prebuilt library for your architecture" or linking errors, follow these steps:
1. Clone the repository
git clone https://github.com/sidan-lab/cardano-golang-signing-module.git
cd cardano-golang-signing-module
2. Build the library manually
./build_manual.sh
3. Use the local library in your project
# In your project directory
go mod edit -replace github.com/sidan-lab/cardano-golang-signing-module=/path/to/cardano-golang-signing-module
go mod tidy
go build
For Publishing New Versions (Maintainers Only)
./build_prebuilt.sh
π When Do You Need Manual Build?
Normal Workflow
- First try importing the package normally (
go mod tidy, go build)
- If that fails with architecture or linking errors β use manual build
Manual Build Required When:
- β No prebuilt library for your architecture (ARM, exotic platforms, etc.)
- β Linking errors with prebuilt libraries
- β CGO compilation issues with distributed binaries
- β
Development - you're modifying the Rust code
- β
Custom optimizations - need specific build flags
- β
Latest features - using unreleased Rust code changes
- β
Security - want to build from source yourself
Prebuilt Build (For Maintainers Only)
Use ./build_prebuilt.sh only when:
- π§ Publishing new version - creating libraries for distribution
- π§ Cross-platform builds - building for all supported platforms
- π§ Release preparation - updating prebuilt binaries for users
ποΈ Complete Manual Build Guide
Prerequisites
-
Install Rust (if not already installed):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
-
Install Go (1.19+):
# macOS
brew install go
# Linux
sudo apt install golang-go # Ubuntu/Debian
sudo yum install golang # CentOS/RHEL
# Windows - download from https://golang.org/dl/
-
C Compiler:
# macOS - Xcode Command Line Tools
xcode-select --install
# Linux
sudo apt install build-essential # Ubuntu/Debian
sudo yum groupinstall "Development Tools" # CentOS/RHEL
# Windows - Visual Studio Build Tools or MinGW
Step-by-Step Manual Build
-
Clone the repository:
git clone <repository-url>
cd cardano-golang-signing-module
-
Run the complete manual build:
./build_manual.sh
This script will:
- Switch to manual build configuration
- Build Rust library from source
- Build Go project
- Run tests
-
Verify the build:
go test -v
Manual Build Steps (Detailed)
If you prefer to run steps manually:
-
Switch to manual build configuration:
./switch_to_manual_build.sh
-
Build Rust library:
cargo build --release
-
Build Go project:
go build
-
Run tests:
go test -v
π¦ Available Build Scripts
Main Build Scripts
./build_manual.sh (For Users)
Complete manual build pipeline
- β
Switches to manual build mode
- β
Builds Rust library from source (
cargo build --release)
- β
Builds Go project using your compiled library
- β
Runs tests
./build_prebuilt.sh (For Maintainers)
Multi-platform build for publishing
- π§ Switches to prebuilt library mode
- π§ Builds Rust libraries for all platforms (via
build_rust.sh)
- π§ Updates prebuilt binaries in
prebuilt/ directory
- π§ Builds Go project using prebuilt libraries
- π§ Runs tests
Configuration Scripts
./switch_to_prebuilt.sh
- Switches
signer.go to use platform-specific prebuilt libraries
- Only changes configuration, doesn't build
./switch_to_manual_build.sh
- Switches
signer.go to use unified manual build library
- Only changes configuration, doesn't build
./build_rust.sh (For Maintainers)
- Builds Rust libraries for all supported platforms
- Creates prebuilt binaries in
prebuilt/ directory
- Used internally by
build_prebuilt.sh
- Only needed when publishing new versions
π Typical User Workflows
Scenario 1: Normal Usage (Works out of the box)
# In your Go project
go mod init myproject
go mod tidy
# Write your Go code with the import
# import "github.com/sidan-lab/cardano-golang-signing-module"
go build # Should work with prebuilt libraries
Scenario 2: Prebuilt Libraries Don't Work
# Step 1: Clone and build manually
git clone https://github.com/sidan-lab/cardano-golang-signing-module.git
cd cardano-golang-signing-module
./build_manual.sh
# Step 2: Use in your project
cd /path/to/your/project
go mod edit -replace github.com/sidan-lab/cardano-golang-signing-module=/path/to/cardano-golang-signing-module
go mod tidy
go build
For Project Maintainers
# When ready to publish new version
./build_prebuilt.sh # Creates binaries for all platforms
git add prebuilt/ # Commit updated prebuilt libraries
git commit -m "Update prebuilt libraries for v1.x.x"
git tag v1.x.x
git push origin main --tags
Prebuilt Libraries Available
- Linux:
x86_64, aarch64 (ARM64)
- macOS:
x86_64 (Intel), aarch64 (Apple Silicon)
- Windows:
x86_64
- Linux/macOS: Static libraries (
.a)
- Windows: Dynamic libraries (
.dll)
π οΈ Library Locations
Prebuilt Mode
Uses platform-specific libraries:
- Linux:
prebuilt/linux_{amd64,arm64}/libsigner_go.a
- macOS:
prebuilt/darwin_{amd64,arm64}/libsigner_go.a
- Windows:
prebuilt/windows_amd64/signer_go.dll
Manual Mode
Uses your compiled library:
- Unix (Linux/macOS):
target/release/libsigner_go.a
- Windows:
target/release/signer_go.dll or libsigner_go.a
π Usage
Complete Example
- Create a new Go project:
mkdir myproject
cd myproject
go mod init myproject
- Create main.go:
package main
import (
"fmt"
"log"
signer "github.com/sidan-lab/cardano-golang-signing-module"
)
func main() {
// Create signer from mnemonic phrase
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
derivationPath := "m/44'/118'/0'/0/0"
signer, err := signer.NewMnemonicSigner(mnemonic, derivationPath)
if err != nil {
log.Fatalf("Failed to create signer: %v", err)
}
defer signer.Close() // Important to free resources!
// Get public key
publicKey, err := signer.GetPublicKey()
if err != nil {
log.Fatalf("Failed to get public key: %v", err)
}
fmt.Printf("Public Key: %s\n", publicKey)
// Sign transaction
txHex := "your_transaction_hex_here"
signature, err := signer.SignTransaction(txHex)
if err != nil {
log.Fatalf("Failed to sign transaction: %v", err)
}
fmt.Printf("Signature: %s\n", signature)
}
- Try to build normally first:
go mod tidy
go build
- If that fails β follow manual build instructions above
### Available Signer Types
#### 1. Mnemonic Signer
```go
signer, err := signer.NewMnemonicSigner(mnemonicPhrase, derivationPath)
2. Bech32 Signer
signer, err := signer.NewBech32Signer(rootPrivateKey, derivationPath)
3. CLI Signer
signer, err := signer.NewCLISigner(ed25519Key)
Signer Methods
Sign Transaction
signature, err := signer.SignTransaction(txHex)
Get Public Key
publicKey, err := signer.GetPublicKey()
Free Resources
signer.Close() // Always call this!
π Troubleshooting
Step 1: Normal Import Issues
"Package not found" or "Module not found"
go mod tidy
go clean -modcache
go mod download
"CGO linking errors" or "Architecture not supported"
Your platform may not have prebuilt libraries. β Go to Manual Build (Step 2)
Step 2: Manual Build Issues
Prerequisites Missing
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Install Go
# Download from https://golang.org/dl/
# Install C compiler
# macOS: xcode-select --install
# Linux: sudo apt install build-essential
Build Errors
# Clean everything and rebuild
go clean
cargo clean
./build_manual.sh
"Local module replace not working"
Make sure the path is absolute:
# Get absolute path
pwd # Note the full path
# Use absolute path in replace
go mod edit -replace github.com/sidan-lab/cardano-golang-signing-module=/full/absolute/path/to/cardano-golang-signing-module
go mod tidy
"cargo not found"
Install Rust toolchain:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
"go not found"
Install Go from https://golang.org/dl/
CGO Compilation Errors
- Make sure you have a C compiler installed
- Try clean rebuild:
go clean
./build_manual.sh # or ./build_prebuilt.sh
Linking Errors
- Check that the library was built successfully
- Try switching build modes:
./build_prebuilt.sh # if manual fails
# or
./build_manual.sh # if prebuilt fails
Runtime Errors
- β Check that
signer.Close() wasn't called before use
- β Verify input data (mnemonic phrases, keys) is valid
- β Make sure error handling is proper
π Project Structure
βββ src/ # Rust source code
β βββ lib.rs # Main Rust library
β βββ c_interface.rs # C-compatible interface
βββ include/
β βββ signer.h # C header file
βββ prebuilt/ # Prebuilt libraries
β βββ linux_amd64/
β βββ linux_arm64/
β βββ darwin_amd64/
β βββ darwin_arm64/
β βββ windows_amd64/
βββ examples/ # Go examples
βββ signer.go # Go interface
βββ signer_test.go # Go tests
βββ build_manual.sh # Manual build script
βββ build_prebuilt.sh # Prebuilt build script
βββ build_rust.sh # Multi-platform Rust build
βββ switch_to_manual_build.sh
βββ switch_to_prebuilt.sh
βββ Cargo.toml # Rust configuration
βββ .cargo/config.toml # Cargo build config
π Security Notes
- Memory Management: Always call
signer.Close() to free Rust resources
- Production Safety: Never use test mnemonic phrases in production
- Error Handling: Always check returned errors
- Build Verification: Manual builds allow you to verify the code yourself
π License
[Add your license information here]
π€ Contributing
[Add contributing guidelines here]