-
Notifications
You must be signed in to change notification settings - Fork 25
Feat/shielded outputs #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 6 commits
3e68140
134491b
4e54c36
97b7d69
fdaad87
e6da778
3cb794a
2e8dfb8
d810ef8
44bce3f
dd6460e
a006544
25a9b43
ef45633
dd9296b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #import <React/RCTBridgeModule.h> | ||
|
|
||
| @interface RCT_EXTERN_MODULE(HathorCtCryptoModule, NSObject) | ||
|
|
||
| RCT_EXTERN_METHOD(createShieldedOutput:(double)value | ||
| recipientPubkey:(NSArray *)recipientPubkey | ||
| tokenUid:(NSArray *)tokenUid | ||
| fullyShielded:(BOOL)fullyShielded | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(decryptShieldedOutput:(NSArray *)recipientPrivkey | ||
| ephemeralPubkey:(NSArray *)ephemeralPubkey | ||
| commitment:(NSArray *)commitment | ||
| rangeProof:(NSArray *)rangeProof | ||
| tokenUid:(NSArray *)tokenUid | ||
| assetCommitment:(NSArray *)assetCommitment | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(deriveEcdhSharedSecret:(NSArray *)privkey | ||
| pubkey:(NSArray *)pubkey | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(deriveTag:(NSArray *)tokenUid | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(createAssetCommitment:(NSArray *)tag | ||
| blindingFactor:(NSArray *)blindingFactor | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(createSurjectionProof:(NSArray *)codomainTag | ||
| codomainBlindingFactor:(NSArray *)codomainBlindingFactor | ||
| domain:(NSArray *)domain | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(computeBalancingBlindingFactor:(NSArray *)otherBlindingFactors | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(createShieldedOutputWithBlinding:(double)value | ||
| recipientPubkey:(NSArray *)recipientPubkey | ||
| tokenUid:(NSArray *)tokenUid | ||
| fullyShielded:(BOOL)fullyShielded | ||
| blindingFactor:(NSArray *)blindingFactor | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(createShieldedOutputWithBothBlindings:(double)value | ||
| recipientPubkey:(NSArray *)recipientPubkey | ||
| tokenUid:(NSArray *)tokenUid | ||
| valueBlindingFactor:(NSArray *)valueBlindingFactor | ||
| assetBlindingFactor:(NSArray *)assetBlindingFactor | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| RCT_EXTERN_METHOD(computeBalancingBlindingFactorFull:(double)value | ||
| generatorBlindingFactor:(NSArray *)generatorBlindingFactor | ||
| inputs:(NSArray *)inputs | ||
| otherOutputs:(NSArray *)otherOutputs | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject) | ||
|
|
||
| @end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,258 @@ | ||
| import Foundation | ||
| import React | ||
|
|
||
| @objc(HathorCtCryptoModule) | ||
| class HathorCtCryptoModule: NSObject { | ||
|
|
||
| @objc static func requiresMainQueueSetup() -> Bool { false } | ||
|
|
||
| private func toData(_ arr: [Any]) -> Data { | ||
| Data(arr.compactMap { ($0 as? NSNumber)?.uint8Value }) | ||
| } | ||
|
|
||
| private func toArray(_ data: Data) -> [NSNumber] { | ||
| data.map { NSNumber(value: $0) } | ||
| } | ||
|
|
||
| @objc func createShieldedOutput( | ||
| _ value: Double, | ||
| recipientPubkey: [Any], | ||
| tokenUid: [Any], | ||
| fullyShielded: Bool, | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let result = try createShieldedOutputUniffi( | ||
| value: UInt64(value), | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| recipientPubkey: toData(recipientPubkey), | ||
| tokenUid: toData(tokenUid), | ||
| fullyShielded: fullyShielded | ||
| ) | ||
| resolve([ | ||
| "ephemeralPubkey": toArray(result.ephemeralPubkey), | ||
| "commitment": toArray(result.commitment), | ||
| "rangeProof": toArray(result.rangeProof), | ||
| "blindingFactor": toArray(result.blindingFactor), | ||
| "assetCommitment": result.assetCommitment.map { toArray($0) } as Any, | ||
| "assetBlindingFactor": result.assetBlindingFactor.map { toArray($0) } as Any, | ||
| ]) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func decryptShieldedOutput( | ||
| _ recipientPrivkey: [Any], | ||
| ephemeralPubkey: [Any], | ||
| commitment: [Any], | ||
| rangeProof: [Any], | ||
| tokenUid: [Any], | ||
| assetCommitment: [Any], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| // JS callers pass [] (instead of null) for the absent parameter, since | ||
| // the RN bridge can't marshal JS null → NSArray. Treat empty arrays as | ||
| // nil before forwarding to UniFFI. | ||
| // - tokenUid is empty for FullShielded (recovered from rangeproof message). | ||
| // - assetCommitment is empty for AmountShielded (generator from tokenUid). | ||
| let tuid: Data? = tokenUid.isEmpty ? nil : toData(tokenUid) | ||
| let ac: Data? = assetCommitment.isEmpty ? nil : toData(assetCommitment) | ||
| let result = try decryptShieldedOutputUniffi( | ||
| recipientPrivkey: toData(recipientPrivkey), | ||
| ephemeralPubkey: toData(ephemeralPubkey), | ||
| commitment: toData(commitment), | ||
| rangeProof: toData(rangeProof), | ||
| tokenUid: tuid, | ||
| assetCommitment: ac | ||
| ) | ||
| resolve([ | ||
| "value": NSNumber(value: result.value), | ||
| "blindingFactor": toArray(result.blindingFactor), | ||
| "tokenUid": toArray(result.tokenUid), | ||
| "assetBlindingFactor": result.assetBlindingFactor.map { toArray($0) } as Any, | ||
| "outputType": result.outputType, | ||
| ]) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func deriveEcdhSharedSecret( | ||
| _ privkey: [Any], | ||
| pubkey: [Any], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let result = try deriveEcdhSharedSecretUniffi(privkey: toData(privkey), pubkey: toData(pubkey)) | ||
| resolve(toArray(result)) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func deriveTag( | ||
| _ tokenUid: [Any], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let result = try deriveTagUniffi(tokenUid: toData(tokenUid)) | ||
| resolve(toArray(result)) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func createAssetCommitment( | ||
| _ tag: [Any], | ||
| blindingFactor: [Any], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let result = try createAssetCommitmentUniffi(tag: toData(tag), blindingFactor: toData(blindingFactor)) | ||
| resolve(toArray(result)) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func createSurjectionProof( | ||
| _ codomainTag: [Any], | ||
| codomainBlindingFactor: [Any], | ||
| domain: [[String: Any]], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let domainEntries = domain.map { entry -> SurjectionDomainEntry in | ||
| SurjectionDomainEntry( | ||
| generator: self.toData(entry["generator"] as! [Any]), | ||
| tag: self.toData(entry["tag"] as! [Any]), | ||
| blindingFactor: self.toData(entry["blindingFactor"] as! [Any]) | ||
| ) | ||
| } | ||
| let result = try createSurjectionProofUniffi( | ||
| codomainTag: toData(codomainTag), | ||
| codomainBlindingFactor: toData(codomainBlindingFactor), | ||
| domain: domainEntries | ||
| ) | ||
| resolve(toArray(result)) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
Comment on lines
+189
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Force-cast dictionary lookups crash the app instead of rejecting the promise The 🐛 Proposed fix — safe unwrapping with rejection- do {
- let domainEntries = domain.map { entry -> SurjectionDomainEntry in
- SurjectionDomainEntry(
- generator: self.toData(entry["generator"] as! [Any]),
- tag: self.toData(entry["tag"] as! [Any]),
- blindingFactor: self.toData(entry["blindingFactor"] as! [Any])
- )
- }
+ do {
+ guard domain.allSatisfy({ $0["generator"] is [Any] && $0["tag"] is [Any] && $0["blindingFactor"] is [Any] }) else {
+ reject("INVALID_INPUT", "domain entries must contain generator, tag, and blindingFactor arrays", nil)
+ return
+ }
+ let domainEntries = domain.map { entry -> SurjectionDomainEntry in
+ SurjectionDomainEntry(
+ generator: self.toData(entry["generator"] as! [Any]),
+ tag: self.toData(entry["tag"] as! [Any]),
+ blindingFactor: self.toData(entry["blindingFactor"] as! [Any])
+ )
+ }Apply the same pattern (pre-validate then cast) for the Also applies to: 233-246 🤖 Prompt for AI Agents |
||
|
|
||
| @objc func computeBalancingBlindingFactor( | ||
| _ otherBlindingFactors: [[Any]], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let bfs = otherBlindingFactors.map { toData($0) } | ||
| let result = try computeBalancingBlindingFactorUniffi(otherBlindingFactors: bfs) | ||
| resolve(toArray(result)) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func createShieldedOutputWithBlinding( | ||
| _ value: Double, | ||
| recipientPubkey: [Any], | ||
| tokenUid: [Any], | ||
| fullyShielded: Bool, | ||
| blindingFactor: [Any], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let result = try createShieldedOutputWithBlindingUniffi( | ||
| value: UInt64(value), | ||
| recipientPubkey: toData(recipientPubkey), | ||
| tokenUid: toData(tokenUid), | ||
| fullyShielded: fullyShielded, | ||
| blindingFactor: toData(blindingFactor) | ||
| ) | ||
| resolve([ | ||
| "ephemeralPubkey": toArray(result.ephemeralPubkey), | ||
| "commitment": toArray(result.commitment), | ||
| "rangeProof": toArray(result.rangeProof), | ||
| "blindingFactor": toArray(result.blindingFactor), | ||
| "assetCommitment": result.assetCommitment.map { toArray($0) } as Any, | ||
| "assetBlindingFactor": result.assetBlindingFactor.map { toArray($0) } as Any, | ||
| ]) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func createShieldedOutputWithBothBlindings( | ||
| _ value: Double, | ||
| recipientPubkey: [Any], | ||
| tokenUid: [Any], | ||
| valueBlindingFactor: [Any], | ||
| assetBlindingFactor: [Any], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let result = try createShieldedOutputWithBothBlindingsUniffi( | ||
| value: UInt64(value), | ||
| recipientPubkey: toData(recipientPubkey), | ||
| tokenUid: toData(tokenUid), | ||
| valueBlindingFactor: toData(valueBlindingFactor), | ||
| assetBlindingFactor: toData(assetBlindingFactor) | ||
| ) | ||
| resolve([ | ||
| "ephemeralPubkey": toArray(result.ephemeralPubkey), | ||
| "commitment": toArray(result.commitment), | ||
| "rangeProof": toArray(result.rangeProof), | ||
| "blindingFactor": toArray(result.blindingFactor), | ||
| "assetCommitment": result.assetCommitment.map { toArray($0) } as Any, | ||
| "assetBlindingFactor": result.assetBlindingFactor.map { toArray($0) } as Any, | ||
| ]) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
|
|
||
| @objc func computeBalancingBlindingFactorFull( | ||
| _ value: Double, | ||
| generatorBlindingFactor: [Any], | ||
| inputs: [[String: Any]], | ||
| otherOutputs: [[String: Any]], | ||
| resolve: RCTPromiseResolveBlock, | ||
| reject: RCTPromiseRejectBlock | ||
| ) { | ||
| do { | ||
| let inEntries = inputs.map { entry -> BlindingEntry in | ||
| BlindingEntry( | ||
| value: (entry["value"] as! NSNumber).uint64Value, | ||
| vbf: self.toData(entry["vbf"] as! [Any]), | ||
| gbf: self.toData(entry["gbf"] as! [Any]) | ||
| ) | ||
| } | ||
| let outEntries = otherOutputs.map { entry -> BlindingEntry in | ||
| BlindingEntry( | ||
| value: (entry["value"] as! NSNumber).uint64Value, | ||
| vbf: self.toData(entry["vbf"] as! [Any]), | ||
| gbf: self.toData(entry["gbf"] as! [Any]) | ||
| ) | ||
| } | ||
| let result = try computeBalancingBlindingFactorFullUniffi( | ||
| value: UInt64(value), | ||
| generatorBlindingFactor: toData(generatorBlindingFactor), | ||
| inputs: inEntries, | ||
| otherOutputs: outEntries | ||
| ) | ||
| resolve(toArray(result)) | ||
| } catch { | ||
| reject("CRYPTO_ERROR", error.localizedDescription, error) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toDatasilently drops non-NSNumberbytes — cryptographic truncation with no errorcompactMap { ($0 as? NSNumber)?.uint8Value }discards any element that isn't anNSNumber. For a 32-byte key where one element is unexpectedly not anNSNumber, the crypto call proceeds with a 31-byte buffer. The UniFFI/Rust layer will then produce a cryptographic error whose message won't indicate the real cause (wrong input length).🛡️ Proposed fix — reject on type mismatch
All call sites already sit inside
do/catchblocks that forward errors toreject(...).🤖 Prompt for AI Agents