-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventSourcedWalletExample.java
More file actions
280 lines (231 loc) · 10.7 KB
/
EventSourcedWalletExample.java
File metadata and controls
280 lines (231 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package examples;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.pekko.actor.typed.ActorSystem;
import org.apache.pekko.cluster.sharding.typed.javadsl.ClusterSharding;
import org.apache.pekko.cluster.sharding.typed.javadsl.EntityRef;
import org.twostack.libspiffy4j.LibSpiffy4j;
import org.twostack.libspiffy4j.aggregate.wallet.WalletAggregate;
import org.twostack.libspiffy4j.aggregate.wallet.WalletCommand;
import org.twostack.libspiffy4j.aggregate.wallet.WalletReply;
import org.twostack.libspiffy4j.aggregate.invoice.InvoiceAggregate;
import org.twostack.libspiffy4j.aggregate.invoice.InvoiceCommand;
import org.twostack.libspiffy4j.model.*;
import org.twostack.libspiffy4j.service.CryptoService;
import org.twostack.libspiffy4j.service.EncryptionService;
import org.twostack.libspiffy4j.storage.postgres.WalletReadModelStorage;
import org.twostack.libspiffy4j.storage.postgres.InvoiceReadModelStorage;
import javax.sql.DataSource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
/**
* Demonstrates the full event-sourced layer: LibSpiffy4j bootstrap, wallet aggregate
* commands, invoice creation, and read model queries.
*
* Requires: PostgreSQL database with Pekko persistence schema.
*
* Covers:
* - LibSpiffy4j initialization and lifecycle
* - Wallet creation via aggregate command
* - Address and UTXO recording
* - Invoice creation
* - Read model queries (WalletReadModelStorage, InvoiceReadModelStorage)
* - Secure key storage
*/
public class EventSourcedWalletExample {
public static void main(String[] args) throws Exception {
// ---------------------------------------------------------------
// 1. Initialize LibSpiffy4j
// ---------------------------------------------------------------
// You must provide a PostgreSQL DataSource (e.g., HikariCP)
DataSource dataSource = createDataSource();
var libSpiffy = LibSpiffy4j.builder()
.dataSource(dataSource)
.objectMapper(new ObjectMapper())
.encryptionMasterKey(EncryptionService.generateMasterKey())
.build();
ActorSystem<Void> system = libSpiffy.system();
ClusterSharding sharding = ClusterSharding.get(system);
var crypto = libSpiffy.cryptoService();
var encryption = libSpiffy.encryptionService();
var secureStorage = libSpiffy.secureStorage();
// ---------------------------------------------------------------
// 2. Create a Wallet (Aggregate Command)
// ---------------------------------------------------------------
// Generate keys
List<String> mnemonic = crypto.generateMnemonic();
var hdKey = crypto.mnemonicToHDPrivateKey(mnemonic, "");
var key0 = crypto.derivePrivateKey(hdKey, 0, 0, 0, false);
String rootAddress = crypto.generateAddress(key0, NetworkType.MAINNET);
String walletId = UUID.randomUUID().toString();
// Get entity reference for this wallet
EntityRef<WalletCommand> walletRef = sharding.entityRefFor(
WalletAggregate.ENTITY_TYPE_KEY, walletId
);
// Send CreateWalletCommand
CompletionStage<WalletReply> createReply = walletRef.ask(
replyTo -> new WalletCommand.CreateWalletCommand(
walletId,
"My First Wallet",
rootAddress,
WalletType.HD,
NetworkType.MAINNET,
Map.of("source", "example"),
replyTo
),
Duration.ofSeconds(10)
);
WalletReply reply = createReply.toCompletableFuture().join();
System.out.println("Create wallet reply: " + reply);
// ---------------------------------------------------------------
// 3. Store Encrypted Key (SecureStorage)
// ---------------------------------------------------------------
// Encrypt the HD key for persistent storage
byte[] hdKeyBytes = hdKey.serializePrivate();
var encrypted = encryption.encrypt(hdKeyBytes, "wallet:" + walletId + ":hdkey");
// Store requires a JDBC Connection (for transaction control)
try (var conn = dataSource.getConnection()) {
secureStorage.storeEncryptedKey(
conn, walletId, "hdkey",
encrypted.ciphertext(), encrypted.nonce(), 1
);
conn.commit();
}
// ---------------------------------------------------------------
// 4. Record Addresses
// ---------------------------------------------------------------
// Derive and record multiple addresses
for (int i = 0; i < 5; i++) {
var key = crypto.derivePrivateKey(hdKey, 0, i, 0, false);
String address = crypto.generateAddress(key, NetworkType.MAINNET);
walletRef.ask(
replyTo -> new WalletCommand.RecordAddressCommand(
walletId, address, replyTo
),
Duration.ofSeconds(10)
).toCompletableFuture().join();
System.out.println("Recorded address " + i + ": " + address);
}
// ---------------------------------------------------------------
// 5. Record a UTXO
// ---------------------------------------------------------------
var utxo = new BitcoinUtxo(
"abc123def456...", 0, 100_000L,
"", rootAddress,
UtxoStatus.AVAILABLE,
850_000, 6,
Instant.now(), Instant.now(),
null, null, null, null, 0
);
walletRef.ask(
replyTo -> new WalletCommand.RecordUtxoCommand(
walletId, utxo, replyTo
),
Duration.ofSeconds(10)
).toCompletableFuture().join();
System.out.println("Recorded UTXO: " + utxo.key());
// ---------------------------------------------------------------
// 6. Reserve a UTXO (for spending)
// ---------------------------------------------------------------
walletRef.ask(
replyTo -> new WalletCommand.ReserveUtxoCommand(
walletId,
utxo.key(), // "txid:vout"
"spending-tx-id", // reserving transaction
Instant.now().plus(Duration.ofMinutes(5)), // expiration
1, // priority
"building payment tx", // reason
replyTo
),
Duration.ofSeconds(10)
).toCompletableFuture().join();
System.out.println("Reserved UTXO for spending");
// ---------------------------------------------------------------
// 7. Create an Invoice
// ---------------------------------------------------------------
String invoiceId = UUID.randomUUID().toString();
EntityRef<InvoiceCommand> invoiceRef = sharding.entityRefFor(
InvoiceAggregate.ENTITY_TYPE_KEY, invoiceId
);
String paymentAddress = crypto.generateAddress(
crypto.derivePrivateKey(hdKey, 0, 5, 0, false), NetworkType.MAINNET
);
invoiceRef.ask(
replyTo -> new InvoiceCommand.CreateInvoiceCommand(
invoiceId,
walletId,
List.of(paymentAddress),
50_000L,
List.of(new InvoiceOutputSpec.P2PKHOutputSpec(paymentAddress, 50_000L, "order-456")),
"Payment for Order #456",
Instant.now().plus(Duration.ofHours(24)),
Map.of("orderId", "456"),
replyTo
),
Duration.ofSeconds(10)
).toCompletableFuture().join();
System.out.println("Created invoice: " + invoiceId);
// ---------------------------------------------------------------
// 8. Query Read Models
// ---------------------------------------------------------------
// Allow a moment for projections to update the read models
Thread.sleep(1000);
var walletStorage = new WalletReadModelStorage();
var invoiceStorage = new InvoiceReadModelStorage();
// Wallet summary
Optional<WalletSummary> summary = walletStorage.findWalletSummary(dataSource, walletId);
summary.ifPresent(s -> {
System.out.println("\nWallet summary:");
System.out.println(" Name: " + s.name());
System.out.println(" Type: " + s.walletType());
System.out.println(" Confirmed: " + s.confirmedBalanceSats() + " sats");
System.out.println(" UTXOs: " + s.utxoCount());
System.out.println(" Addresses: " + s.addressCount());
});
// List UTXOs by status
List<BitcoinUtxo> available = walletStorage.findUtxosByStatus(
dataSource, walletId, UtxoStatus.AVAILABLE
);
System.out.println("Available UTXOs: " + available.size());
// Wallet balance
walletStorage.getWalletBalance(dataSource, walletId).ifPresent(b -> {
System.out.println("Balance: " + b);
});
// Invoice queries
Optional<Invoice> invoice = invoiceStorage.findInvoice(dataSource, invoiceId);
invoice.ifPresent(inv -> {
System.out.println("\nInvoice: " + inv.invoiceId());
System.out.println(" Status: " + inv.status());
System.out.println(" Amount: " + inv.amountSats() + " sats");
System.out.println(" Expires: " + inv.expiresAt());
});
// List pending invoices
List<Invoice> pendingInvoices = invoiceStorage.listInvoices(
dataSource, walletId, InvoiceStatus.PENDING, 50, 0
);
System.out.println("Pending invoices: " + pendingInvoices.size());
// ---------------------------------------------------------------
// 9. Cleanup
// ---------------------------------------------------------------
libSpiffy.close();
System.out.println("\nLibSpiffy4j shut down.");
}
/** Placeholder — replace with your actual DataSource (e.g., HikariDataSource). */
private static DataSource createDataSource() {
// Example with HikariCP:
//
// var config = new HikariConfig();
// config.setJdbcUrl("jdbc:postgresql://localhost:5432/spiffy");
// config.setUsername("spiffy");
// config.setPassword("spiffy");
// config.setAutoCommit(false);
// return new HikariDataSource(config);
throw new UnsupportedOperationException(
"Replace this method with your actual PostgreSQL DataSource"
);
}
}