Skip to content

Commit 1bbac71

Browse files
author
JACOB STANLEY
committed
https://github.com/jaynomyaro/SYNCRO.git
1 parent 73d7ca9 commit 1bbac71

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

contracts/contracts/src/subscription_registry.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use soroban_sdk::{
2-
contract, contractevent, contractimpl, contracttype, vec, xdr::ToXdr, Address, BytesN, Env,
2+
contract, contractevent, contractimpl, contracttype, vec, xdr::ToXdr, Address, Bytes, BytesN, Env,
33
String, Vec,
44
};
55

@@ -11,6 +11,7 @@ pub struct SubscriptionMetadata {
1111
pub expected_amount: i128,
1212
pub next_renewal: u64,
1313
pub is_active: bool,
14+
pub encrypted_blob: Bytes,
1415
}
1516

1617
#[contracttype]
@@ -30,6 +31,7 @@ pub struct SubscriptionCreatedEvent {
3031
pub billing_interval: u64,
3132
pub expected_amount: i128,
3233
pub next_renewal: u64,
34+
pub encrypted_blob: Bytes,
3335
}
3436

3537
#[contractevent]
@@ -41,6 +43,7 @@ pub struct SubscriptionUpdatedEvent {
4143
pub billing_interval: u64,
4244
pub expected_amount: i128,
4345
pub next_renewal: u64,
46+
pub encrypted_blob: Bytes,
4447
}
4548

4649
#[contractevent]
@@ -64,6 +67,7 @@ impl SubscriptionRegistry {
6467
billing_interval: u64,
6568
expected_amount: i128,
6669
next_renewal: u64,
70+
encrypted_blob: Bytes,
6771
) -> BytesN<32> {
6872
user.require_auth();
6973
if billing_interval == 0 {
@@ -116,6 +120,7 @@ impl SubscriptionRegistry {
116120
expected_amount,
117121
next_renewal,
118122
is_active: true,
123+
encrypted_blob: encrypted_blob.clone(),
119124
};
120125
env.storage()
121126
.instance()
@@ -133,6 +138,7 @@ impl SubscriptionRegistry {
133138
billing_interval,
134139
expected_amount,
135140
next_renewal,
141+
encrypted_blob: encrypted_blob.clone(),
136142
}
137143
.publish(&env);
138144

@@ -148,6 +154,7 @@ impl SubscriptionRegistry {
148154
billing_interval: Option<u64>,
149155
expected_amount: Option<i128>,
150156
next_renewal: Option<u64>,
157+
encrypted_blob: Option<Bytes>,
151158
) {
152159
user.require_auth();
153160
let mut metadata: SubscriptionMetadata = env
@@ -181,6 +188,9 @@ impl SubscriptionRegistry {
181188
}
182189
metadata.next_renewal = nr;
183190
}
191+
if let Some(eb) = encrypted_blob {
192+
metadata.encrypted_blob = eb;
193+
}
184194

185195
env.storage()
186196
.instance()
@@ -193,6 +203,7 @@ impl SubscriptionRegistry {
193203
billing_interval: metadata.billing_interval,
194204
expected_amount: metadata.expected_amount,
195205
next_renewal: metadata.next_renewal,
206+
encrypted_blob: metadata.encrypted_blob.clone(),
196207
}
197208
.publish(&env);
198209
}

contracts/contracts/tests/subscription-registry-test.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use soroban_sdk::{testutils::Address as _, Address, BytesN, Env, String};
1+
use soroban_sdk::{testutils::Address as _, Address, Bytes, BytesN, Env, String};
22
use subscription_registry::{SubscriptionRegistry, SubscriptionRegistryClient};
33

44
#[test]
@@ -14,13 +14,15 @@ fn test_create_subscription() {
1414
let billing_interval = 2592000u64;
1515
let expected_amount = 1599i128;
1616
let next_renewal = 1735689600u64;
17+
let encrypted_blob = Bytes::from_slice(&env, &[1u8, 2, 3, 4, 5]);
1718

1819
let subscription_id = client.create_subscription(
1920
&user,
2021
&service_id,
2122
&billing_interval,
2223
&expected_amount,
2324
&next_renewal,
25+
&encrypted_blob,
2426
);
2527

2628
// Verify subscription metadata was stored correctly
@@ -30,6 +32,7 @@ fn test_create_subscription() {
3032
assert_eq!(metadata.expected_amount, expected_amount);
3133
assert_eq!(metadata.next_renewal, next_renewal);
3234
assert!(metadata.is_active);
35+
assert_eq!(metadata.encrypted_blob, encrypted_blob);
3336

3437
// Verify subscription is mapped to user
3538
let user_subs = client.get_user_subscriptions(&user);
@@ -53,6 +56,7 @@ fn test_create_multiple_subscriptions() {
5356
&2592000u64,
5457
&1599i128,
5558
&1735689600u64,
59+
&Bytes::from_slice(&env, &[1u8, 2, 3]),
5660
);
5761

5862
let sub2_id = client.create_subscription(
@@ -61,6 +65,7 @@ fn test_create_multiple_subscriptions() {
6165
&2592000u64,
6266
&999i128,
6367
&1735689600u64,
68+
&Bytes::from_slice(&env, &[4u8, 5, 6]),
6469
);
6570

6671
let sub3_id = client.create_subscription(
@@ -69,6 +74,7 @@ fn test_create_multiple_subscriptions() {
6974
&2592000u64,
7075
&799i128,
7176
&1735689600u64,
77+
&Bytes::from_slice(&env, &[7u8, 8, 9]),
7278
);
7379

7480
// Verify all subscriptions are associated with the user
@@ -88,30 +94,35 @@ fn test_update_subscription() {
8894
let client = SubscriptionRegistryClient::new(&env, &contract_id);
8995

9096
let user = Address::generate(&env);
97+
let encrypted_blob = Bytes::from_slice(&env, &[1u8, 2, 3, 4, 5]);
9198
let subscription_id = client.create_subscription(
9299
&user,
93100
&String::from_str(&env, "netflix"),
94101
&2592000u64,
95102
&1599i128,
96103
&1735689600u64,
104+
&encrypted_blob,
97105
);
98106

99107
// Update amount and renewal date
100108
let new_amount = 1799i128;
101109
let new_renewal = 1738281600u64;
110+
let new_encrypted_blob = Bytes::from_slice(&env, &[10u8, 11, 12]);
102111
client.update_subscription(
103112
&subscription_id,
104113
&user,
105114
&None,
106115
&None,
107116
&Some(new_amount),
108117
&Some(new_renewal),
118+
&Some(new_encrypted_blob),
109119
);
110120

111121
// Verify updates were applied
112122
let metadata = client.get_subscription(&subscription_id).unwrap();
113123
assert_eq!(metadata.expected_amount, new_amount);
114124
assert_eq!(metadata.next_renewal, new_renewal);
125+
assert_eq!(metadata.encrypted_blob, new_encrypted_blob);
115126
}
116127

117128
#[test]
@@ -123,12 +134,14 @@ fn test_cancel_subscription() {
123134
let client = SubscriptionRegistryClient::new(&env, &contract_id);
124135

125136
let user = Address::generate(&env);
137+
let encrypted_blob = Bytes::from_slice(&env, &[1u8, 2, 3]);
126138
let subscription_id = client.create_subscription(
127139
&user,
128140
&String::from_str(&env, "netflix"),
129141
&2592000u64,
130142
&1599i128,
131143
&1735689600u64,
144+
&encrypted_blob,
132145
);
133146

134147
client.cancel_subscription(&subscription_id, &user);
@@ -154,6 +167,7 @@ fn test_create_subscription_invalid_billing_interval() {
154167
&0u64,
155168
&1599i128,
156169
&1735689600u64,
170+
&Bytes::from_slice(&env, &[1u8, 2, 3]),
157171
);
158172
}
159173

@@ -173,6 +187,7 @@ fn test_create_subscription_negative_amount() {
173187
&2592000u64,
174188
&-100i128,
175189
&1735689600u64,
190+
&Bytes::from_slice(&env, &[1u8, 2, 3]),
176191
);
177192
}
178193

@@ -188,7 +203,7 @@ fn test_update_nonexistent_subscription() {
188203
let user = Address::generate(&env);
189204
let fake_id = BytesN::from_array(&env, &[0u8; 32]);
190205

191-
client.update_subscription(&fake_id, &user, &None, &None, &Some(1999i128), &None);
206+
client.update_subscription(&fake_id, &user, &None, &None, &Some(1999i128), &None, &None);
192207
}
193208

194209
#[test]
@@ -201,12 +216,14 @@ fn test_cancel_already_cancelled_subscription() {
201216
let client = SubscriptionRegistryClient::new(&env, &contract_id);
202217

203218
let user = Address::generate(&env);
219+
let encrypted_blob = Bytes::from_slice(&env, &[1u8, 2, 3]);
204220
let subscription_id = client.create_subscription(
205221
&user,
206222
&String::from_str(&env, "netflix"),
207223
&2592000u64,
208224
&1599i128,
209225
&1735689600u64,
226+
&encrypted_blob,
210227
);
211228

212229
client.cancel_subscription(&subscription_id, &user);
@@ -223,12 +240,14 @@ fn test_update_cancelled_subscription() {
223240
let client = SubscriptionRegistryClient::new(&env, &contract_id);
224241

225242
let user = Address::generate(&env);
243+
let encrypted_blob = Bytes::from_slice(&env, &[1u8, 2, 3]);
226244
let subscription_id = client.create_subscription(
227245
&user,
228246
&String::from_str(&env, "netflix"),
229247
&2592000u64,
230248
&1599i128,
231249
&1735689600u64,
250+
&encrypted_blob,
232251
);
233252

234253
client.cancel_subscription(&subscription_id, &user);
@@ -239,6 +258,7 @@ fn test_update_cancelled_subscription() {
239258
&None,
240259
&Some(1999i128),
241260
&None,
261+
&None,
242262
);
243263
}
244264

@@ -272,6 +292,7 @@ fn test_multiple_users_independent() {
272292
&2592000u64,
273293
&1599i128,
274294
&1735689600u64,
295+
&Bytes::from_slice(&env, &[1u8, 2, 3]),
275296
);
276297

277298
let sub2_id = client.create_subscription(
@@ -280,6 +301,7 @@ fn test_multiple_users_independent() {
280301
&2592000u64,
281302
&999i128,
282303
&1735689600u64,
304+
&Bytes::from_slice(&env, &[4u8, 5, 6]),
283305
);
284306

285307
// Verify users have separate subscription lists
@@ -308,20 +330,23 @@ fn test_subscription_id_uniqueness() {
308330
&2592000u64,
309331
&1599i128,
310332
&1735689600u64,
333+
&Bytes::from_slice(&env, &[1u8, 2, 3]),
311334
);
312335
let sub2_id = client.create_subscription(
313336
&user,
314337
&String::from_str(&env, "spotify"),
315338
&2592000u64,
316339
&999i128,
317340
&1735689600u64,
341+
&Bytes::from_slice(&env, &[4u8, 5, 6]),
318342
);
319343
let sub3_id = client.create_subscription(
320344
&user,
321345
&String::from_str(&env, "hulu"),
322346
&2592000u64,
323347
&799i128,
324348
&1735689600u64,
349+
&Bytes::from_slice(&env, &[7u8, 8, 9]),
325350
);
326351

327352
// Verify all subscription IDs are unique
@@ -347,6 +372,7 @@ fn test_create_duplicate_subscription_fails() {
347372
&2592000u64,
348373
&1599i128,
349374
&1735689600u64,
375+
&Bytes::from_slice(&env, &[1u8, 2, 3]),
350376
);
351377

352378
// This second one should panic
@@ -356,6 +382,7 @@ fn test_create_duplicate_subscription_fails() {
356382
&2592000u64,
357383
&1599i128,
358384
&1735689600u64,
385+
&Bytes::from_slice(&env, &[4u8, 5, 6]),
359386
);
360387
}
361388

@@ -368,12 +395,14 @@ fn test_update_subscription_unauthorized() {
368395
let client = SubscriptionRegistryClient::new(&env, &contract_id);
369396

370397
let user = Address::generate(&env);
398+
let encrypted_blob = Bytes::from_slice(&env, &[1u8, 2, 3]);
371399
let subscription_id = client.create_subscription(
372400
&user,
373401
&String::from_str(&env, "netflix"),
374402
&2592000u64,
375403
&1599i128,
376404
&1735689600u64,
405+
&encrypted_blob,
377406
);
378407

379408
// Clear mock auths to simulate unauthorized user
@@ -387,5 +416,6 @@ fn test_update_subscription_unauthorized() {
387416
&None,
388417
&Some(1999i128),
389418
&None,
419+
&None,
390420
);
391421
}

0 commit comments

Comments
 (0)