|
| 1 | +# Wallet Package |
| 2 | + |
| 3 | +The wallet package provides a comprehensive interface for managing blockchain transactions, certificates, and cryptographic operations in the BSV Go SDK. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The wallet package provides functionality for: |
| 8 | +- Transaction creation and management |
| 9 | +- Certificate handling and verification |
| 10 | +- Cryptographic operations (encryption, signatures, HMAC) |
| 11 | +- Output management |
| 12 | +- Identity and authentication |
| 13 | + |
| 14 | +## Package Structure |
| 15 | + |
| 16 | +- `wallet.go` - Core wallet implementation |
| 17 | +- `interfaces.go` - Interface definitions and types |
| 18 | +- `error.go` - Error types and handling |
| 19 | +- `key_deriver.go` - Key derivation functionality |
| 20 | +- `proto_wallet.go` - Protocol-specific wallet implementation |
| 21 | +- `mock.go` - Mock implementations for testing |
| 22 | +- `cached_key_deriver.go` - Cached key derivation implementation |
| 23 | + |
| 24 | +### Substrates |
| 25 | + |
| 26 | +The `substrates/` directory contains lower-level implementations and utilities used by the wallet package. |
| 27 | + |
| 28 | +### Serializer |
| 29 | + |
| 30 | +The `serializer/` directory contains functionality for serializing and deserializing wallet data structures. |
| 31 | + |
| 32 | +## Quick Start |
| 33 | + |
| 34 | +```go |
| 35 | +import ( |
| 36 | + "context" |
| 37 | + "github.com/bsv-blockchain/go-sdk/wallet" |
| 38 | +) |
| 39 | + |
| 40 | +func main() { |
| 41 | + ctx := context.Background() |
| 42 | + |
| 43 | + // Create a new wallet client |
| 44 | + walletClient := wallet.NewWalletClient() |
| 45 | + |
| 46 | + // Create a transaction |
| 47 | + createArgs := wallet.CreateActionArgs{ |
| 48 | + Description: "Example transaction", |
| 49 | + Outputs: []wallet.CreateActionOutput{ |
| 50 | + { |
| 51 | + LockingScript: "76a914...", |
| 52 | + Satoshis: 5000, |
| 53 | + OutputDescription: "Payment", |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + result, err := walletClient.CreateAction(ctx, createArgs, "example-app") |
| 59 | + if err != nil { |
| 60 | + // Handle error |
| 61 | + } |
| 62 | + |
| 63 | + // Use the result... |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Testing |
| 68 | + |
| 69 | +The package includes comprehensive tests in `*_test.go` files. Run tests with: |
| 70 | + |
| 71 | +```bash |
| 72 | +go test ./... |
| 73 | +``` |
| 74 | + |
| 75 | +## Core Interfaces |
| 76 | + |
| 77 | +### Interface |
| 78 | + |
| 79 | +The main `Interface` interface combines all wallet functionality: |
| 80 | + |
| 81 | +```go |
| 82 | +type Interface interface { |
| 83 | + KeyOperations |
| 84 | + CreateAction(ctx context.Context, args CreateActionArgs, originator string) (*CreateActionResult, error) |
| 85 | + SignAction(ctx context.Context, args SignActionArgs, originator string) (*SignActionResult, error) |
| 86 | + AbortAction(ctx context.Context, args AbortActionArgs, originator string) (*AbortActionResult, error) |
| 87 | + ListActions(ctx context.Context, args ListActionsArgs, originator string) (*ListActionsResult, error) |
| 88 | + InternalizeAction(ctx context.Context, args InternalizeActionArgs, originator string) (*InternalizeActionResult, error) |
| 89 | + ListOutputs(ctx context.Context, args ListOutputsArgs, originator string) (*ListOutputsResult, error) |
| 90 | + RelinquishOutput(ctx context.Context, args RelinquishOutputArgs, originator string) (*RelinquishOutputResult, error) |
| 91 | + RevealCounterpartyKeyLinkage(ctx context.Context, args RevealCounterpartyKeyLinkageArgs, originator string) (*RevealCounterpartyKeyLinkageResult, error) |
| 92 | + RevealSpecificKeyLinkage(ctx context.Context, args RevealSpecificKeyLinkageArgs, originator string) (*RevealSpecificKeyLinkageResult, error) |
| 93 | + AcquireCertificate(ctx context.Context, args AcquireCertificateArgs, originator string) (*Certificate, error) |
| 94 | + ListCertificates(ctx context.Context, args ListCertificatesArgs, originator string) (*ListCertificatesResult, error) |
| 95 | + ProveCertificate(ctx context.Context, args ProveCertificateArgs, originator string) (*ProveCertificateResult, error) |
| 96 | + RelinquishCertificate(ctx context.Context, args RelinquishCertificateArgs, originator string) (*RelinquishCertificateResult, error) |
| 97 | + DiscoverByIdentityKey(ctx context.Context, args DiscoverByIdentityKeyArgs, originator string) (*DiscoverCertificatesResult, error) |
| 98 | + DiscoverByAttributes(ctx context.Context, args DiscoverByAttributesArgs, originator string) (*DiscoverCertificatesResult, error) |
| 99 | + IsAuthenticated(ctx context.Context, args any, originator string) (*AuthenticatedResult, error) |
| 100 | + WaitForAuthentication(ctx context.Context, args any, originator string) (*AuthenticatedResult, error) |
| 101 | + GetHeight(ctx context.Context, args any, originator string) (*GetHeightResult, error) |
| 102 | + GetHeaderForHeight(ctx context.Context, args GetHeaderArgs, originator string) (*GetHeaderResult, error) |
| 103 | + GetNetwork(ctx context.Context, args any, originator string) (*GetNetworkResult, error) |
| 104 | + GetVersion(ctx context.Context, args any, originator string) (*GetVersionResult, error) |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### KeyOperations |
| 109 | + |
| 110 | +The `KeyOperations` interface handles cryptographic operations: |
| 111 | + |
| 112 | +```go |
| 113 | +type KeyOperations interface { |
| 114 | + GetPublicKey(ctx context.Context, args GetPublicKeyArgs, originator string) (*GetPublicKeyResult, error) |
| 115 | + Encrypt(ctx context.Context, args EncryptArgs, originator string) (*EncryptResult, error) |
| 116 | + Decrypt(ctx context.Context, args DecryptArgs, originator string) (*DecryptResult, error) |
| 117 | + CreateHmac(ctx context.Context, args CreateHmacArgs, originator string) (*CreateHmacResult, error) |
| 118 | + VerifyHmac(ctx context.Context, args VerifyHmacArgs, originator string) (*VerifyHmacResult, error) |
| 119 | + CreateSignature(ctx context.Context, args CreateSignatureArgs, originator string) (*CreateSignatureResult, error) |
| 120 | + VerifySignature(ctx context.Context, args VerifySignatureArgs, originator string) (*VerifySignatureResult, error) |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Transaction Management |
| 125 | + |
| 126 | +### Creating Transactions |
| 127 | + |
| 128 | +To create a new transaction, use the `CreateAction` method with `CreateActionArgs`: |
| 129 | + |
| 130 | +```go |
| 131 | +type CreateActionArgs struct { |
| 132 | + Description string // Human-readable description |
| 133 | + InputBEEF []byte // Optional BEEF-encoded input |
| 134 | + Inputs []CreateActionInput // Transaction inputs |
| 135 | + Outputs []CreateActionOutput // Transaction outputs |
| 136 | + LockTime uint32 // Optional lock time |
| 137 | + Version uint32 // Transaction version |
| 138 | + Labels []string // Optional labels for organization |
| 139 | + Options *CreateActionOptions // Optional configuration |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +Example: |
| 144 | +```go |
| 145 | +ctx := context.Background() |
| 146 | + |
| 147 | +// Create transaction |
| 148 | +createArgs := wallet.CreateActionArgs{ |
| 149 | + Description: "Example transaction", |
| 150 | + Outputs: []wallet.CreateActionOutput{ |
| 151 | + { |
| 152 | + LockingScript: "76a914...", |
| 153 | + Satoshis: 5000, |
| 154 | + OutputDescription: "Payment", |
| 155 | + }, |
| 156 | + }, |
| 157 | +} |
| 158 | + |
| 159 | +result, err := walletClient.CreateAction(ctx, createArgs, "example-app") |
| 160 | +if err != nil { |
| 161 | + log.Fatal(err) |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +### Signing Transactions |
| 166 | + |
| 167 | +After creating a transaction, use `SignAction` to sign it: |
| 168 | + |
| 169 | +```go |
| 170 | +type SignActionArgs struct { |
| 171 | + Reference string // Base64-encoded reference |
| 172 | + Spends map[uint32]SignActionSpend // Input index -> spending details |
| 173 | + Options *SignActionOptions // Optional configuration |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +Example: |
| 178 | +```go |
| 179 | +signArgs := wallet.SignActionArgs{ |
| 180 | + Reference: result.Reference, |
| 181 | + Spends: map[uint32]wallet.SignActionSpend{ |
| 182 | + 0: { |
| 183 | + UnlockingScript: "...", |
| 184 | + SequenceNumber: 0xffffffff, |
| 185 | + }, |
| 186 | + }, |
| 187 | +} |
| 188 | + |
| 189 | +signResult, err := walletClient.SignAction(ctx, signArgs, "example-app") |
| 190 | +if err != nil { |
| 191 | + log.Fatal(err) |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +## Certificate Management |
| 196 | + |
| 197 | +### Certificate Structure |
| 198 | + |
| 199 | +Certificates are represented by the `Certificate` type: |
| 200 | + |
| 201 | +```go |
| 202 | +type Certificate struct { |
| 203 | + Type string // Base64-encoded certificate type ID |
| 204 | + SerialNumber string // Base64-encoded unique serial number |
| 205 | + Subject *ec.PublicKey // Public key of the certificate subject |
| 206 | + Certifier *ec.PublicKey // Public key of the certificate issuer |
| 207 | + RevocationOutpoint string // Format: "txid:outputIndex" |
| 208 | + Fields map[string]string // Field name -> field value (encrypted) |
| 209 | + Signature string // Hex-encoded signature |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +### Acquiring Certificates |
| 214 | + |
| 215 | +Use `AcquireCertificate` to obtain a new certificate: |
| 216 | + |
| 217 | +```go |
| 218 | +type AcquireCertificateArgs struct { |
| 219 | + Type string // Base64-encoded certificate type ID |
| 220 | + Certifier string // Certifier's public key |
| 221 | + AcquisitionProtocol AcquisitionProtocol // "direct" or "issuance" |
| 222 | + Fields map[string]string // Field name -> field value pairs |
| 223 | + SerialNumber string // Unique serial number |
| 224 | + RevocationOutpoint string // Optional: Format "txid:outputIndex" |
| 225 | + Signature string // Optional: Hex-encoded signature |
| 226 | + CertifierUrl string // Optional: URL of the certifier |
| 227 | + KeyringRevealer string // Optional: "certifier" or PubKeyHex |
| 228 | + KeyringForSubject map[string]string // Optional: Field name -> keyring mapping |
| 229 | + Privileged *bool // Optional: Whether this is a privileged operation |
| 230 | + PrivilegedReason string // Optional: Reason for privileged access |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +Example: |
| 235 | +```go |
| 236 | +// Acquire certificate |
| 237 | +acquireArgs := wallet.AcquireCertificateArgs{ |
| 238 | + Type: "example-cert", |
| 239 | + Certifier: "certifier-pubkey", |
| 240 | + AcquisitionProtocol: wallet.AcquisitionProtocolDirect, |
| 241 | + Fields: map[string]string{ |
| 242 | + "name": "Example", |
| 243 | + }, |
| 244 | +} |
| 245 | + |
| 246 | +cert, err := walletClient.AcquireCertificate(ctx, acquireArgs, "example-app") |
| 247 | +if err != nil { |
| 248 | + log.Fatal(err) |
| 249 | +} |
| 250 | +``` |
| 251 | + |
| 252 | +### Managing Certificates |
| 253 | + |
| 254 | +List and manage certificates using the provided methods: |
| 255 | + |
| 256 | +```go |
| 257 | +// List certificates |
| 258 | +listArgs := wallet.ListCertificatesArgs{ |
| 259 | + Types: []string{"example-cert"}, |
| 260 | + Limit: 10, |
| 261 | + Offset: 0, |
| 262 | +} |
| 263 | + |
| 264 | +certs, err := walletClient.ListCertificates(ctx, listArgs, "example-app") |
| 265 | +if err != nil { |
| 266 | + log.Fatal(err) |
| 267 | +} |
| 268 | +``` |
| 269 | + |
| 270 | +## Output Management |
| 271 | + |
| 272 | +### Listing Outputs |
| 273 | + |
| 274 | +Use `ListOutputs` to query available outputs: |
| 275 | + |
| 276 | +```go |
| 277 | +type ListOutputsArgs struct { |
| 278 | + Basket string |
| 279 | + Tags []string |
| 280 | + TagQueryMode QueryMode // "any" or "all" |
| 281 | + Include OutputInclude // "locking scripts" or "entire transactions" |
| 282 | + IncludeCustomInstructions *bool |
| 283 | + IncludeTags *bool |
| 284 | + IncludeLabels *bool |
| 285 | + Limit uint32 // Default 10, max 10000 |
| 286 | + Offset uint32 |
| 287 | + SeekPermission *bool // Default true |
| 288 | +} |
| 289 | +``` |
0 commit comments