stellar 原

Stellar Network Overview

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);
        }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏工科狗和生物喵

FreeRTOS-Qemu 实现三任务同步通信机制以及API信息

---- 1. 本次作业的考察要点: 作业地址:https://github.com/HustWolfzzb/STM32F429DiscoveryFreeRTO...

1.1K6
来自专栏小尘哥的专栏

小程序(3):授权登录

判断是否授权,如果没有,则显示授权按钮。注意上面的open-type="getUserInfo",这个会自动调起授权框。看一下js

2484
来自专栏JavaEE

Java调用微信登录以及eclipse 远程调试前言:一、微信测试号的连接与申请:二、eclipse远程调试:总结:

3415
来自专栏JetpropelledSnake

Django学习笔记之利用Form和Ajax实现注册功能

1705
来自专栏Kubernetes

深度解析Kubernetes Local Persistent Volume(二)

摘要:上一篇博客”深度解析Kubernetes Local Persistent Volume(一)“对local volume的基本原理和注意事项进行了分析,...

1.4K3
来自专栏玄魂工作室

Msfvenom后门重新学习与分析-windows篇

Msfvenom 是msf框架配套的攻击载荷生成器。 什么是攻击荷载: Payload:目标系统上渗透成功后执行的代码 msfvenom命令行选项如下: ? ?...

3698
来自专栏JAVA高级架构

结合RPC框架通信谈 netty如何解决TCP粘包问题

因为自己造一个RPC框架的轮子时,需要解决TCP的粘包问题,特此记录,希望方便他人。这是我写的RPC框架的 GitHub地址 https://github.co...

1043
来自专栏Seebug漏洞平台

CVE-2017-16943 Exim UAF漏洞分析

作者:Hcamael@知道创宇404实验室 感恩节那天,meh在Bugzilla上提交了一个exim的uaf漏洞:https://bugs.exim.org/s...

4176
来自专栏玄魂工作室

Msfvenom后门重新学习与分析-windows篇

Msfvenom 是msf框架配套的攻击载荷生成器。 什么是攻击荷载: Payload:目标系统上渗透成功后执行的代码 msfvenom命令行选项如下: ? ?...

5359
来自专栏影子

开始食用grpc(之二)

转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9570992.html

5664

扫码关注云+社区

领取腾讯云代金券