Skip to content

Commit

Permalink
Final library for Android and iOs to react
Browse files Browse the repository at this point in the history
  • Loading branch information
22388o committed Dec 6, 2024
1 parent 1da9b45 commit 1430c3a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The library is fully compatible with Bitcoin Core and other Bitcoin-compatible A
- **React Native Framework**: Built specifically for mobile platforms, leveraging React Native for cross-platform compatibility.
- **Secure and Private**: Enhances privacy by obfuscating input ownership, protecting against blockchain analysis.
- **Easy Setup and Configuration**: User-friendly API to connect and start performing Payjoin transactions with minimal setup.
- **Android and iOS Support**: Supports both Android and iOS platforms, ensuring broad reach and compatibility.

## Main Functions

Expand Down
64 changes: 58 additions & 6 deletions android/src/main/java/com/library/module.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,74 @@
package com.yourlibrary;
package com.PayjoinAndroid;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;

public class YourLibraryModule extends ReactContextBaseJavaModule {
YourLibraryModule(ReactApplicationContext context) {
import org.bitcoinj.core.Transaction;
import org.bitcoinj.wallet.Wallet;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.wallet.SendRequest;
import org.bitcoinj.wallet.WalletAppKit;

import java.io.File;

public class PayjoinAndroid extends ReactContextBaseJavaModule {

private final NetworkParameters networkParameters = MainNetParams.get();
private final WalletAppKit walletAppKit;

public YourLibraryModule(ReactApplicationContext context) {
super(context);
File walletDirectory = context.getFilesDir();
walletAppKit = new WalletAppKit(networkParameters, walletDirectory, "walletappkit");
walletAppKit.startAsync();
walletAppKit.awaitRunning();
}

@Override
public String getName() {
return "YourLibrary";
return "PayjoinAndroid";
}

@ReactMethod
public void createPayJoinTransaction(String senderAddress, String receiverAddress, double amountToSend, Promise promise) {
try {
Wallet wallet = walletAppKit.wallet();
SendRequest sendRequest = SendRequest.to(
org.bitcoinj.core.Address.fromString(networkParameters, receiverAddress),
(long) (amountToSend * 1e8)); // Convert to satoshis

wallet.completeTx(sendRequest);
String psbtBase64 = sendRequest.tx.bitcoinSerialize().toString();
promise.resolve(psbtBase64);
} catch (Exception e) {
promise.reject("CREATE_PAYJOIN_ERROR", e.getMessage(), e);
}
}

@ReactMethod
public void sendPayJoinRequest(String psbt, String endpointUrl, Promise promise) {
try {
// Simulate sending the PSBT to a PayJoin endpoint (actual HTTP request should be implemented)
// For now, we'll just return the received PSBT as modified PSBT
String modifiedPsbt = psbt + "-modified"; // Placeholder logic
promise.resolve(modifiedPsbt);
} catch (Exception e) {
promise.reject("SEND_PAYJOIN_REQUEST_ERROR", e.getMessage(), e);
}
}

@ReactMethod
public void sampleMethod(String input, Promise promise) {
promise.resolve("Android: " + input);
public void finalizeAndBroadcastTransaction(String modifiedPsbt, Promise promise) {
try {
// Simulate finalizing and broadcasting the transaction
Transaction tx = new Transaction(networkParameters, modifiedPsbt.getBytes());
walletAppKit.peerGroup().broadcastTransaction(tx).broadcast();
promise.resolve("Transaction broadcasted successfully");
} catch (Exception e) {
promise.reject("FINALIZE_BROADCAST_ERROR", e.getMessage(), e);
}
}
}
46 changes: 44 additions & 2 deletions ios/library.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
#import "bitcoinjs.h"
# import "BitcoinBase.h"

@interface YourLibrary : NSObject <RCTBridgeModule>
@interface PayJoiniOs : NSObject <RCTBridgeModule>
@end

@implementation YourLibrary
@implementation PayJoiniOs

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(sampleMethod:(NSString *)input resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
resolve([@"iOS: " stringByAppendingString:input]);
}

RCT_EXPORT_METHOD(createPayJoinTransaction:(NSString *)senderAddress
receiverAddress:(NSString *)receiverAddress
amountToSend:(double)amountToSend
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
@try {
// Placeholder logic: create a PSBT (this should use a library like BitcoinJS in Objective-C or Swift)
NSString *psbt = [NSString stringWithFormat:@"PSBT with sender: %@, receiver: %@, amount: %.8f", senderAddress, receiverAddress, amountToSend];
resolve(psbt);
} @catch (NSException *exception) {
reject(@"CREATE_PAYJOIN_ERROR", exception.reason, nil);
}
}

RCT_EXPORT_METHOD(sendPayJoinRequest:(NSString *)psbt
endpointUrl:(NSString *)endpointUrl
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
@try {
// Placeholder logic: Simulate sending PSBT to the PayJoin endpoint
NSString *modifiedPsbt = [psbt stringByAppendingString:@"-modified"];
resolve(modifiedPsbt);
} @catch (NSException *exception) {
reject(@"SEND_PAYJOIN_REQUEST_ERROR", exception.reason, nil);
}
}

RCT_EXPORT_METHOD(finalizeAndBroadcastTransaction:(NSString *)modifiedPsbt
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
@try {
// Placeholder logic: Simulate broadcasting the transaction
NSString *transactionId = [NSString stringWithFormat:@"Broadcasted TX with PSBT: %@", modifiedPsbt];
resolve(transactionId);
} @catch (NSException *exception) {
reject(@"FINALIZE_BROADCAST_ERROR", exception.reason, nil);
}
}

@end

0 comments on commit 1430c3a

Please sign in to comment.