文档章节

stellar

吴伟祥
 吴伟祥
发布于 2017/09/08 16:59
字数 1016
阅读 20
收藏 0

Stellar Network Overview

Stellar Ecosystem

Create an Account

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:

 /**
     * 创建一笔交易
     *
     * @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);
        }

 

© 著作权归作者所有

共有 人打赏支持
吴伟祥
粉丝 10
博文 387
码字总数 266108
作品 0
泉州
后端工程师
私信 提问
前端——Stellar.js 制作视差滚动

近些年讨论得很热烈的设计趋势是视觉差滚动效果。不管你喜不喜欢,很多网站都在用它。在本教程中,我会介绍视觉差滚动和用jQuery插件Stellar.js来制作视觉差滚动效果。 视差滚动(Parallax ...

fycool
2016/11/08
61
0
区块链精进手册 | 007 | 精准空投是创业机会吗?

1. 一种通证:XLM XLM是Stellar区块链网络的通证,总量恒定发行1000多亿枚,目前市值约41亿美元,在数字货币市值排名中位列第七。 XLM的英文名叫做Lumens,被译为流明币,XLM为其数字货币英文...

马烈_Bodhi
08/27
0
0
核财经独家|IBM进军区块链支付系统 详解如何实现秒级跨境支付

近日,由IBM开发的区块链全球支付系统——IBM Blockchain World Wire正式上线,此套支付系统采用的是Stellar协议。对,没错,这里所说的Stellar就是恒星币的那个公司。 IBM布局区块链这个事情...

核财经
09/06
0
0
AugustRush/Stellar

A fantastic Physical animation library for swift(Not Just Spring !!!), it is base on UIDynamic and extension to it, friendly APIs make you use it or custom your own animation ve......

AugustRush
2016/07/02
0
0
电驴创始人Jed McCaleb的传奇人生

(本文首发于微信公众号“万云BaaS”) Jed McCaleb,区块链项目Stellar联合创始人兼首席技术官、Ripple创始人,也是eDonkey(电驴)的创始人,是电驴各时期版本的软件的作者和主要开发者。e...

万向区块链实验室
01/11
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周六乱弹 —— 你一口我一口多咬一口是小狗

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @达尔文 :分享Roy Orbison的单曲《She's a Mystery to Me》 《She's a Mystery to Me》- Roy Orbison 手机党少年们想听歌,请使劲儿戳(这里...

小小编辑
今天
141
4
Spring源码学习笔记-1-Resource

打算补下基础,学习下Spring源码,参考书籍是《Spring源码深度解析》,使用版本是Spring 3.2.x,本来想试图用脑图记录的,发现代码部分不好贴,还是作罢,这里只大略记录下想法,不写太细了 ...

zypy333
今天
13
0
RestClientUtil和ConfigRestClientUtil区别说明

RestClientUtil directly executes the DSL defined in the code. ConfigRestClientUtil gets the DSL defined in the configuration file by the DSL name and executes it. RestClientUtil......

bboss
今天
21
0

中国龙-扬科
昨天
4
0
Linux系统设置全局的默认网络代理

更改全局配置文件/etc/profile all_proxy="all_proxy=socks://rahowviahva.ml:80/"ftp_proxy="ftp_proxy=http://rahowviahva.ml:80/"http_proxy="http_proxy=http://rahowviahva.ml:80/"......

临江仙卜算子
昨天
14
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部