区块链开发之VSYS钱包APP
目录
基础知识
1、什么是v.systems?
v.systems(也称为VSYS)是一个区块链数据库云项目,旨在为区块链数据库创建安全的基础架构平台。该项目将提供去中心化的云数据库技术,以执行复杂的去中心化应用程序,并以高可扩展性,持久性和高性能来有效地支持数万亿个区块链的运行。
2、什么是nonce?
在Vsys链中,通过助记词能导入很多个账号,这些账号之间使用Nonce来进行区别。助记词和Nonce一致则导入的导入的账号必然一致,所以在进行助记词方式导入时,请一定确认需导入账号的Nonce值,确保导入账号为您所希望的账号。
3、什么是VSYS钱包?
-
官方钱包:同时提供热钱包和冷钱包,以提供最佳安全解决方案来存储您的VSYS硬币。
image
下载链接:https://v.systems/wallet -
民间钱包:也就是第三方开发的钱包支持VSYS链,当然今天讲解的钱包也是第三方钱包,大多数第三方钱包都是热钱包。都会涉及到联网,交易等。
开发准备
1、准备SDK:
-
在官方的github下载源码:https://github.com/virtualeconomy/java-v-sdk,并且删除与项目中冲突的文件。如果不使用lombok的话,请删除相关代码,自己生成相关的方法。这个整理需要些时间,如果需要我整理好的代码,请在评论区留言。
image
2、环境准备: -
测试环境:
官网:https://test.v.systems
节点:http://test.v.systems:9922
浏览器:https://testexplorer.v.systems -
正式环境:
官网:https://v.systems
节点:https://wallet.v.systems/api
浏览器:https://explorer.v.systems
3、简单使用:
String nodeUrl = "https://wallet.v.systems/api";
Blockchain blockchain = new Blockchain(NetworkType.Mainnet,nodeUrl);
Integer height = blockchain.getHeight();
System.out.println("height = " + height);
结果:
height = 10742516
实战开发
1、随机生成账号
int nonce = 0; //nonce,在上面提及到。
Account account = new Account(NetworkType.Mainnet, mnemonic, nonce);
System.out.println("mnemonic: " + mnemonic);
System.out.println("privateKey: " + account.getPrivateKey());
System.out.println("publicKey: " + account.getPublicKey());
System.out.println("address: " + account.getAddress());
结果:
mnemonic: foil select play devote tail initial multiply away ancient trend tuition valve net foot olive
privateKey: 97JX39eeV3Q7SZpo941UeTUdokoaWdBQQ6Y647BDFXuN
publicKey: 3k2hdcoiryNKrESg7s2RWBjToXUANrj8Ld4zia3Uvvtn
address: ARAEswrPshwQDKk8n3WEVMMqBr4cTGthyvp
2、助记词生成账号
String mnemonic = "foil select play devote tail initial multiply away ancient trend tuition valve net foot olive";
int nonce = 0; //nonce,在上面提及到。
Account account = new Account(NetworkType.Mainnet, mnemonic, nonce);
System.out.println("mnemonic: " + mnemonic);
System.out.println("privateKey: " + account.getPrivateKey());
System.out.println("publicKey: " + account.getPublicKey());
System.out.println("address: " + account.getAddress());
结果:
mnemonic: foil select play devote tail initial multiply away ancient trend tuition valve net foot olive
privateKey: 97JX39eeV3Q7SZpo941UeTUdokoaWdBQQ6Y647BDFXuN
publicKey: 3k2hdcoiryNKrESg7s2RWBjToXUANrj8Ld4zia3Uvvtn
address: ARAEswrPshwQDKk8n3WEVMMqBr4cTGthyvp
注意:同样的助记词和同样的nonce才能得到唯一的地址。
3、私钥生成账号
String privateKey = "97JX39eeV3Q7SZpo941UeTUdokoaWdBQQ6Y647BDFXuN";
Account account = new Account(NetworkType.Mainnet,privateKey);
System.out.println("privateKey: " + account.getPrivateKey());
System.out.println("publicKey: " + account.getPublicKey());
System.out.println("address: " + account.getAddress());
结果:
privateKey: 97JX39eeV3Q7SZpo941UeTUdokoaWdBQQ6Y647BDFXuN
publicKey: 3k2hdcoiryNKrESg7s2RWBjToXUANrj8Ld4zia3Uvvtn
address: ARAEswrPshwQDKk8n3WEVMMqBr4cTGthyvp
4、获取余额
String nodeUrl = "https://wallet.v.systems/api";
Blockchain blockchain = new Blockchain(NetworkType.Mainnet, nodeUrl);
String address = "ARHXxk7ZYLpALQhFGKX4GjvRSYQi3ZztRnV";
Long balance = blockchain.getBalance(address);
System.out.println("balance = " + balance);
结果:
balance = 27026944953361
5、获取代币余额
String nodeUrl = "https://wallet.v.systems/api";
Blockchain blockchain = new Blockchain(NetworkType.Mainnet, nodeUrl);
String address = "ARHXxk7ZYLpALQhFGKX4GjvRSYQi3ZztRnV";
String tokenId = "TWaN4DqnRMdUS5d1ohCn9Vh9VdGWDLm9Um1jHbQiF";
TokenBalance balance = blockchain.getTokenBalance(address,tokenId);
System.out.println("balance = " + balance.getBalance());
结果:
balance = 0
6、转账
String privateKey = "xxx 你的私钥";
String toAddress = "你要转给谁的地址";
String nodeUrl = "https://wallet.v.systems/api";
Blockchain blockchain = new Blockchain(NetworkType.Mainnet, nodeUrl);
Long amount = 1 * Blockchain.V_UNITY; // Send 1.0 V coin
PaymentTransaction tx = TransactionFactory.buildPaymentTx(toAddress, amount);
Account account = new Account(NetworkType.Mainnet, privateKey);
// Usage 1: for hot wallet sending transaction
Transaction result = account.sendTransaction(blockchain, tx);
String id = result.getId();//即交易的ID
//拿到ID就可以查询交易状态,是否成功等待。
// Usage 2: for cold wallet signing transaction
String signature = account.getSignature(tx);
7、代币转账
String privateKey = "xxx 你的私钥";
String toAddress = "你要转给谁的地址";
String tokenId = "代币ID";
String nodeUrl = "https://wallet.v.systems/api";
Blockchain blockchain = new Blockchain(NetworkType.Mainnet, nodeUrl);
ContractType tokenType = blockchain.getContractTypeByTokenId(tokenId);
TokenInfo tokenInfo = blockchain.getTokenInfo(tokenId);
Long tokenUnity = tokenInfo.getUnity();
Long amount = 1 * tokenUnity; // Send 1.0 Token
ExecuteContractFunctionTransaction tx = TransactionFactory.buildSendTokenTx(tokenId, tokenType, toAddress, amount);
String txId = tx.getId(); // get Tx ID offline
// Usage 1: for hot wallet sending transaction
Account account = new Account(NetworkType.Mainnet, privateKey);
Transaction result = account.sendTransaction(blockchain, tx);
String id = result.getId();//即交易的ID
//拿到ID就可以查询交易状态,是否成功等待。
// Usage 2: for cold wallet signing transaction
String signature = account.getSignature(tx);
8、查询交易详情
String txId = "Dsc6iCLr4WhXYtJqdwQkjrp3LA4gZ23vyme2YUw7nMFC";
String nodeUrl = "https://wallet.v.systems/api";
Blockchain blockchain = new Blockchain(NetworkType.Mainnet, nodeUrl);
Transaction transaction = blockchain.getTransactionById(txId);
String json = JSON.toJSONString(transaction); //为了方便查看我转成json
System.out.println(json);
结果:
{"amount":3600000000,"height":10742584,"id":"Dsc6iCLr4WhXYtJqdwQkjrp3LA4gZ23vyme2YUw7nMFC","recipient":"ARHXxk7ZYLpALQhFGKX4GjvRSYQi3ZztRnV","status":"Success","timestamp":1586581452003689785,"type":5}
可以看到status为Success就表示成功了
总结
1、开发VSYS钱包相比前几篇的BTC,ETH,EOS简单多了,主要是提供了相关的SDK供我们使用,最大的问题可能就是整理SDK。
2、若你在开发过程中遇到什么问题都可以给我评论留言。
参考文献
github:https://github.com/virtualeconomy/java-v-sdk
官网:https://v.systems/