I wanted to use React Native to develop a version of Ethereum wallet app, but there was a problem in generating accounts. There was no NodeJS package such as Crypto. React Native runs in JavaScriptCore environment. There is no buffer, crypto and Stream libraries, so in order to solve the problem, I developed Web3Go based on Golang with my colleagues, and then compiled it into the framework required by ios and the JAR AAR required by Android using gomoble tool, which perfectly solved the problem
- demo
- Install web3go
git clone https://github.com/bcl-chain/web3.go.git
Copy the code
- Compile to framework, JAR, AAR using Gomobile
// generate framework
gomobile bind -target=ios ./github.com/bcl-chain/web3.go/mobile
// generate arr jar
gomobile bind -target=android ./github.com/bcl-chain/web3.go/mobile
Copy the code
- Link the generated package to the native app
- Download the ETH local test tool Ganache using cli
- Install dependencies
yarn
react-native run-android
react-native run-ios
Copy the code
- GetBalance code analysis
// IOS
RCT_EXPORT_METHOD(getBalance:
(NSString*) address:
(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject){
/ / IP address
Web3goEthereumClient* client = Web3goNewEthereumClient(nodeIP, nil);
Web3goContext* ctx = Web3goNewContext();
// Account address
Web3goAddress* address1 = Web3goNewAddressFromHex(address, nil);
@try {
Web3goBigInt* a = [client getBalanceAt:ctx account:address1 number:- 1 error:nil];
NSString* ammount = [a getString:10];
NSLog(@"% @", ammount);
resolve(ammount);
} @catch (NSError *exception) {
NSLog(@"NSError: %@", exception);
reject(@"NSError: %@"The @"There were no events", exception);
} @finally {
NSLog(@"finally"); }}// android
@ReactMethod
public void getBalance(String address, Promise promise) {
try {
web3go.EthereumClient client = Web3go.newEthereumClient(nodeIP);
web3go.Context ctx = Web3go.newContext();
web3go.Address address1 = Web3go.newAddressFromHex(address);
web3go.BigInt a = client.getBalanceAt(ctx, address1, - 1);
String ammout = a.getString(10);
promise.resolve(ammout);
} catch(Exception e) { promise.reject(e.getMessage()); }}// react-native
async getBalance() {
try {
var ammount = await NativeModules.Web3go.getBalance(this.state.defGaAddress);
this.setState({
gaAmmount: ammount
})
} catch (e) {
console.error(e); }}Copy the code
If it works, give a start
web3go
React-Native-Dapp