前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stellar 原

stellar 原

作者头像
wuweixiang
发布2018-08-14 11:32:31
5400
发布2018-08-14 11:32:31
举报
文章被收录于专栏:吴伟祥吴伟祥

Stellar Network Overview

Stellar Ecosystem
Stellar Ecosystem

Create an Account

代码语言:javascript
复制
package com.step.controller;
import org.stellar.sdk.KeyPair;
import java.net.*;
import java.io.*;
import java.util.*;
import org.stellar.sdk.Server;
import org.stellar.sdk.responses.AccountResponse;

/**
 * Created by weixiang.wu on 2017/9/8.
 */
public class Test {
    
    public static void main(String[] args) throws IOException, DecoderException {
        KeyPair pair = KeyPair.random();
        System.out.println(new String(pair.getSecretSeed()));
        System.out.println(pair.getAccountId());
        String friendbotUrl = String.format(
                "https://horizon-testnet.stellar.org/friendbot?addr=%s",
                pair.getAccountId());
        InputStream response = new URL(friendbotUrl).openStream();
        String body = new Scanner(response, "UTF-8").useDelimiter("\\A").next();
        System.out.println("SUCCESS! You have a new account :)\n" + body);
        Server server = new Server("https://horizon-testnet.stellar.org");
        AccountResponse account = server.accounts().account(KeyPair.fromAccountId(pair.getAccountId()));
        System.out.println("Balances for account " + pair.getAccountId());
        for (AccountResponse.Balance balance : account.getBalances()) {
            System.out.println(String.format(
                    "Type: %s, Code: %s, Balance: %s",
                    balance.getAssetType(),
                    balance.getAssetCode(),
                    balance.getBalance()));
        }
}

Send Payments

Actions that change things in Stellar, like sending payments, changing your account, or making offers to trade various kinds of currencies, are called operations.[1] In order to actually perform an operation, you create a transaction, which is just a group of operations accompanied by some extra information, like what account is making the transaction and a cryptographic signature to verify that the transaction is authentic.[2]

If any operation in the transaction fails, they all fail. For example, let’s say you have 100 lumens and you make two payment operations of 60 lumens each. If you make two transactions (each with one operation), the first will succeed and the second will fail because you don’t have enough lumens. You’ll be left with 40 lumens. However, if you group the two payments into a single transaction, they will both fail and you’ll be left with the full 100 lumens still in your account.

Finally, every transaction costs a small fee. Like the minimum balance on accounts, this fee helps stop people from overloading the system with lots of transactions. Known as the base fee, it is very small—100 stroops per operation (that’s 0.00001 XLM; stroops are easier to talk about than such tiny fractions of a lumen).

A transaction with two operations would cost 200 stroops.[3]

Building a Transaction

Stellar stores and communicates transaction data in a binary format called XDR.[4] Luckily, the Stellar SDKs provide tools that take care of all that. Here’s how you might send 10 lumens to another account:

代码语言:javascript
复制
 /**
     * 创建一笔交易
     *
     * @param source1      来源账户
     * @param destination1 目标账户
     * @param hash         上传文件的hash值
     * @throws IOException
     */
    public static void buildingTransaction(String source1, String destination1, String hash) throws IOException, DecoderException {
        Logger logger = org.apache.log4j.LogManager.getLogger(BuildingTransaction.class);
          /*新网络 Network提供获取不同恒星网络的密码或id,提供了
        Network.current 类方法,返回该进程将用于生成签名的网络
        添加第一个签名之前,应该选择应用使用的网络
        usePublicNetwork(恒星公共网络) 或者 useTestNetWork(测试网络) 方法。
        */
        Network.useTestNetwork();
        Server server = new Server("https://horizon-testnet.stellar.org");
        //不安全的人从一个strkey编码的星际秘密种子中创造出一个新的恒星密钥。这个方法是不安全的。只有当意识到安全问题时才使用。
        KeyPair source = null;
        try {
            source = KeyPair.fromSecretSeed(source1);
        } catch (Exception e) {
            throw new RuntimeException("私钥验证失败!");
        }
        //从一个字符串编码的恒星账户ID创建一个新的恒星密钥

        KeyPair destination = null;
        try {
            destination = KeyPair.fromAccountId(destination1);
        } catch (Exception e) {
            throw new RuntimeException("公钥验证失败!");
        }

        //1.通过从Stellar网络加载关联的帐户数据,确认要发送的帐户ID实际存在
        server.accounts().account(destination);
        //2.加载要发送的帐户的数据。一个帐户只能一次执行一个事务[5],并且有一个称为序列号的东西,这有助于Stellar验证事务的顺序
        // 交易的序列号需要与帐户的序列号匹配,oi。
        AccountResponse sourceAccount = server.accounts().account(source);

        //3.开始建立一个交易,需要账户对象,不仅仅是账户id,因为它会增加账户的序列号
        //构造一个新的事物构建器→为该事物添加一个新操作→为该事务添加一份备忘录→构建一个事务,它将增加源帐户的序列号。
        //4.addOperation()将付款操作添加到该账户.
        //new PaymentOperation.Builder(destination(将资产发送到目的帐户), new AssetTypeNative()(返回资产的类型), "0.00001"(asset发送的资产总额))创建一个新的PaymentOperation构建器
        //5.addMemo()添加元数据(备忘录)
        //build()构建一个事务,增加源账户的序列号。
        Transaction transaction = new Transaction.Builder(sourceAccount)
                .addOperation(new PaymentOperation.Builder(destination, new AssetTypeNative(), "0.00001"/*浮点数可能会不准确*/).build())
                .addMemo(Memo.hash(hash))/*对此数据不做任何操作*/
                .build();
        //6.为该事务添加一个新的签名
        transaction.sign(source);/*必须使用密码种子进行加密签名,证明是你的交易而不是冒充你的人*/
        try {
            //7.将它发送到恒星网络上.
            SubmitTransactionResponse response = server.submitTransaction(transaction);
            System.out.println("支付成功!");
            System.out.println(response);
        } catch (Exception e) {
            logger.error("创建交易时发生异常,异常信息:", e);
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/09/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Stellar Network Overview
  • Create an Account
    • Send Payments
      • Building a Transaction
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档