-
Notifications
You must be signed in to change notification settings - Fork 0
/
StdlibDemo.java
80 lines (65 loc) · 3.18 KB
/
StdlibDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0
import java.util.Arrays;
import java.util.ArrayList;
import com.novi.serde.Bytes;
import com.novi.serde.Unsigned; // used as documentation.
import com.diem.stdlib.Helpers;
import com.diem.stdlib.ScriptCall;;
import com.diem.stdlib.ScriptFunctionCall;;
import com.diem.types.AccountAddress;
import com.diem.types.Identifier;
import com.diem.types.Script;
import com.diem.types.StructTag;
import com.diem.types.TypeTag;
import com.diem.types.TransactionPayload;
public class StdlibDemo {
private static void demo_p2p_script() throws Exception {
StructTag.Builder builder = new StructTag.Builder();
builder.address = AccountAddress.valueOf(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
builder.module = new Identifier("XDX");
builder.name = new Identifier("XDX");
builder.type_params = new ArrayList<com.diem.types.TypeTag>();
StructTag tag = builder.build();
TypeTag token = new TypeTag.Struct(tag);
AccountAddress payee = AccountAddress.valueOf(
new byte[]{0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22});
@Unsigned Long amount = Long.valueOf(1234567);
Script script =
Helpers.encode_peer_to_peer_with_metadata_script(token, payee, amount, Bytes.empty(), Bytes.empty());
ScriptCall.PeerToPeerWithMetadata call = (ScriptCall.PeerToPeerWithMetadata)Helpers.decode_script(script);
assert(call.amount.equals(amount));
assert(call.payee.equals(payee));
byte[] output = script.bcsSerialize();
for (byte o : output) {
System.out.print(((int) o & 0xFF) + " ");
};
System.out.println();
}
private static void demo_p2p_script_function() throws Exception {
StructTag.Builder builder = new StructTag.Builder();
builder.address = AccountAddress.valueOf(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
builder.module = new Identifier("XDX");
builder.name = new Identifier("XDX");
builder.type_params = new ArrayList<com.diem.types.TypeTag>();
StructTag tag = builder.build();
TypeTag token = new TypeTag.Struct(tag);
AccountAddress payee = AccountAddress.valueOf(
new byte[]{0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22});
@Unsigned Long amount = Long.valueOf(1234567);
TransactionPayload payload =
Helpers.encode_peer_to_peer_with_metadata_script_function(token, payee, amount, Bytes.empty(), Bytes.empty());
ScriptFunctionCall.PeerToPeerWithMetadata call = (ScriptFunctionCall.PeerToPeerWithMetadata)Helpers.decode_script_function_payload(payload);
assert(call.amount.equals(amount));
assert(call.payee.equals(payee));
byte[] output = payload.bcsSerialize();
for (byte o : output) {
System.out.print(((int) o & 0xFF) + " ");
};
System.out.println();
}
public static void main(String[] args) throws Exception {
demo_p2p_script();
demo_p2p_script_function();
}
}