SDK 下载

JS SDK

下载

GitHub:https://github.com/MatrixAINetwork/aiman

通过npm安装

1
npm install aiman

使用示例

1
2
3
4
5
6
7
8
//初始化过程
var AIMan = require('AIMan');
if (typeof aiman !== 'undefined') {
aiman = new AIMan(aiman.currentProvider);
} else {
// set the provider you want from AIMan.providers
aiman = new AIMan(new AIMan.providers.HttpProvider("http://localhost:8567"));
}

JAVA SDK

安装使用

SDK下载

  • GitHub:https://github.com/MatrixAINetwork/aimanj

  • jar包列表:

    aimanjdemo/src/aiManj/core-1.0.0.jar

    aimanjdemo/src/aiManj/utils-1.0.0.jar

    aimanjdemo/src/aiManj/tuples-1.0.0.jar

    aimanjdemo/src/aiManj/rlp-1.0.0.jar

    aimanjdemo/src/aiManj/crypto-1.0.0.jar

    aimanjdemo/src/aiManj/abi-1.0.0.jar

SDK使用示例

安装 java 和 maven

示例程序依赖 Java 1.8 运行时环境以及Maven,可以通过如下命令确认是否已经安装。

1
2
3
4
$ mvn -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)
...
Java version: 1.8.0_161, vendor: Oracle Corporation

下载示例程序

默认示例程序已经配置jar包,若是新建项目,需手动导入jar包。

使用idea IDE,示例程序最终目录结构如下:

image

安装依赖

修改pom文件,添加如下依赖:

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
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.21</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.8.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.8.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>

右键pom文件,选择maven, reimport安装依赖

image

运行示例程序

更多详情参考aimanjdemo中的示例程序。

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package Utils;

import org.aimanj.crypto.*;
import org.aimanj.protocol.AiManj;
import org.aimanj.protocol.core.DefaultBlockParameterName;
import org.aimanj.protocol.core.methods.response.ManBlockNumber;
import org.aimanj.protocol.core.methods.response.ManGetTransactionCount;
import org.aimanj.protocol.core.methods.response.ManSendTransaction;
import org.aimanj.protocol.http.HttpService;
import org.aimanj.utils.Numeric;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class sendTx {

public static void main(String[] args) throws Exception {
AiManj aiManj = AiManj.build(new HttpService("https://testnet.matrix.io"));
ManGetTransactionCount count = aiManj
.manGetTransactionCount("MAN.5xYzBHrJfXeJi9yQ8Qq8hvm19bU4", DefaultBlockParameterName.LATEST).send();

List list = new ArrayList();
List listResult = new ArrayList();
List list1 = new ArrayList();
list1.add(BigInteger.ZERO);
list1.add(BigInteger.ZERO);
List toList = new ArrayList();
toList.add("MAN.5xYzBHrJfXeJi9yQ8Qq8hvm19bU4");
toList.add(new BigInteger("1"));
toList.add(BigInteger.ZERO);
List tempList = new ArrayList();
tempList.add(toList);
list1.add(tempList);
listResult.add(list1);
list.add(listResult);
byte chainid = 3;
RawTransaction rawTransaction = RawTransaction.createTransaction(count.getTransactionCount(),
new BigInteger("18000000000"), new BigInteger("210000"), "MAN.2nRsUetjWAaYUizRkgBxGETimfUTz",
"0x" + str2HexStr("12131"), BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, list);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainid,
Credentials.create("3ec3678077a79400081e525f516d722bce7d19f80b7288d0992c84c2481c5faa"));
String hexValue = Numeric.toHexString(signedMessage);
SignedRawTransaction rawTransaction1 = (SignedRawTransaction) TransactionDecoder.decode(hexValue);
System.out.println(rawTransaction1.getFrom());
HashMap map = new HashMap();
map.put("nonce", Numeric.toHexStringWithPrefix(rawTransaction1.getNonce()));
map.put("v", "0x" + byteToHex(rawTransaction1.getSignatureData().getV()));
map.put("r", Numeric.toHexString(rawTransaction1.getSignatureData().getR()));
map.put("s", Numeric.toHexString(rawTransaction1.getSignatureData().getS()));
map.put("to", rawTransaction1.getTo());
map.put("currency", rawTransaction1.getTo().split("\\.")[0]);
map.put("gasPrice", Numeric.toHexStringWithPrefix(rawTransaction1.getGasPrice()));
map.put("gas", Numeric.toHexStringWithPrefix(rawTransaction1.getGasLimit()));
map.put("commitTime", rawTransaction1.getCommitTime());
map.put("isEntrustTx", rawTransaction1.getIsEntrustTx());
map.put("txEnterType", rawTransaction1.getTxEnterType());
List extraToList = (List) ((List) ((List) rawTransaction1.getExtra_to().get(0)).get(0)).get(2);
List extraTo = new ArrayList();
for (int i = 0, length = extraToList.size(); i < length; i++) {
HashMap<String, String> toMap = new HashMap<String, String>();
toMap.put("to", (String) ((List) extraToList.get(0)).get(0));
toMap.put("value", Numeric.toHexStringWithPrefix((BigInteger) ((List) extraToList.get(0)).get(1)));
extraTo.add(toMap);
}
map.put("extra_to", extraTo);
map.put("data", "0x" + rawTransaction1.getData());
map.put("txType", ((List) ((List) rawTransaction1.getExtra_to().get(0)).get(0)).get(0));
map.put("lockHeight", ((List) ((List) rawTransaction1.getExtra_to().get(0)).get(0)).get(1));
map.put("value", Numeric.toHexStringWithPrefix(rawTransaction1.getValue()));
ManSendTransaction transactionResponse = aiManj.manSendRawTransaction(map).sendAsync().get();
String hash = transactionResponse.getTransactionHash();
System.out.println(hash);
}

public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
// sb.append(' ');
}
return sb.toString().trim();
}

public static String byteToHex(byte b) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() < 2) {
hex = "0" + hex;
}
return hex;
}
}

运行发送交易示例程序显示如下结果

image