### Run Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/get_public_key/README.md Commands to navigate to the example directory, tidy Go modules, and run the public key derivation example. ```bash cd go-sdk/docs/examples/get_public_key go mod tidy go run get_public_key.go ``` -------------------------------- ### Setup User Wallet Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/get_public_key/README.md Initializes a new wallet for the user by generating a private key and then creating a wallet instance from it. Handles potential errors during key generation and wallet creation. ```go package main import ( "context" "encoding/hex" "fmt" ec "github.com/bsv-blockchain/go-sdk/primitives/ec" "log" "github.com/bsv-blockchain/go-sdk/wallet" ) func main() { ctx := context.Background() // Generate a new private key for the user userKey, err := ec.NewPrivateKey() if err != nil { log.Fatalf("Failed to generate user private key: %v", err) } log.Printf("User private key: %s", userKey.Wif()) // Create a new wallet for the user userWallet, err := wallet.NewWallet(userKey) if err != nil { log.Fatalf("Failed to create user wallet: %v", err) } log.Println("User wallet created successfully.") ``` -------------------------------- ### Cloning and Setup Source: https://github.com/bsv-blockchain/go-sdk/blob/master/CONTRIBUTING.md Steps to fork, clone the repository, navigate to the project directory, and install dependencies using npm. ```bash git clone https://github.com/YOUR_USERNAME/PROJECT.git cd bsv-sdk npm install ``` -------------------------------- ### Run the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/http_wallet/README.md Provides instructions on how to navigate to the example directory, tidy Go modules, and run the HTTP wallet example script. ```bash cd go-sdk/docs/examples/http_wallet go mod tidy go run http_wallet.go ``` -------------------------------- ### Run Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/create_signature/README.md Commands to navigate to the example directory, tidy Go modules, and run the signature creation example. ```bash cd go-sdk/docs/examples/create_signature go mod tidy go run create_signature.go ``` -------------------------------- ### Quick Start: Creating a Transaction Source: https://github.com/bsv-blockchain/go-sdk/blob/master/wallet/README.md Demonstrates how to initialize the wallet client and create a new transaction with specified outputs. This snippet covers the basic setup and a common use case for transaction creation. ```go import ( "context" "github.com/bsv-blockchain/go-sdk/wallet" ) func main() { ctx := context.Background() // Create a new wallet client walletClient := wallet.NewWalletClient() // Create a transaction createArgs := wallet.CreateActionArgs{ Description: "Example transaction", Outputs: []wallet.CreateActionOutput{ { LockingScript: "76a914...", Satoshis: 5000, OutputDescription: "Payment", }, }, } result, err := walletClient.CreateAction(ctx, createArgs, "example-app") if err != nil { // Handle error } // Use the result... } ``` -------------------------------- ### Install go-sdk Source: https://github.com/bsv-blockchain/go-sdk/blob/master/identity/README.md Command to install the go-sdk package using go get. ```bash go get github.com/bsv-blockchain/go-sdk ``` -------------------------------- ### Run Create HMAC Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/create_hmac/README.md Provides instructions on how to navigate to the example directory, tidy Go modules, and run the create HMAC example Go program. ```bash cd go-sdk/docs/examples/create_hmac go mod tidy go run create_hmac.go ``` -------------------------------- ### Setup Wallet for Self-Signing Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/create_signature/README.md Initializes a wallet instance and defines protocol and key identifiers for self-signing operations. Requires a private key and sets up the necessary parameters for wallet operations. ```go privateKey, _ := ec.NewPrivateKey() signerWallet, _ := wallet.NewWallet(privateKey) selfProtocolID := wallet.Protocol{Protocol: "ECDSA SelfSign", SecurityLevel: wallet.SecurityLevelSilent} selfKeyID := "my signing key v1" ``` -------------------------------- ### Run the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/storage_downloader/storage_downloader.md Provides the command to execute the storage downloader example. Users may need to replace the default UHRP URL if it becomes unavailable. ```bash go run storage_downloader.go ``` -------------------------------- ### Setup Wallet for HMAC Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/create_hmac/README.md Initializes a wallet instance and defines necessary parameters like ProtocolID and KeyID for HMAC operations. These parameters are crucial for deriving the correct key. ```go privateKey, _ := ec.NewPrivateKey() myWallet, _ := wallet.NewWallet(privateKey) // Define ProtocolID and KeyID for HMAC operations hmacProtocolID := wallet.Protocol{Protocol: "HMAC SelfSign", SecurityLevel: wallet.SecurityLevelSilent} hmacKeyID := "my hmac key v1" ``` -------------------------------- ### Running the Identity Client Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/identity_client/README.md This snippet shows the command to navigate to the example directory and execute the Go program for the Identity Client. ```bash cd docs/examples/identity_client go run main.go ``` -------------------------------- ### Setup Wallets Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/encrypt_data/README.md Creates two wallet instances, Alice's and Bob's, using newly generated private keys. In production, these keys should be securely managed. ```go alicePrivKey, _ := ec.NewPrivateKey() aliceWallet, _ := wallet.NewWallet(alicePrivKey) bobPrivKey, _ := ec.NewPrivateKey() bobWallet, _ := wallet.NewWallet(bobPrivKey) ``` -------------------------------- ### Running the WebSocket Peer Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/websocket_peer/README.md Provides the command to execute the Go example, demonstrating the peer-to-peer communication flow and how to handle graceful shutdown. ```bash go run websocket_peer.go ``` -------------------------------- ### Running the Encryption Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/encrypt_data/README.md Instructions on how to navigate to the example directory and execute the Go program for data encryption and decryption. ```bash cd go-sdk/docs/examples/encrypt_data go mod tidy # (if you haven't run it before or added new imports) go run encrypt_data.go ``` -------------------------------- ### Running the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/encrypted_message/README.md Command to execute the Go example program. ```bash go run message.go ``` -------------------------------- ### Running the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/authenticated_messaging/README.md Command to execute the authenticated messaging example. ```bash go run authenticated_messaging.go ``` -------------------------------- ### Run Storage Uploader Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/storage_uploader/storage_uploader.md Command to execute the storage uploader example. Note that a proper wallet implementation is required for successful connection to the storage service. ```bash go run storage_uploader.go ``` -------------------------------- ### Run Registry Register Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/registry_register/README.md Command to execute the registry registration example Go program. ```bash go run registry_register.go ``` -------------------------------- ### Run the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/address_from_wif/README.md Command to execute the Go program that demonstrates deriving an address from a WIF. ```bash go run address_from_wif.go ``` -------------------------------- ### Run the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/create_tx_with_op_return/README.md Command to execute the Go example code for creating a transaction with an OP_RETURN output. ```bash go run create_tx_with_op_return.go ``` -------------------------------- ### Running the AES Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/aes/README.md Command to execute the Go AES encryption/decryption example. Assumes the Go file is named 'aes.go'. ```bash go run aes.go ``` -------------------------------- ### Storage Downloader Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/README.md Demonstrates how to download files using UHRP URLs with the go-sdk. ```go package main import ( "fmt" "log" "github.com/bsv-blockchain/go-sdk/storage/downloader" ) func main() { // Example usage of storage downloader uhropURL := "uhrop://example.com/file.txt" outputFile := "./downloaded_file.txt" err := downloader.DownloadFile(uhropURL, outputFile) if err != nil { log.Fatalf("Failed to download file: %v", err) } fmt.Printf("File downloaded successfully to %s\n", outputFile) } ``` -------------------------------- ### Run HD Key From xPub Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/hd_key_from_xpub/README.md Command to execute the Go program that demonstrates converting an xPub string to an HD key object. Ensure you have Go installed and the necessary dependencies downloaded. ```bash go run hd_key_from_xpub.go ``` -------------------------------- ### Get User's Identity Public Key Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/get_public_key/README.md Retrieves the wallet's root identity public key by setting `IdentityKey: true` in the `GetPublicKeyArgs`. Prints the compressed public key in hexadecimal format. Handles potential errors during the retrieval process. ```go // --- Get User's Own Public Key (Identity Key) --- identityPubKeyArgs := wallet.GetPublicKeyArgs{ IdentityKey: true, // Indicates we want the root identity public key of the wallet } identityPubKeyResult, err := userWallet.GetPublicKey(ctx, identityPubKeyArgs, "example-app") if err != nil { log.Fatalf("Failed to get user's identity public key: %v", err) } fmt.Printf("User's Identity Public Key: %s\n", hex.EncodeToString(identityPubKeyResult.PublicKey.Compressed())) ``` -------------------------------- ### Get User's Derived Public Key for Self Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/get_public_key/README.md Derives and retrieves a public key for the wallet's own use under a specific protocol and key ID. This is achieved by setting `Counterparty.Type` to `wallet.CounterpartyTypeSelf`. Prints the derived public key in hexadecimal format. Handles potential errors. ```go // --- Get User's Derived Public Key for a Protocol/KeyID (Self) --- // Define a protocol and key ID for deriving a key selfProtocol := wallet.Protocol{ SecurityLevel: wallet.SecurityLevelEveryApp, // Or other appropriate security level Protocol: "myprotocol", } selfKeyID := "user001" getSelfPubKeyArgs := wallet.GetPublicKeyArgs{ EncryptionArgs: wallet.EncryptionArgs{ ProtocolID: selfProtocol, KeyID: selfKeyID, Counterparty: wallet.Counterparty{ Type: wallet.CounterpartyTypeSelf, // Explicitly for self }, }, } selfPubKeyResult, err := userWallet.GetPublicKey(ctx, getSelfPubKeyArgs, "example-app") if err != nil { log.Fatalf("Failed to get user's derived public key (self): %v", err) } fmt.Printf("User's Derived Public Key (Self - Protocol: %s, KeyID: %s): %s\n", selfProtocol.Protocol, selfKeyID, hex.EncodeToString(selfPubKeyResult.PublicKey.Compressed())) ``` -------------------------------- ### Run the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/create_wallet/README.md Provides instructions on how to navigate to the example directory, tidy Go modules, and run the `create_wallet.go` script. It also includes a note about generating unique wallets and securely storing mnemonic phrases or master extended private keys. ```bash cd go-sdk/docs/examples/create_wallet go mod tidy # (if you haven't run it before or added new imports in other examples) go run create_wallet.go ``` -------------------------------- ### Setting Up Peers with Mock Transport Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/websocket_peer/README.md Demonstrates the creation of multiple authenticated peers, each configured with a wallet and a mock transport connected to a central server. This setup is crucial for establishing communication channels. ```go // Create mock WebSocket server server := newMockWebSocketServer() // Create wallets // ... (wallet creation for Alice, Bob, Charlie) ... // Create peers with mock transport alicePeer := auth.NewPeer(&auth.PeerOptions{ Wallet: aliceWallet, Transport: newMockTransport("alice", server), }) // ... (similarly for Bob and Charlie) ... ``` -------------------------------- ### Get Recipient's Public Key Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/encrypt_data/README.md Retrieves the recipient's public key, which is necessary for encrypting data for them. The `IdentityKey: true` argument ensures a stable public key is obtained for ECIES. ```go bobIdentityKeyArgs := wallet.GetPublicKeyArgs{IdentityKey: true} bobPubKeyResult, _ := bobWallet.GetPublicKey(context.Background(), bobIdentityKeyArgs, "bob_originator_get_pubkey") bobECPubKey := bobPubKeyResult.PublicKey ``` -------------------------------- ### Run Private Key to Backup Shares Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/keyshares_pk_to_backup/README.md Command to execute the go program that demonstrates generating private key backup shares. ```bash go run to_backup.go ``` -------------------------------- ### Get Keys and Addresses Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/create_wallet/README.md Extracts the private key, public key, and P2PKH address from a derived extended key. It shows two methods: deriving from `*ec.PrivateKey` and `*ec.PublicKey`, and directly from the `*bip32.ExtendedKey`. ```go // Get the private key from the derived extended key privateKey, err := derivedKey.ECPrivKey() fmt.Printf("Derived Private Key (Hex): %x\n", privateKey.Serialize()) // Get the public key from the private key publicKey := privateKey.PubKey() fmt.Printf("Derived Public Key (Hex): %x\n", publicKey.Compressed()) // Get the P2PKH address from the public key addressFromPubKey, _ := script.NewAddressFromPublicKey(publicKey, true) fmt.Printf("Address from Public Key: %s\n", addressFromPubKey.AddressString) // You can also get the address directly from the derived extended key addressFromExtendedKey := derivedKey.Address(&chaincfg.MainNet) fmt.Printf("Address from Derived Extended Key: %s\n", addressFromExtendedKey) ``` -------------------------------- ### Derive Public Key for Counterparty Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/get_public_key/README.md Generates a new private key for a counterparty and then derives the user's public key intended for interaction with that counterparty. This involves setting the protocol, key ID, and specifying the counterparty's public key with `CounterpartyTypeOther`. ```go // --- Get Counterparty's Public Key --- // Generate a new private key for the counterparty counterpartyKey, err := ec.NewPrivateKey() if err != nil { log.Fatalf("Failed to generate counterparty private key: %v", err) } log.Printf("Counterparty private key: %s", counterpartyKey.Wif()) // Define a protocol and key ID for deriving a key with a counterparty counterpartyProtocol := wallet.Protocol{ SecurityLevel: wallet.SecurityLevelEveryAppAndCounterparty, Protocol: "sharedprotocol", } counterpartyKeyID := "sharedkey001" getCounterpartyPubKeyArgs := wallet.GetPublicKeyArgs{ EncryptionArgs: wallet.EncryptionArgs{ ProtocolID: counterpartyProtocol, KeyID: counterpartyKeyID, Counterparty: wallet.Counterparty{ Type: wallet.CounterpartyTypeOther, Counterparty: counterpartyKey.PubKey(), // The counterparty's public key }, }, // ForSelf: false, // This is the default, meaning we want the public key *for* the counterparty } // User's wallet gets the public key it would use to interact with the counterparty derivedForCounterpartyPubKeyResult, err := userWallet.GetPublicKey(ctx, getCounterpartyPubKeyArgs, "example-app") if err != nil { log.Fatalf("Failed to get derived public key for counterparty: %v", err) } fmt.Printf("User's Derived Public Key (for Counterparty - Protocol: %s, KeyID: %s): %s\n", counterpartyProtocol.Protocol, counterpartyKeyID, hex.EncodeToString(derivedForCounterpartyPubKeyResult.PublicKey.Compressed())) log.Println("Successfully retrieved public keys.") } ``` -------------------------------- ### Run the Example Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/keyshares_pk_from_backup/README.md This bash command demonstrates how to execute the Go example program for reconstructing a private key from backup shares. ```bash go run from_backup.go ``` -------------------------------- ### Create Storage Downloader Source: https://github.com/bsv-blockchain/go-sdk/blob/master/docs/examples/storage_downloader/storage_downloader.md Initializes a new StorageDownloader instance configured for the mainnet network. This downloader is essential for resolving and fetching content via UHRP URLs. ```go downloader := storage.NewStorageDownloader(storage.DownloaderConfig{ Network: overlay.NetworkMainnet, }) ```