-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Final library for Android and iOs to react
- Loading branch information
Showing
3 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |