文档章节

stellar

吴伟祥
 吴伟祥
发布于 2017/09/08 16:59
字数 1016
阅读 17
收藏 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);
        }

 

© 著作权归作者所有

吴伟祥
粉丝 9
博文 340
码字总数 212901
作品 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
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
核财经独家|IBM进军区块链支付系统 详解如何实现秒级跨境支付

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

核财经
09/06
0
0
电驴创始人Jed McCaleb的传奇人生

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

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

没有更多内容

加载失败,请刷新页面

加载更多

理解JavaScript普通函数以及箭头函数里使用的this

this 普通函数的this 普通函数的this是由动态作用域决定,它总指向于它的直接调用者。具体可以分为以下四项: this总是指向它的直接调用者, 例如 obj.func() ,那么func()里的this指的是obj...

Jack088
7分钟前
0
0
RPC框架几行代码就够了

摘自梁飞的博客 - RPC框架几行代码就够了 简易RPC框架实现: /* * Copyright 2011 Alibaba.com All right reserved. This software is the * confidential and proprietary information ......

千面人
15分钟前
16
0
10 道关于 Java 泛型的面试题

1.Java中的泛型是什么 ? 使用泛型的好处是什么? 这是在各种Java泛型面试中,一开场你就会被问到的问题中的一个,主要集中在初级和中级面试中。那些拥有Java1.4或更早版本的开发背景的人都知道...

蚂蚁-Declan
25分钟前
1
0
配置Git

1.首先在本地创建ssh key; $ ssh-keygen -t rsa -C "your_email@youremail.com" 后面的your_email@youremail.com改为你在github上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认...

15834278076
26分钟前
0
0
day126-20181024-英语流利阅读-待学习

看脸时代,男人也要化妆 Lala 2018-10-24 1.今日导读 十年前,或许绝大多数男生都认为男性化妆是一件很滑稽的事。但现在,从电视荧幕上化妆的小鲜肉,到社交媒体涌现的大批男性美妆博主、网络...

飞鱼说编程
27分钟前
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部