diff --git a/android/src/main/java/com/reactlibrary/IndySdkModule.java b/android/src/main/java/com/reactlibrary/IndySdkModule.java index 0167e4d..63a2962 100644 --- a/android/src/main/java/com/reactlibrary/IndySdkModule.java +++ b/android/src/main/java/com/reactlibrary/IndySdkModule.java @@ -16,6 +16,8 @@ package com.reactlibrary; +import android.annotation.TargetApi; + import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -39,38 +41,37 @@ import org.hyperledger.indy.sdk.pool.Pool; import org.hyperledger.indy.sdk.wallet.Wallet; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; +@TargetApi(24) public class IndySdkModule extends ReactContextBaseJavaModule { private final ReactApplicationContext reactContext; - private Pool pool; - private Wallet wallet; + private Map walletMap; + private Map poolMap; public IndySdkModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; + this.walletMap = new ConcurrentHashMap<>(); + this.poolMap = new ConcurrentHashMap<>(); } @Override public String getName() { - return "IndySdk"; - } - - @ReactMethod - public void sampleMethod(String stringArgument, int numberArgument, Promise promise) { - // TODO: Implement some actually useful functionality - promise.resolve("Received numberArgument: " + numberArgument + " stringArgument: " + stringArgument); + return "IndyBridge"; } // wallet @ReactMethod - public void createWallet(String config, String credentials, Promise promise) { + public void createWallet(String configJson, String credentialsJson, Promise promise) { try { - Wallet.createWallet(config, credentials).get(); - promise.resolve("Wallet has been created."); + Wallet.createWallet(configJson, credentialsJson).get(); + promise.resolve(null); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); promise.reject(rejectResponse.getCode(), rejectResponse.toJson(), e); @@ -78,10 +79,11 @@ public void createWallet(String config, String credentials, Promise promise) { } @ReactMethod - public void openWallet(String config, String credentials, Promise promise) { + public void openWallet(String configJson, String credentialsJson, Promise promise) { try { - this.wallet = Wallet.openWallet(config, credentials).get(); - promise.resolve(this.wallet.getWalletHandle()); + Wallet wallet = Wallet.openWallet(configJson, credentialsJson).get(); + walletMap.put(wallet.getWalletHandle(), wallet); + promise.resolve(wallet.getWalletHandle()); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); promise.reject(rejectResponse.getCode(), rejectResponse.toJson(), e); @@ -89,9 +91,11 @@ public void openWallet(String config, String credentials, Promise promise) { } @ReactMethod - public void closeWallet(Promise promise) { + public void closeWallet(int walletHandle, Promise promise) { try { - this.wallet.closeWallet().get(); + Wallet wallet = walletMap.get(walletHandle); + wallet.closeWallet().get(); + walletMap.remove(walletHandle); promise.resolve(null); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -100,9 +104,9 @@ public void closeWallet(Promise promise) { } @ReactMethod - public void deleteWallet(String config, String credentials, Promise promise) { + public void deleteWallet(String configJson, String credentialsJson, Promise promise) { try { - Wallet.deleteWallet(config, credentials).get(); + Wallet.deleteWallet(configJson, credentialsJson).get(); promise.resolve(null); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -113,9 +117,10 @@ public void deleteWallet(String config, String credentials, Promise promise) { // did @ReactMethod - public void createAndStoreMyDid(String didJson, Double wallet, Promise promise) { + public void createAndStoreMyDid(int walletHandle, String didJson, Promise promise) { try { - DidResults.CreateAndStoreMyDidResult createMyDidResult = Did.createAndStoreMyDid(this.wallet, didJson).get(); + Wallet wallet = walletMap.get(walletHandle); + DidResults.CreateAndStoreMyDidResult createMyDidResult = Did.createAndStoreMyDid(wallet, didJson).get(); String myDid = createMyDidResult.getDid(); String myVerkey = createMyDidResult.getVerkey(); WritableArray response = new WritableNativeArray(); @@ -129,9 +134,11 @@ public void createAndStoreMyDid(String didJson, Double wallet, Promise promise) } @ReactMethod - public void keyForDid(String did, Promise promise) { + public void keyForDid(int poolHandle, int walletHandle, String did, Promise promise) { try { - String receivedKey = Did.keyForDid(this.pool, this.wallet, did).get(); + Wallet wallet = walletMap.get(walletHandle); + Pool pool = poolMap.get(poolHandle); + String receivedKey = Did.keyForDid(pool, wallet, did).get(); promise.resolve(receivedKey); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -140,9 +147,10 @@ public void keyForDid(String did, Promise promise) { } @ReactMethod - public void keyForLocalDid(String did, Promise promise) { + public void keyForLocalDid(int walletHandle, String did, Promise promise) { try { - String receivedKey = Did.keyForLocalDid(this.wallet, did).get(); + Wallet wallet = walletMap.get(walletHandle); + String receivedKey = Did.keyForLocalDid(wallet, did).get(); promise.resolve(receivedKey); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -150,10 +158,12 @@ public void keyForLocalDid(String did, Promise promise) { } } + // pairwise @ReactMethod - public void createPairwise(String theirDid, String myDid, String metadata, Promise promise) { + public void createPairwise(int walletHandle, String theirDid, String myDid, String metadata, Promise promise) { try { - Pairwise.createPairwise(this.wallet, theirDid, myDid, metadata).get(); + Wallet wallet = walletMap.get(walletHandle); + Pairwise.createPairwise(wallet, theirDid, myDid, metadata).get(); promise.resolve(null); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -162,9 +172,10 @@ public void createPairwise(String theirDid, String myDid, String metadata, Promi } @ReactMethod - public void getPairwise(String theirDid, Promise promise) { + public void getPairwise(int walletHandle, String theirDid, Promise promise) { try { - String receivedKey = Pairwise.getPairwise(this.wallet, theirDid).get(); + Wallet wallet = walletMap.get(walletHandle); + String receivedKey = Pairwise.getPairwise(wallet, theirDid).get(); promise.resolve(receivedKey); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -174,10 +185,19 @@ public void getPairwise(String theirDid, Promise promise) { // crypto + private byte[] readableArrayToBuffer(ReadableArray arr) { + byte[] buffer = new byte[arr.size()]; + for (int i = 0; i < arr.size(); i++) { + buffer[i] = (byte) arr.getInt(i); + } + return buffer; + } + @ReactMethod - public void cryptoAnonCrypt(String theirKey, String message, Promise promise) { + public void cryptoAnonCrypt(String theirKey, ReadableArray message, Promise promise) { try { - byte[] encryptedData = Crypto.anonCrypt(theirKey, message.getBytes()).get(); + byte[] buffer = readableArrayToBuffer(message); + byte[] encryptedData = Crypto.anonCrypt(theirKey, buffer).get(); WritableArray result = new WritableNativeArray(); for (byte b : encryptedData) { result.pushInt(b); @@ -190,15 +210,11 @@ public void cryptoAnonCrypt(String theirKey, String message, Promise promise) { } @ReactMethod - public void cryptoAnonDecrypt(String recipientVk, ReadableArray encryptedMessage, Promise promise) { + public void cryptoAnonDecrypt(int walletHandle, String recipientVk, ReadableArray encryptedMessage, Promise promise) { try { - byte[] encryptedMessageBytes = new byte[encryptedMessage.size()]; - - for (int i = 0; i <= encryptedMessage.size(); i++) { - encryptedMessageBytes[i] = (byte) encryptedMessage.getInt(i); - } - - byte[] decryptedData = Crypto.anonDecrypt(this.wallet, recipientVk, encryptedMessageBytes).get(); + byte [] encryptedMessageBytes = readableArrayToBuffer(encryptedMessage); + Wallet wallet = walletMap.get(walletHandle); + byte[] decryptedData = Crypto.anonDecrypt(wallet, recipientVk, encryptedMessageBytes).get(); promise.resolve(decryptedData); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -207,9 +223,12 @@ public void cryptoAnonDecrypt(String recipientVk, ReadableArray encryptedMessage } @ReactMethod - public void cryptoAuthCrypt(String senderVk, String recipientVk, String message, Promise promise) { + @Deprecated + public void cryptoAuthCrypt(int walletHandle, String senderVk, String recipientVk, ReadableArray message, Promise promise) { try { - byte[] encryptedData = Crypto.authCrypt(this.wallet, senderVk, recipientVk, message.getBytes()).get(); + byte[] buffer = readableArrayToBuffer(message); + Wallet wallet = walletMap.get(walletHandle); + byte[] encryptedData = Crypto.authCrypt(wallet, senderVk, recipientVk, buffer).get(); WritableArray result = new WritableNativeArray(); for (byte b : encryptedData) { result.pushInt(b); @@ -222,15 +241,12 @@ public void cryptoAuthCrypt(String senderVk, String recipientVk, String message, } @ReactMethod - public void cryptoAuthDecrypt(String recipientVk, ReadableArray encryptedMessage, Promise promise) { + @Deprecated + public void cryptoAuthDecrypt(int walletHandle, String recipientVk, ReadableArray encryptedMessage, Promise promise) { try { - byte[] encryptedMessageBytes = new byte[encryptedMessage.size()]; - - for (int i = 0; i < encryptedMessage.size(); i++) { - encryptedMessageBytes[i] = (byte) encryptedMessage.getInt(i); - } - - CryptoResults.AuthDecryptResult decryptedResult = Crypto.authDecrypt(this.wallet, recipientVk, encryptedMessageBytes).get(); + byte[] encryptedMessageBytes = readableArrayToBuffer(encryptedMessage); + Wallet wallet = walletMap.get(walletHandle); + CryptoResults.AuthDecryptResult decryptedResult = Crypto.authDecrypt(wallet, recipientVk, encryptedMessageBytes).get(); String theirKey = decryptedResult.getVerkey(); WritableArray decryptedData = new WritableNativeArray(); @@ -248,6 +264,81 @@ public void cryptoAuthDecrypt(String recipientVk, ReadableArray encryptedMessage } } + @ReactMethod + public void cryptoSign(int walletHandle, String signerVk, ReadableArray messageRaw, Promise promise) { + try { + Wallet wallet = walletMap.get(walletHandle); + byte[] buffer = readableArrayToBuffer(messageRaw); + byte[] signature = Crypto.cryptoSign(wallet, signerVk, buffer).get(); + WritableArray result = new WritableNativeArray(); + for (byte b : signature) { + result.pushInt(b); + } + promise.resolve(result); + } catch (Exception e) { + IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); + promise.reject(rejectResponse.getCode(), rejectResponse.toJson(), e); + } + } + + @ReactMethod + public void cryptoVerify(String signerVk, ReadableArray messageRaw, ReadableArray signatureRaw, Promise promise) { + try { + byte[] messageBuf = readableArrayToBuffer(messageRaw); + byte[] sigBuf = readableArrayToBuffer(signatureRaw); + boolean valid = Crypto.cryptoVerify(signerVk, messageBuf, sigBuf).get(); + + promise.resolve(valid); + } catch (Exception e) { + IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); + promise.reject(rejectResponse.getCode(), rejectResponse.toJson(), e); + } + } + + + @ReactMethod + public void packMessage(int walletHandle, ReadableArray message, ReadableArray receiverKeys, String senderVk, Promise promise) { + try { + Wallet wallet = walletMap.get(walletHandle); + byte[] buffer = readableArrayToBuffer(message); + + String[] keys = new String[receiverKeys.size()]; + for (int i = 0; i < receiverKeys.size(); i++) { + keys[i] = receiverKeys.getString(i); + } + Gson gson = new Gson(); + String receiverKeysJson = gson.toJson(keys); + + byte[] jwe = Crypto.packMessage(wallet, receiverKeysJson, senderVk, buffer).get(); + WritableArray result = new WritableNativeArray(); + for (byte b : jwe) { + result.pushInt(b); + } + promise.resolve(result); + } catch (Exception e) { + IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); + promise.reject(rejectResponse.getCode(), rejectResponse.toJson(), e); + } + } + + @ReactMethod + public void unpackMessage(int walletHandle, ReadableArray jwe, Promise promise) { + try { + Wallet wallet = walletMap.get(walletHandle); + byte[] buffer = readableArrayToBuffer(jwe); + byte[] res = Crypto.unpackMessage(wallet, buffer).get(); + + WritableArray result = new WritableNativeArray(); + for (byte b : res) { + result.pushInt(b); + } + promise.resolve(result); + } catch (Exception e) { + IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); + promise.reject(rejectResponse.getCode(), rejectResponse.toJson(), e); + } + } + // pool @ReactMethod @@ -262,10 +353,9 @@ public void setProtocolVersion(int protocolVersion, Promise promise) { } @ReactMethod - public void createPoolLedgerConfig(String poolName, String poolConfig, Promise promise) { + public void createPoolLedgerConfig(String configName, String poolConfig, Promise promise) { try { - System.out.println(poolConfig); - Pool.createPoolLedgerConfig(poolName, poolConfig).get(); + Pool.createPoolLedgerConfig(configName, poolConfig).get(); promise.resolve(null); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -274,10 +364,11 @@ public void createPoolLedgerConfig(String poolName, String poolConfig, Promise p } @ReactMethod - public void openPoolLedger(String poolName, String poolConfig, Promise promise) { + public void openPoolLedger(String configName, String poolConfig, Promise promise) { try { - this.pool = Pool.openPoolLedger(poolName, poolConfig).get(); - promise.resolve(this.pool.getPoolHandle()); + Pool pool = Pool.openPoolLedger(configName, poolConfig).get(); + poolMap.put(pool.getPoolHandle(), pool); + promise.resolve(pool.getPoolHandle()); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); promise.reject(rejectResponse.getCode(), rejectResponse.toJson(), e); @@ -285,9 +376,11 @@ public void openPoolLedger(String poolName, String poolConfig, Promise promise) } @ReactMethod - public void closePoolLedger(Promise promise) { + public void closePoolLedger(int handle, Promise promise) { try { - this.pool.closePoolLedger().get(); + Pool pool = poolMap.get(handle); + pool.closePoolLedger().get(); + poolMap.remove(handle); promise.resolve(null); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -298,9 +391,10 @@ public void closePoolLedger(Promise promise) { // ledger @ReactMethod - public void submitRequest(String requestJson, Promise promise) { + public void submitRequest(int poolHandle, String requestJson, Promise promise) { try { - String response = Ledger.submitRequest(this.pool, requestJson).get(); + Pool pool = poolMap.get(poolHandle); + String response = Ledger.submitRequest(pool, requestJson).get(); promise.resolve(response); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -361,9 +455,10 @@ public void parseGetCredDefResponse(String getCredDefResponse, Promise promise) // anoncreds @ReactMethod - public void proverCreateMasterSecret(String masterSecretId, Promise promise) { + public void proverCreateMasterSecret(int walletHandle, String masterSecretId, Promise promise) { try { - String outputMasterSecretId = Anoncreds.proverCreateMasterSecret(this.wallet, masterSecretId).get(); + Wallet wallet = walletMap.get(walletHandle); + String outputMasterSecretId = Anoncreds.proverCreateMasterSecret(wallet, masterSecretId).get(); promise.resolve(outputMasterSecretId); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -372,9 +467,10 @@ public void proverCreateMasterSecret(String masterSecretId, Promise promise) { } @ReactMethod - public void proverCreateCredentialReq(String proverDid, String credentialOfferJson, String credentialDefJson, String masterSecretId, Promise promise) { + public void proverCreateCredentialReq(int walletHandle, String proverDid, String credentialOfferJson, String credentialDefJson, String masterSecretId, Promise promise) { try { - AnoncredsResults.ProverCreateCredentialRequestResult credentialRequestResult = Anoncreds.proverCreateCredentialReq(this.wallet, proverDid, credentialOfferJson, credentialDefJson, masterSecretId).get(); + Wallet wallet = walletMap.get(walletHandle); + AnoncredsResults.ProverCreateCredentialRequestResult credentialRequestResult = Anoncreds.proverCreateCredentialReq(wallet, proverDid, credentialOfferJson, credentialDefJson, masterSecretId).get(); WritableArray response = new WritableNativeArray(); response.pushString(credentialRequestResult.getCredentialRequestJson()); response.pushString(credentialRequestResult.getCredentialRequestMetadataJson()); @@ -386,9 +482,10 @@ public void proverCreateCredentialReq(String proverDid, String credentialOfferJs } @ReactMethod - public void proverStoreCredential(String credId, String credReqMetadataJson, String credJson, String credDefJson, String revRegDefJson, Promise promise) { + public void proverStoreCredential(int walletHandle, String credId, String credReqMetadataJson, String credJson, String credDefJson, String revRegDefJson, Promise promise) { try { - String newCredId = Anoncreds.proverStoreCredential(this.wallet, credId, credReqMetadataJson, credJson, credDefJson, revRegDefJson).get(); + Wallet wallet = walletMap.get(walletHandle); + String newCredId = Anoncreds.proverStoreCredential(wallet, credId, credReqMetadataJson, credJson, credDefJson, revRegDefJson).get(); promise.resolve(newCredId); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -397,9 +494,10 @@ public void proverStoreCredential(String credId, String credReqMetadataJson, Str } @ReactMethod - public void proverGetCredential(String credId, Promise promise) { + public void proverGetCredential(int walletHandle, String credId, Promise promise) { try { - String credential = Anoncreds.proverGetCredential(this.wallet, credId).get(); + Wallet wallet = walletMap.get(walletHandle); + String credential = Anoncreds.proverGetCredential(wallet, credId).get(); promise.resolve(credential); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); @@ -408,9 +506,10 @@ public void proverGetCredential(String credId, Promise promise) { } @ReactMethod - public void proverGetCredentials(String filter, Promise promise) { + public void proverGetCredentials(int walletHandle, String filter, Promise promise) { try { - String credentials = Anoncreds.proverGetCredentials(this.wallet, filter).get(); + Wallet wallet = walletMap.get(walletHandle); + String credentials = Anoncreds.proverGetCredentials(wallet, filter).get(); promise.resolve(credentials); } catch (Exception e) { IndyBridgeRejectResponse rejectResponse = new IndyBridgeRejectResponse(e); diff --git a/src/index.js b/src/index.js index 5ccd741..825d311 100644 --- a/src/index.js +++ b/src/index.js @@ -223,13 +223,9 @@ export type Verkey = string export type WalletHandle = number export type PoolHandle = number -const { IndySdk } = NativeModules +const { IndyBridge } = NativeModules export default { - sampleMethod(stringArgument, numberArgument) { - return IndySdk.sampleMethod(stringArgument, numberArgument) - }, - // wallet createWallet(config: Object, credentials: Object): Promise { @@ -244,7 +240,7 @@ export default { if (Platform.OS === 'ios') { return IndyBridge.closeWallet(wh.callSomething()) } - return IndyBridge.closeWallet() + return IndyBridge.closeWallet(wh) }, deleteWallet(config: Object, credentials: Object): Promise { @@ -258,21 +254,24 @@ export default { * because it keeps wallet as a private attribute. */ createAndStoreMyDid(wh: WalletHandle, did: Object): Promise<[Did, Verkey]> { - return IndyBridge.createAndStoreMyDid(JSON.stringify(did), wh) + if (Platform.OS === 'ios') { + return IndyBridge.createAndStoreMyDid(JSON.stringify(did), wh) + } + return IndyBridge.createAndStoreMyDid(wh, JSON.stringify(did)) }, keyForDid(poolHandle: PoolHandle, wh: WalletHandle, did: Did): Promise { if (Platform.OS === 'ios') { return IndyBridge.keyForDid(did, poolHandle, wh) } - return IndyBridge.keyForDid(did) + return IndyBridge.keyForDid(poolHandle, wh, did) }, keyForLocalDid(wh: WalletHandle, did: Did): Promise { if (Platform.OS === 'ios') { return IndyBridge.keyForLocalDid(did, wh) } - return IndyBridge.keyForLocalDid(did) + return IndyBridge.keyForLocalDid(wh, did) }, storeTheirDid(wh: WalletHandle, identity: {}) { @@ -288,49 +287,78 @@ export default { if (Platform.OS === 'ios') { return IndyBridge.createPairwise(theirDid, myDid, metadata, wh) } - return IndyBridge.createPairwise(theirDid, myDid, metadata) + return IndyBridge.createPairwise(wh, theirDid, myDid, metadata) }, async getPairwise(wh: WalletHandle, theirDid: Did): Promise { if (Platform.OS === 'ios') { return JSON.parse(await IndyBridge.getPairwise(theirDid, wh)) } - return JSON.parse(await IndyBridge.getPairwise(theirDid)) + return JSON.parse(await IndyBridge.getPairwise(wh, theirDid)) }, // crypto - cryptoAnonCrypt(recipientVk: Verkey, messageRaw: Uint8Array): Promise> { + async cryptoAnonCrypt(recipientVk: Verkey, messageRaw: Buffer): Promise { if (Platform.OS === 'ios') { return IndyBridge.cryptoAnonCrypt(messageRaw, recipientVk) } - return IndyBridge.cryptoAnonCrypt(recipientVk, messageRaw) + return Buffer.from(await IndyBridge.cryptoAnonCrypt(recipientVk, Array.from(messageRaw))) }, - cryptoAnonDecrypt(wh: WalletHandle, recipientVk: Verkey, encryptedMsg: Array): Promise> { + async cryptoAnonDecrypt(wh: WalletHandle, recipientVk: Verkey, encryptedMsg: Buffer): Promise { if (Platform.OS === 'ios') { return IndyBridge.cryptoAnonDecrypt(encryptedMsg, recipientVk, wh) } - return IndyBridge.cryptoAnonDecrypt(recipientVk, encryptedMsg) + return Buffer.from(await IndyBridge.cryptoAnonDecrypt(wh, recipientVk, Array.from(encryptedMsg))) }, - cryptoAuthCrypt(wh: WalletHandle, senderVk: Verkey, recipientVk: Verkey, messageRaw: string): Promise> { + async cryptoAuthCrypt(wh: WalletHandle, senderVk: Verkey, recipientVk: Verkey, messageRaw: Buffer): Promise { if (Platform.OS === 'ios') { return IndyBridge.cryptoAuthCrypt(messageRaw, senderVk, recipientVk, wh) } - return IndyBridge.cryptoAuthCrypt(senderVk, recipientVk, messageRaw) + return Buffer.from(await IndyBridge.cryptoAuthCrypt(wh, senderVk, recipientVk, Array.from(messageRaw))) }, - cryptoAuthDecrypt( + async cryptoAuthDecrypt( wh: WalletHandle, recipientVk: Verkey, - encryptedMsgRaw: Array - ): Promise<[Verkey, Array]> { + encryptedMsgRaw: Buffer + ): Promise<[Verkey, Buffer]> { if (Platform.OS === 'ios') { return IndyBridge.cryptoAuthDecrypt(encryptedMsgRaw, recipientVk, wh) } - return IndyBridge.cryptoAuthDecrypt(recipientVk, encryptedMsgRaw) + const [verkey, msg] = await IndyBridge.cryptoAuthDecrypt(recipientVk, Array.from(encryptedMsgRaw)) + return [verkey, Buffer.from(msg)] + }, + + async cryptoSign(wh: WalletHandle, signerVk: string, message: Buffer): Promise { + if (Platform.OS == 'ios') { + throw new Error(`Unsupported operation! Platform: ${Platform.OS}`) + } + return Buffer.from(await IndyBridge.cryptoSign(wh, signerVk, Array.from(message))) + }, + + cryptoVerify(signerVk: string, message: Buffer, signature: Buffer) { + if (Platform.OS == 'ios') { + throw new Error(`Unsupported operation! Platform: ${Platform.OS}`) + } + return IndyBridge.cryptoVerify(signerVk, Array.from(message), Array.from(signature)) + }, + + async packMessage(wh: WalletHandle, message: Buffer, receiverKeys: Verkey[], senderVk: string): Promise { + if (Platform.OS == 'ios') { + throw new Error(`Unsupported operation! Platform: ${Platform.OS}`) + } + return Buffer.from(await IndyBridge.packMessage(wh, Array.from(message), receiverKeys, senderVk)) + }, + + async unpackMessage(wh: WalletHandle, jwe: Buffer): Promise { + if (Platform.OS == 'ios') { + throw new Error(`Unsupported operation! Platform: ${Platform.OS}`) + } + return Buffer.from(await IndyBridge.unpackMessage(wh, Array.from(jwe))) }, // pool @@ -351,43 +379,54 @@ export default { }, closePoolLedger(ph: PoolHandle): Promise { - if (Platform.OS === 'ios') { - return IndyBridge.closePoolLedger(ph) - } - return IndyBridge.closePoolLedger() + return IndyBridge.closePoolLedger(ph) }, async submitRequest(poolHandle: PoolHandle, request: LedgerRequest): Promise { if (Platform.OS === 'ios') { return JSON.parse(await IndyBridge.submitRequest(request, poolHandle)) } - return JSON.parse(await IndyBridge.submitRequest(request)) + return JSON.parse(await IndyBridge.submitRequest(ph, JSON.stringify(request))) }, - buildGetSchemaRequest(submitterDid: Did, id: string): Promise { - return IndyBridge.buildGetSchemaRequest(submitterDid, id) + async buildGetSchemaRequest(submitterDid: Did, id: string): Promise { + if (Platform.OS === 'ios') { + return IndyBridge.buildGetSchemaRequest(submitterDid, id) + } + return JSON.parse(await IndyBridge.buildGetSchemaRequest(submitterDid, id)) }, - parseGetSchemaResponse(getSchemaResponse: LedgerRequestResult): Promise<[SchemaId, Schema]> { - return IndyBridge.parseGetSchemaResponse(JSON.stringify(getSchemaResponse)) + async parseGetSchemaResponse(getSchemaResponse: LedgerRequestResult): Promise<[SchemaId, Schema]> { + if (Platform.OS === 'ios') { + return IndyBridge.parseGetSchemaResponse(JSON.stringify(getSchemaResponse)) + } + const [id, schema ] = await IndyBridge.parseGetSchemaResponse(JSON.stringify(getSchemaResponse)) + return [id, JSON.parse(schema)] }, - buildGetCredDefRequest(submitterDid: Did, id: string): Promise { - return IndyBridge.buildGetCredDefRequest(submitterDid, id) + async buildGetCredDefRequest(submitterDid: Did, id: string): Promise { + if (Platform.OS === 'ios') { + return IndyBridge.buildGetCredDefRequest(submitterDid, id) + } + return JSON.parse(await IndyBridge.buildGetCredDefRequest(submitterDid, id)) }, - parseGetCredDefResponse(getCredDefResponse: LedgerRequestResult): Promise<[CredDefId, CredDef]> { - return IndyBridge.parseGetCredDefResponse(JSON.stringify(getCredDefResponse)) + async parseGetCredDefResponse(getCredDefResponse: LedgerRequestResult): Promise<[CredDefId, CredDef]> { + if (Platform.OS === 'ios') { + return IndyBridge.parseGetCredDefResponse(JSON.stringify(getCredDefResponse)) + } + const [credDefId, credDef ] = await IndyBridge.parseGetCredDefResponse(JSON.stringify(getCredDefResponse)) + return [credDefId, JSON.parse(credDef)] }, proverCreateMasterSecret(wh: WalletHandle, masterSecretId: ?MasterSecretId): Promise { if (Platform.OS === 'ios') { return IndyBridge.proverCreateMasterSecret(masterSecretId, wh) } - return IndyBridge.proverCreateMasterSecret(masterSecretId) + return IndyBridge.proverCreateMasterSecret(wh, masterSecretId) }, - proverCreateCredentialReq( + async proverCreateCredentialReq( wh: WalletHandle, proverDid: Did, credOffer: CredOffer, @@ -403,12 +442,14 @@ export default { wh ) } - return IndyBridge.proverCreateCredentialReq( + const [credReq, credReqMetadata ] = await IndyBridge.proverCreateCredentialReq( + wh, proverDid, JSON.stringify(credOffer), JSON.stringify(credDef), masterSecretId ) + return [JSON.parse(credReq), JSON.parse(credReqMetadata)] }, proverStoreCredential( @@ -430,6 +471,7 @@ export default { ) } return IndyBridge.proverStoreCredential( + wh, credId, JSON.stringify(credReqMetadata), JSON.stringify(cred), @@ -442,7 +484,7 @@ export default { if (Platform.OS === 'ios') { return JSON.parse(await IndyBridge.proverGetCredentials(JSON.stringify(filter), wh)) } - return JSON.parse(await IndyBridge.proverGetCredentials(JSON.stringify(filter))) + return JSON.parse(await IndyBridge.proverGetCredentials(wh, JSON.stringify(filter))) }, async proverGetCredential(wh: WalletHandle, credId: CredId): Promise { @@ -455,7 +497,10 @@ export default { // TODO Add return flow type. // It needs little investigation, because is doesn't seem to be same format as Credential stored in wallet. async proverGetCredentialsForProofReq(wh: WalletHandle, proofRequest: ProofRequest) { - return JSON.parse(await IndyBridge.proverGetCredentialsForProofReq(JSON.stringify(proofRequest), wh)) + if (Platform.OS == 'ios') { + return JSON.parse(await IndyBridge.proverGetCredentialsForProofReq(JSON.stringify(proofRequest), wh)) + } + throw new Error(`Not implemented for platfrom: ${Platform.OS}`) }, async proverCreateProof( @@ -467,16 +512,19 @@ export default { credentialDefs: CredentialDefs, revStates: RevStates = {} ): Promise { - return JSON.parse( - await IndyBridge.proverCreateProofForRequest( - JSON.stringify(proofReq), - JSON.stringify(requestedCredentials), - masterSecretName, - JSON.stringify(schemas), - JSON.stringify(credentialDefs), - JSON.stringify(revStates), - wh + if (Platform.OS == 'ios') { + return JSON.parse( + await IndyBridge.proverCreateProofForRequest( + JSON.stringify(proofReq), + JSON.stringify(requestedCredentials), + masterSecretName, + JSON.stringify(schemas), + JSON.stringify(credentialDefs), + JSON.stringify(revStates), + wh + ) ) - ) + } + throw new Error(`Not implemented for platfrom: ${Platform.OS}`) }, }