Radix Java客户端库-入门

Introduction

In this guide, we'll build a small distributed App (DApp) from the ground up using the Java client library. The techniques you’ll learn in this tutorial are fundamental to make any DApp on Radix, and mastering it will give you a better understanding of the Radix distributed ledger.

About our example DApp

作为本指南的DApp示例,我们将构建一个简单的ChatBot,该ChatBot可以接收和回复发送到特定Radix地址的消息。 通过我们的小型ChatBot DApp,您将学到:

-引导并连接到Radix分布式分类帐 -创建基数标识 -从此身份创建基数地址 -发送和接收加密的消息到其他基数地址

如果您不熟悉Radix的概念,请不要担心,因为我们将在此过程中回顾基本的构建基块。

Basic setup

我们的第一步是进行设置,以便您可以开始使用Radix构建第一个Java DApp。

Installation

您可以安装radixdlt-java通过gradle库:

repositories {
    maven { url 'https://jitpack.io' }
}
implementation 'com.radixdlt:radixdlt-java:0.9.3'

Recommendations

在本指南中,我们假定您对Java有一定的了解,但是即使您来自其他编程语言,也应该能够继续学习。

If you need to review Java, we recommend reading this guide.

我们还将假设您熟悉编程概念,例如函数,对象,数组和类。 另外,了解反应性和可观察的模式是有帮助的,但不是必需的。

Overview

设置完成后,请随时查看我们的词汇表,以便我们共享一种通用语言:

-Universe
-Shards
-Nodes
-Atoms
-Account
-Address
-Identity

Building a ChatBot

现在,我们已经对Radix的概念进行了简要概述,并且我们共享一种通用语言,我们准备开始使用以下示例来构建示例ChatBot DApp:radixdlt-java图书馆。

Connecting to the network

The first step, before we can interact with the ledger, is to choose which Universe we want to connect to. In this case, we will use the ALPHANET universe configuration since it's our main testing environment.

我们首先创建一个ChatBot类,然后使用初始化与Radix Universe的连接RadixUniverse.bootstrap()方法:

import com.radixdlt.client.core.RadixUniverse;

public class ChatBot {
    static {
        RadixUniverse.bootstrap(Bootstrap.ALPHANET);
    }

    public ChatBot() {
    }
}

Creating a Radix Identity

接下来,我们需要生成一个快速身份它将代表ChatBot处理私钥/公钥逻辑。 我们通过调用Simple快速身份()构造函数:

public class ChatBot {
    static {
        RadixUniverse.bootstrap(Bootstrap.ALPHANET);
    }

    public ChatBot() throws IOException {
        // Load Identity
        final RadixIdentity chatBotIdentity = new SimpleRadixIdentity("chatbot.key");
    }
}
注意:thechatbot.key文件是私钥将被保存和加载的位置。

Creating a Radix Address

现在,使用身份的公钥,我们可以生成一个唯一的Radix地址,该地址是ChatBot在Radix Universe中的唯一标识符。

public class ChatBot {
    static {
        RadixUniverse.bootstrap(Bootstrap.ALPHANET);
    }

    private final RadixAddress address;

    public ChatBot() throws IOException {
        // Load Identity
        final RadixIdentity chatBotIdentity = new SimpleRadixIdentity("chatbot.key");
        final ECPublicKey publicKey = chatBotIdentity.getPublicKey();
        this.address = RadixUniverse.getInstance().getAddressFrom(publicKey);
    }
}

Receiving messages

为ChatBot创建地址后,就可以开始接收消息了。 要获取消息,我们需要:

-初始化基数消息模块 -订阅一个听众到我们的地址 -解密每条消息(使用我们的身份) -打印我们收到的每条消息

public class ChatBot {
    static {
        RadixUniverse.bootstrap(Bootstrap.ALPHANET);
    }

    private final RadixAddress address;

    public ChatBot() throws IOException {
        // Load Identity
        final RadixIdentity chatBotIdentity = new SimpleRadixIdentity("chatbot.key");
        final ECPublicKey publicKey = chatBotIdentity.getPublicKey();
        this.address = RadixUniverse.getInstance().getAddressFrom(publicKey);

        // Subscribe/Decrypt messages
        final Observable<RadixMessage> messageObservables = RadixMessaging.getInstance()
            .getAllMessagesDecrypted(chatBotIdentity);

        // Print messages
        messageObservables.subscribe(System.out::println);
    }
}

Sending messages

现在,ChatBot可以接收消息了,我们可以通过将消息发送回发件人来使机器人回复。 我们这样做是: 订阅另一个听众到我们的地址 过滤消息(因此我们不响应自己的消息) 创建加密的回显消息并将其发送给原始发件人

public class ChatBot {
    static {
        RadixUniverse.bootstrap(Bootstrap.ALPHANET);
    }

    private final RadixAddress address;

    public ChatBot(Function<String,String> chatBotAlgorithm) {
        // Load Identity
        final RadixIdentity chatBotIdentity = new SimpleRadixIdentity("chatbot.key");
        final ECPublicKey publicKey = chatBotIdentity.getPublicKey();
        this.address = RadixUniverse.getInstance().getAddressFrom(publicKey);

        // Subscribe/Decrypt messages
        final Observable<RadixMessage> messageObservables = RadixMessaging.getInstance()
                                                                    .getAllMessagesDecrypted(chatBotIdentity);

        // Print messages
        messageObservables.subscribe(System.out::println);

        // Chatbot replies
        messageObservables
            .filter(message -> !message.getFrom().equals(address)) // Don't reply to ourselves!
            .filter(message -> Math.abs(message.getTimestamp() - System.currentTimeMillis()) < 60000) // Only reply to recent messages
            .subscribe(message ->
                RadixMessaging.getInstance()
                    .sendMessage(chatBotAlgorithm.apply(message.getContent()), chatBotIdentity, message.getFrom());
    }
}

Finishing touches

至此,我们已经拥有了简单的ChatBot DApp的所有基本构建块。 现在,要拥有完整且功能正常的DApp,让我们添加一些最后的内容:

-用一个Function<String,String>表示ChatBot算法以使其易于扩展 -建造一个主要通过向其地址发送消息来运行和测试的方法

import com.radixdlt.client.core.Bootstrap;
import com.radixdlt.client.core.RadixUniverse;
import com.radixdlt.client.core.address.RadixAddress;
import com.radixdlt.client.core.crypto.ECPublicKey;
import com.radixdlt.client.core.identity.OneTimeUseIdentity;
import com.radixdlt.client.core.identity.RadixIdentity;
import com.radixdlt.client.core.identity.SimpleRadixIdentity;
import com.radixdlt.client.core.network.AtomSubmissionUpdate;
import com.radixdlt.client.messaging.RadixMessage;
import com.radixdlt.client.messaging.RadixMessaging;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import java.util.function.Function;

public class ChatBot {
    static {
        RadixUniverse.bootstrap(Bootstrap.ALPHANET);
    }

    private final RadixAddress address;

    public ChatBot(Function<String,String> chatBotAlgorithm) throws Exception {
        // Load Identity
        final RadixIdentity chatBotIdentity = new SimpleRadixIdentity("chatbot.key");
        final ECPublicKey publicKey = chatBotIdentity.getPublicKey();
        this.address = RadixUniverse.getInstance().getAddressFrom(publicKey);

        // Subscribe/Decrypt messages
        final Observable<RadixMessage> messageObservables = RadixMessaging.getInstance()
            .getAllMessagesDecrypted(chatBotIdentity);

        // Print messages
        messageObservables.subscribe(System.out::println);

        // Chatbot replies
        messageObservables
            .filter(message -> !message.getFrom().equals(address)) // Don't reply to ourselves!
            .filter(message -> Math.abs(message.getTimestamp() - System.currentTimeMillis()) < 60000) // Only reply to recent messages
            .flatMap((io.reactivex.functions.Function<RadixMessage, ObservableSource<AtomSubmissionUpdate>>) message ->
                RadixMessaging.getInstance().sendMessage(chatBotAlgorithm.apply(message.getContent() + " from Bot!"), chatBotIdentity, message.getFrom())
            ).subscribe(System.out::println, Throwable::printStackTrace);
    }

    public static void main(String[] args) throws Exception {
        ChatBot chatBot = new ChatBot(Function.identity());
        System.out.println("Chatbot address: " + chatBot.address);

        // Send message to bot
        RadixIdentity oneTimeUseIdentity = new OneTimeUseIdentity();
        RadixMessaging.getInstance()
            .sendMessage("Hello World", oneTimeUseIdentity, chatBot.address)
            .subscribe(System.out::println);
    }
}

运行此命令将产生类似于以下的输出:

Chatbot address: JGX2vijpLPBqTbtbznT81MGM7vVSDNDT95xHuChVdHnGzk9vBie
JGePib7zXBWa4ExNkpCmMbfkNy8H6UP351gMsdoYMgsrGUC43aB -> JGX2vijpLPBqTbtbznT81MGM7vVSDNDT95xHuChVdHnGzk9vBie: Hello World
JGX2vijpLPBqTbtbznT81MGM7vVSDNDT95xHuChVdHnGzk9vBie -> JGePib7zXBWa4ExNkpCmMbfkNy8H6UP351gMsdoYMgsrGUC43aB: Hello World from Bot!

恭喜你! 现在,您已经创建了一个连接到Radix网络的ChatBot。

Join The Radix Community

Ťelegram for general chat
​Discord for developers chat
​Reddit for general discussion
Forum for technical discussion
Ťwitter for announcements
​Email newsletter for weekly updates
Mail to hello@radixdlt.com for general enquiries

from: https://dev.to//radixdlt/the-radix-java-client-library-getting-started-5426

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值