使用React Native創(chuàng)建以太坊錢包實(shí)現(xiàn)轉(zhuǎn)賬等功能
之前想用React Native開發(fā)一版以太坊錢包app,但在生成賬戶那塊遇見了問題,沒有crypto等nodejs包,react native是運(yùn)行在JavaScriptCore環(huán)境里面,是沒有buffer, crypto 跟 stream這些庫的,所以為了解決,就跟同事開發(fā)了基于golang的web3go,然后使用gomoble工具編譯成ios需要的framework以及android需要的jar aar,完美解決問題
演示

dapp-demo-1.jpg

dapp-demo-2.jpg
安裝web3go
git clone https://github.com/bcl-chain/web3.go.git
使用gomobile編譯成framework,jar,aar
// generate frameworkgomobile bind -target=ios ./github.com/bcl-chain/web3 .go/mobile// generate arr jargomobile bind -target=android ./github.com/bcl-chain/web3.go/mobile
把生成的包link到原生app里面

link-web3go.jpg

andoir-getbalence.jpg
下載ETH本地測(cè)試工具ganache-cli

gan-cli.jpg
安裝依賴
yarn react-native run-android react-native run-ios
getBalance代碼分析
// IOS
RCT_EXPORT_METHOD(getBalance:
(NSString*) address:
(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject){
// ip地址
Web3goEthereumClient* client = Web3goNewEthereumClient(nodeIP, nil);
Web3goContext* ctx = Web3goNewContext();
// 賬戶地址
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: %@", @"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);
}
}
如果有用,給個(gè)start
總結(jié)
以上所述是小編給大家介紹的使用React Native創(chuàng)建以太坊錢包實(shí)現(xiàn)轉(zhuǎn)賬等功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
解決React報(bào)錯(cuò)Property does not exist on 
這篇文章主要為大家介紹了React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
react路由v6版本NavLink的兩個(gè)小坑及解決
這篇文章主要介紹了react路由v6版本NavLink的兩個(gè)小坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
react-native聊天室|RN版聊天App仿微信實(shí)例|RN仿微信界面
這篇文章主要介紹了react-native聊天室|RN版聊天App仿微信實(shí)例|RN仿微信界面,需要的朋友可以參考下2019-11-11

