|
| 1 | +// Package main demonstrates how to use the WalletAdvertiser for creating and managing |
| 2 | +// SHIP and SLAP overlay advertisements. |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + |
| 9 | + "github.com/bsv-blockchain/go-overlay-discovery-services/pkg/advertiser" |
| 10 | + "github.com/bsv-blockchain/go-overlay-discovery-services/pkg/types" |
| 11 | + oa "github.com/bsv-blockchain/go-overlay-services/pkg/core/advertiser" |
| 12 | + "github.com/bsv-blockchain/go-sdk/overlay" |
| 13 | + "github.com/bsv-blockchain/go-sdk/script" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + fmt.Println("BSV Overlay Discovery Services - WalletAdvertiser Example") |
| 18 | + fmt.Println("========================================================") |
| 19 | + |
| 20 | + // Example configuration |
| 21 | + chain := "main" |
| 22 | + privateKey := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" |
| 23 | + storageURL := "https://storage.example.com" |
| 24 | + advertisableURI := "https://service.example.com/" |
| 25 | + |
| 26 | + // Optional lookup resolver configuration |
| 27 | + lookupConfig := &types.LookupResolverConfig{ |
| 28 | + HTTPSEndpoint: stringPtr("https://resolver.example.com"), |
| 29 | + MaxRetries: intPtr(3), |
| 30 | + TimeoutMS: intPtr(5000), |
| 31 | + } |
| 32 | + |
| 33 | + // Create a new WalletAdvertiser |
| 34 | + fmt.Println("\n1. Creating WalletAdvertiser...") |
| 35 | + advertiser, err := advertiser.NewWalletAdvertiser( |
| 36 | + chain, |
| 37 | + privateKey, |
| 38 | + storageURL, |
| 39 | + advertisableURI, |
| 40 | + lookupConfig, |
| 41 | + ) |
| 42 | + if err != nil { |
| 43 | + log.Fatalf("Failed to create WalletAdvertiser: %v", err) |
| 44 | + } |
| 45 | + fmt.Printf("✓ WalletAdvertiser created successfully\n") |
| 46 | + fmt.Printf(" Chain: %s\n", advertiser.GetChain()) |
| 47 | + fmt.Printf(" Storage URL: %s\n", advertiser.GetStorageURL()) |
| 48 | + fmt.Printf(" Advertisable URI: %s\n", advertiser.GetAdvertisableURI()) |
| 49 | + |
| 50 | + // Set up mock dependencies (in a real scenario, these would be actual implementations) |
| 51 | + fmt.Println("\n2. Setting up dependencies...") |
| 52 | + advertiser.SetSkipStorageValidation(true) // Skip storage validation for example |
| 53 | + fmt.Println("✓ Dependencies configured") |
| 54 | + |
| 55 | + // Initialize the advertiser |
| 56 | + fmt.Println("\n3. Initializing WalletAdvertiser...") |
| 57 | + if err := advertiser.Init(); err != nil { |
| 58 | + log.Fatalf("Failed to initialize WalletAdvertiser: %v", err) |
| 59 | + } |
| 60 | + fmt.Printf("✓ WalletAdvertiser initialized successfully\n") |
| 61 | + fmt.Printf(" Initialized: %v\n", advertiser.IsInitialized()) |
| 62 | + |
| 63 | + // Create some example advertisements |
| 64 | + fmt.Println("\n4. Creating advertisements...") |
| 65 | + adsData := []*oa.AdvertisementData{ |
| 66 | + { |
| 67 | + Protocol: overlay.ProtocolSHIP, |
| 68 | + TopicOrServiceName: "payments", |
| 69 | + }, |
| 70 | + { |
| 71 | + Protocol: overlay.ProtocolSLAP, |
| 72 | + TopicOrServiceName: "identity_verification", |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + // Note: This will fail in the current implementation since BSV SDK integration is not complete |
| 77 | + _, err = advertiser.CreateAdvertisements(adsData) |
| 78 | + if err != nil { |
| 79 | + fmt.Printf("⚠ CreateAdvertisements failed (expected): %v\n", err) |
| 80 | + fmt.Println(" This is expected as BSV SDK integration is not yet implemented") |
| 81 | + } |
| 82 | + |
| 83 | + // Parse an example advertisement |
| 84 | + fmt.Println("\n5. Parsing an advertisement...") |
| 85 | + outputScriptBytes := []byte{0x01, 0x02, 0x03, 0x04, 0x05} // Mock script |
| 86 | + outputScript := script.NewFromBytes(outputScriptBytes) |
| 87 | + advertisement, err := advertiser.ParseAdvertisement(outputScript) |
| 88 | + if err != nil { |
| 89 | + fmt.Printf("Failed to parse advertisement: %v\n", err) |
| 90 | + } else { |
| 91 | + fmt.Printf("✓ Advertisement parsed successfully:\n") |
| 92 | + fmt.Printf(" Protocol: %s\n", advertisement.Protocol) |
| 93 | + fmt.Printf(" Identity Key: %s\n", advertisement.IdentityKey) |
| 94 | + fmt.Printf(" Domain: %s\n", advertisement.Domain) |
| 95 | + fmt.Printf(" Topic/Service: %s\n", advertisement.TopicOrService) |
| 96 | + } |
| 97 | + |
| 98 | + // Find all advertisements for a protocol |
| 99 | + fmt.Println("\n6. Finding advertisements...") |
| 100 | + _, err = advertiser.FindAllAdvertisements(overlay.ProtocolSHIP) |
| 101 | + if err != nil { |
| 102 | + fmt.Printf("⚠ FindAllAdvertisements failed (expected): %v\n", err) |
| 103 | + fmt.Println(" This is expected as storage integration is not yet implemented") |
| 104 | + } |
| 105 | + |
| 106 | + fmt.Println("\n✓ Example completed successfully!") |
| 107 | + fmt.Println("\nNote: Some operations failed as expected because they require:") |
| 108 | + fmt.Println("- BSV SDK integration for transaction creation and signing") |
| 109 | + fmt.Println("- Storage backend integration for persistence") |
| 110 | + fmt.Println("- Real PushDrop decoder implementation") |
| 111 | +} |
| 112 | + |
| 113 | +// Helper functions |
| 114 | +func stringPtr(s string) *string { |
| 115 | + return &s |
| 116 | +} |
| 117 | + |
| 118 | +func intPtr(i int) *int { |
| 119 | + return &i |
| 120 | +} |
0 commit comments