derivation) {
41 | return derivation.derive(rootNode, derivationPath, standardCkdFunction);
42 | }
43 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/crypto/bip32/derivation/CkdFunctionResultCacheDecorator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * BIP32derivation
3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | *
18 | * Original source: https://github.com/NovaCrypto/BIP32derivation
19 | * You can contact the authors via github issues.
20 | */
21 |
22 | package com.github.ontio.crypto.bip32.derivation;
23 |
24 | import java.util.HashMap;
25 | import java.util.Map;
26 |
27 | /**
28 | * Non-thread safe result cache for ckd functions.
29 | *
30 | * If the same child of the same parent is requested a second time, the original result will be returned.
31 | *
32 | * @param Key Node type.
33 | */
34 | public final class CkdFunctionResultCacheDecorator implements CkdFunction {
35 |
36 | private final CkdFunction decoratedCkdFunction;
37 |
38 | private final Map> cache = new HashMap<>();
39 |
40 | private CkdFunctionResultCacheDecorator(final CkdFunction decoratedCkdFunction) {
41 | this.decoratedCkdFunction = decoratedCkdFunction;
42 | }
43 |
44 | @Override
45 | public Key deriveChildKey(final Key parent, final int childIndex) {
46 | final Map mapForParent = getMapOf(parent);
47 | Key child = mapForParent.get(childIndex);
48 | if (child == null) {
49 | child = decoratedCkdFunction.deriveChildKey(parent, childIndex);
50 | mapForParent.put(childIndex, child);
51 | }
52 | return child;
53 | }
54 |
55 | private Map getMapOf(final Key parentKey) {
56 | HashMap mapForParent = cache.get(parentKey);
57 | if (mapForParent == null) {
58 | mapForParent = new HashMap<>();
59 | cache.put(parentKey, mapForParent);
60 | }
61 | return mapForParent;
62 | }
63 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/crypto/bip32/derivation/Derivation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * BIP32 library, a Java implementation of BIP32
3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | *
18 | * Original source: https://github.com/NovaCrypto/BIP32
19 | * You can contact the authors via github issues.
20 | */
21 |
22 | package com.github.ontio.crypto.bip32.derivation;
23 |
24 | import com.github.ontio.crypto.bip32.HdKey;
25 |
26 | public interface Derivation {
27 |
28 | /**
29 | * Traverse the nodes from the root key node to find the node referenced by the path.
30 | *
31 | * @param rootKey The root of the path
32 | * @param path The path to follow
33 | * @param ckdFunction Allows you to follow one link
34 | * @param The type of node we are visiting
35 | * @return The final node found at the end of the path
36 | */
37 | Key derive(final Key rootKey,final Path path, final CkdFunction ckdFunction);
38 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/crypto/bip32/derivation/Derive.java:
--------------------------------------------------------------------------------
1 | /*
2 | * BIP32 library, a Java implementation of BIP32
3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | *
18 | * Original source: https://github.com/NovaCrypto/BIP32
19 | * You can contact the authors via github issues.
20 | */
21 |
22 | package com.github.ontio.crypto.bip32.derivation;
23 |
24 | public interface Derive {
25 |
26 | /**
27 | * Derive from a string path such as m/44'/0'/0'/0/1
28 | *
29 | * @param derivationPath Path
30 | * @return Key at the path
31 | */
32 | Key fromPath(final CharSequence derivationPath);
33 |
34 | /**
35 | * Derive from a generic path using the {@link Derivation} supplied to extract the child indexes
36 | *
37 | * @param derivationPath Path
38 | * @param derivation The class that extracts the path elements
39 | * @param The generic type of the path
40 | * @return Key at the path
41 | */
42 | Key fromPath(final Path derivationPath, final Derivation derivation);
43 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/io/Serializable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.io;
21 |
22 | import com.github.ontio.common.Helper;
23 |
24 | import java.io.*;
25 |
26 | /**
27 | * Serialize interface
28 | */
29 | public interface Serializable {
30 | static T from(byte[] value, Class t) throws InstantiationException, IllegalAccessException {
31 | try (ByteArrayInputStream ms = new ByteArrayInputStream(value)) {
32 | try (BinaryReader reader = new BinaryReader(ms)) {
33 | return reader.readSerializable(t);
34 | }
35 | } catch (IOException ex) {
36 | throw new IllegalArgumentException(ex);
37 | }
38 | }
39 |
40 | /**
41 | * @param reader
42 | * @throws IOException
43 | */
44 | void deserialize(BinaryReader reader) throws IOException;
45 |
46 | /**
47 | * @param writer
48 | * @throws IOException
49 | */
50 | void serialize(BinaryWriter writer) throws IOException;
51 |
52 | default byte[] toArray() {
53 | try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) {
54 | try (BinaryWriter writer = new BinaryWriter(ms)) {
55 | serialize(writer);
56 | writer.flush();
57 | return ms.toByteArray();
58 | }
59 | } catch (IOException ex) {
60 | throw new UnsupportedOperationException(ex);
61 | }
62 | }
63 |
64 | default String toHexString() {
65 | return Helper.toHexString(toArray());
66 | }
67 |
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/io/utils.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.io;
2 |
3 | import com.github.ontio.common.Address;
4 | import com.github.ontio.common.Helper;
5 |
6 | import java.io.IOException;
7 | import java.math.BigInteger;
8 |
9 | public class utils {
10 |
11 | public static long readVarInt(BinaryReader reader) throws IOException {
12 | byte[] r = reader.readVarBytes();
13 | BigInteger b = Helper.BigIntFromNeoBytes(r);
14 | return b.longValue();
15 | }
16 | public static Address readAddress(BinaryReader reader) throws IOException {
17 | byte[] r = reader.readVarBytes();
18 | return new Address(r);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/connect/AbstractConnector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.connect;
21 |
22 |
23 | public abstract class AbstractConnector implements IConnector {
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/exception/ConnectorException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.exception;
21 |
22 |
23 | public class ConnectorException extends Exception {
24 | private static final long serialVersionUID = 1110342144692879043L;
25 |
26 | public ConnectorException(String message) {
27 | super(message);
28 | }
29 |
30 | public ConnectorException(String message, Throwable ex) {
31 | super(message, ex);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/exception/RestfulException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.exception;
21 |
22 | public class RestfulException extends ConnectorException {
23 | private static final long serialVersionUID = -8558006777817318117L;
24 |
25 | public RestfulException(String message) {
26 | super(message);
27 | }
28 |
29 | public RestfulException(String message, Throwable ex) {
30 | super(message, ex);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/exception/RpcException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.exception;
21 |
22 | /**
23 | *
24 | */
25 | public class RpcException extends ConnectorException {
26 | private static final long serialVersionUID = -8558006777817318117L;
27 |
28 | public final int code;
29 |
30 | public RpcException(int code,String message) {
31 | super(message);
32 | this.code = code;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/rest/Result.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.rest;
21 |
22 | import com.alibaba.fastjson.JSON;
23 |
24 | public class Result {
25 | public String Action;
26 | public long Error;
27 | public String Desc;
28 | public Object Result;
29 | public String Version;
30 | @Override
31 | public String toString() {
32 | return JSON.toJSONString(this);
33 | }
34 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/rest/X509.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.rest;
21 |
22 | import javax.net.ssl.X509TrustManager;
23 | import java.security.cert.CertificateException;
24 | import java.security.cert.X509Certificate;
25 |
26 | /**
27 | *
28 | */
29 | public class X509 implements X509TrustManager {
30 | @Override
31 | public X509Certificate[] getAcceptedIssuers() {
32 | return null;
33 | }
34 | @Override
35 | public void checkClientTrusted(X509Certificate[] chain, String authType)
36 | throws CertificateException {
37 | }
38 | @Override
39 | public void checkServerTrusted(X509Certificate[] chain, String authType)
40 | throws CertificateException {
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/websocket/MsgQueue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.websocket;
21 |
22 | import com.alibaba.fastjson.JSON;
23 |
24 | import java.util.*;
25 |
26 | /**
27 | *
28 | */
29 | public class MsgQueue {
30 |
31 | private static Set resultSet = new HashSet();
32 | public static void addResult(Result obj) {
33 | resultSet.add(JSON.toJSONString(obj));
34 | }
35 | public static Set getResultSet(){
36 | Set rt = new HashSet();
37 | rt.addAll(resultSet);
38 | return rt;
39 | }
40 |
41 | public static void removeResult(String ele){
42 | resultSet.remove(ele);
43 | }
44 | public static void clear(){
45 | resultSet.clear();
46 | }
47 | public static int size() {
48 | return resultSet.size();
49 | }
50 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/network/websocket/Result.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.network.websocket;
21 |
22 | /**
23 | *
24 | */
25 | public class Result {
26 | public String Action;
27 | public long Error;
28 | public String Desc;
29 | public Object Result;
30 | public String Version;
31 | public Object Id;
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/CredentialStatus.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 |
4 | import com.alibaba.fastjson.annotation.JSONType;
5 |
6 | @JSONType(orders = {"id", "type"})
7 | public class CredentialStatus {
8 |
9 | public String id; // should be CredentialRecord contract address
10 | public CredentialStatusType type;
11 |
12 | public CredentialStatus() {
13 | }
14 |
15 | public CredentialStatus(String scriptHash, CredentialStatusType type) {
16 | this.id = scriptHash;
17 | this.type = type;
18 | }
19 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/CredentialStatusType.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | public enum CredentialStatusType {
4 | AttestContract,
5 | RevocationList
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/OntIdPubKey.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | public class OntIdPubKey {
4 | public String id; // pubkey URI
5 | public PubKeyType type; // pubkey type, for example: EcdsaSecp256r1VerificationKey2019
6 | public String controller;
7 | public String publicKeyHex;
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/OntIdSigner.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | import com.github.ontio.account.Account;
4 |
5 | public class OntIdSigner {
6 | String ontId;
7 | OntIdPubKey pubKey;
8 | Account signer;
9 |
10 | public OntIdSigner(String ontId, OntIdPubKey pubKey, Account signer) {
11 | this.ontId = ontId;
12 | this.pubKey = pubKey;
13 | this.signer = signer;
14 | }
15 |
16 | // public byte[] hash(byte[] msg) throws Exception {
17 | // return pubKey.type.getAlg().hash(msg);
18 | // }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/Proof.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | import com.alibaba.fastjson.annotation.JSONType;
4 | import com.github.ontio.account.Account;
5 | import com.github.ontio.common.Helper;
6 |
7 | @JSONType(orders = {"type", "created", "challenge", "domain", "proofPurpose", "verificationMethod", "hex", "jws"})
8 | public class Proof {
9 | public PubKeyType type;
10 | public String created; // time stamp
11 | public String challenge;
12 | public Object domain;
13 | public ProofPurpose proofPurpose;
14 | public String verificationMethod; // pubkey uri
15 | public String hex;
16 | public String jws;
17 |
18 | public Proof() {
19 | }
20 |
21 | public Proof(String publicKeyURI, String created, PubKeyType type, ProofPurpose proofPurpose) {
22 | this.type = type;
23 | this.created = created;
24 | if (proofPurpose == null) {
25 | proofPurpose = ProofPurpose.assertionMethod;
26 | }
27 | this.proofPurpose = proofPurpose;
28 | this.verificationMethod = publicKeyURI;
29 | }
30 |
31 | public Proof(String publicKeyURI, String created, PubKeyType type, ProofPurpose proofPurpose,
32 | String challenge, Object domain) {
33 | this(publicKeyURI, created, type, proofPurpose);
34 | this.challenge = challenge;
35 | this.domain = domain;
36 | }
37 |
38 | public Proof genNeedSignProof() {
39 | return new Proof(verificationMethod, created, type, proofPurpose, challenge, domain);
40 | }
41 |
42 | public Proof genJWTProof() {
43 | Proof proof = new Proof();
44 | proof.created = created;
45 | proof.proofPurpose = proofPurpose;
46 | proof.hex = hex;
47 | return proof;
48 | }
49 |
50 | public void fillHexSignature(Account account, byte[] needSignData) throws Exception {
51 | byte[] sig = account.generateSignature(needSignData, account.getSignatureScheme(), null);
52 | hex = Helper.toHexString(sig);
53 | }
54 |
55 | public byte[] parseHexSignature() {
56 | return Helper.hexToBytes(hex);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/ProofPurpose.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | public enum ProofPurpose {
4 | assertionMethod
5 | }
6 | // TODO: authentication
7 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/PubKeyType.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | import com.github.ontio.crypto.SignatureScheme;
4 |
5 | public enum PubKeyType {
6 | EcdsaSecp224r1VerificationKey2019(ALG.ES224, ALG.TYPE_ECDSA, ALG.CURVE_P224, ALG.HASH_224, SignatureScheme.SHA224WITHECDSA),
7 | EcdsaSecp256r1VerificationKey2019(ALG.ES256, ALG.TYPE_ECDSA, ALG.CURVE_P256, ALG.HASH_256, SignatureScheme.SHA256WITHECDSA),
8 | EcdsaSecp384r1VerificationKey2019(ALG.ES384, ALG.TYPE_ECDSA, ALG.CURVE_P384, ALG.HASH_384, SignatureScheme.SHA384WITHECDSA),
9 | EcdsaSecp521r1VerificationKey2019(ALG.ES512, ALG.TYPE_ECDSA, ALG.CURVE_P521, ALG.HASH_512, SignatureScheme.SHA512WITHECDSA),
10 | EcdsaSecp256k1VerificationKey2019(ALG.ES256K, ALG.TYPE_ECDSA, ALG.CURVE_secp256k1, ALG.HASH_256, SignatureScheme.SHA256WITHECDSA),
11 | Ed25519VerificationKey2018(ALG.EdDSA, ALG.TYPE_EDDSA, ALG.CURVE_Curve25519, ALG.HASH_256, null),
12 | SM2VerificationKey2019(ALG.SM, ALG.TYPE_SM2, ALG.CURVE_SM2P256V1, ALG.HASH_SM3, SignatureScheme.SM3WITHSM2);
13 |
14 | private ALG alg;
15 | private String algType;
16 | private String curve;
17 | private String hashMethod;
18 | private SignatureScheme signatureScheme;
19 |
20 | PubKeyType(ALG alg, String algType, String curve, String hashMethod, SignatureScheme scheme) {
21 | this.alg = alg;
22 | this.algType = algType;
23 | this.curve = curve;
24 | this.hashMethod = hashMethod;
25 | this.signatureScheme = scheme;
26 |
27 | // inject self to alg pub key type
28 | alg.setProofPubKeyType(this);
29 | }
30 |
31 | public ALG getAlg() {
32 | return alg;
33 | }
34 |
35 | public String getAlgType() {
36 | return algType;
37 | }
38 |
39 | public String getCurve() {
40 | return curve;
41 | }
42 |
43 | public String getHashMethod() {
44 | return hashMethod;
45 | }
46 |
47 | public SignatureScheme getSignatureScheme() {
48 | return signatureScheme;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/PubKeyTypeFactory.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | import com.github.ontio.sdk.exception.SDKException;
4 |
5 | public class PubKeyTypeFactory {
6 | public static PubKeyType genPubKeyType(String pubKeyType) throws Exception {
7 | if ("EcdsaSecp224r1VerificationKey2019".equals(pubKeyType)) {
8 | return PubKeyType.EcdsaSecp224r1VerificationKey2019;
9 | } else if ("EcdsaSecp256r1Signature2019".equals(pubKeyType)) {
10 | return PubKeyType.EcdsaSecp256r1VerificationKey2019;
11 | } else if ("EcdsaSecp384r1VerificationKey2019".equals(pubKeyType)) {
12 | return PubKeyType.EcdsaSecp384r1VerificationKey2019;
13 | } else if ("EcdsaSecp521r1VerificationKey2019".equals(pubKeyType)) {
14 | return PubKeyType.EcdsaSecp521r1VerificationKey2019;
15 | } else if ("EcdsaSecp256k1VerificationKey2019".equals(pubKeyType)) {
16 | return PubKeyType.EcdsaSecp256k1VerificationKey2019;
17 | } else if ("Ed25519VerificationKey2018".equals(pubKeyType)) {
18 | return PubKeyType.Ed25519VerificationKey2018;
19 | } else if ("SM2VerificationKey2019".equals(pubKeyType)) {
20 | return PubKeyType.SM2VerificationKey2019;
21 | } else {
22 | throw new SDKException("un support pub key type");
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/SignRequest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.alibaba.fastjson.annotation.JSONType;
5 | import com.alibaba.fastjson.serializer.SerializerFeature;
6 |
7 | @JSONType(orders = {"credentialSubject", "ontId", "proof"})
8 | public class SignRequest {
9 | Object credentialSubject;
10 | String ontId;
11 | Proof proof;
12 |
13 | public SignRequest(Object credentialSubject, String ontId, Proof proof) {
14 | this.credentialSubject = credentialSubject;
15 | this.ontId = ontId;
16 | this.proof = proof;
17 | }
18 |
19 | public byte[] genNeedSignData() {
20 | Proof proof = this.proof;
21 | this.proof = this.proof.genNeedSignProof();
22 | String jsonStr = JSON.toJSONString(this, SerializerFeature.MapSortField);
23 | this.proof = proof;
24 | return jsonStr.getBytes();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/Util.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.alibaba.fastjson.JSONArray;
5 | import com.alibaba.fastjson.JSONObject;
6 | import com.github.ontio.sdk.exception.SDKException;
7 |
8 | import java.util.Set;
9 | import java.util.TreeSet;
10 |
11 | public class Util {
12 |
13 | public static int getIndexFromPubKeyURI(String pubKeyURI) throws Exception {
14 | String[] keyInfo = pubKeyURI.split("#keys-");
15 | if (keyInfo.length != 2) {
16 | throw new SDKException(String.format("invalid pubKeyURI %s", pubKeyURI));
17 | }
18 | return Integer.parseInt(keyInfo[1]);
19 | }
20 |
21 | public static String getOntIdFromPubKeyURI(String pubKeyURI) throws Exception {
22 | String[] keyInfo = pubKeyURI.split("#keys-");
23 | if (keyInfo.length != 2) {
24 | throw new SDKException(String.format("invalid pubKeyURI %s", pubKeyURI));
25 | }
26 | return keyInfo[0];
27 | }
28 |
29 | // fetch "id" field of object
30 | // if object doesn't contain "id" field, return ""
31 | // if object is array, return ""
32 | public static String fetchId(Object object) {
33 | if (object == null) {
34 | return "";
35 | }
36 | if (object instanceof String) {
37 | return (String) object;
38 | }
39 | if (object.getClass().isPrimitive()) {
40 | return "";
41 | }
42 | if (object instanceof JSONArray) {
43 | return "";
44 | }
45 | if (object.getClass().isArray()) {
46 | return "";
47 | }
48 | JSONObject jsonObject = (JSONObject) JSONObject.toJSON(object);
49 | String id = jsonObject.getString("id");
50 | if (id == null) {
51 | return "";
52 | }
53 | return id;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/jwt/JWTHeader.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid.jwt;
2 |
3 | import com.alibaba.fastjson.annotation.JSONType;
4 | import com.github.ontio.ontid.*;
5 |
6 | @JSONType(orders = {"alg", "kid", "typ"})
7 | public class JWTHeader {
8 | public ALG alg;
9 | public String kid; // VerifiableCredential issuer
10 | public String typ = "JWT";
11 |
12 | public JWTHeader() {
13 | }
14 |
15 | public JWTHeader(ALG alg, String kid) {
16 | this.alg = alg;
17 | this.kid = kid;
18 | }
19 |
20 | public JWTHeader(PubKeyType pubKeyType, String kid) {
21 | this.alg = pubKeyType.getAlg();
22 | this.kid = kid;
23 | }
24 |
25 | public JWTHeader(VerifiableCredential credential) {
26 | this.alg = credential.proof.type.getAlg();
27 | this.kid = credential.proof.verificationMethod;
28 | }
29 |
30 | public JWTHeader(Proof proof) throws Exception {
31 | this.alg = proof.type.getAlg();
32 | this.kid = proof.verificationMethod;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/jwt/JWTVC.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid.jwt;
2 |
3 | import com.alibaba.fastjson.JSONArray;
4 | import com.alibaba.fastjson.JSONObject;
5 | import com.alibaba.fastjson.annotation.JSONField;
6 | import com.alibaba.fastjson.annotation.JSONType;
7 | import com.github.ontio.ontid.CredentialStatus;
8 | import com.github.ontio.ontid.Proof;
9 | import com.github.ontio.ontid.VerifiableCredential;
10 | import com.github.ontio.sdk.exception.SDKException;
11 |
12 | @JSONType(orders = {"@context", "type", "issuer", "credentialSubject", "credentialStatus", "proof"})
13 | public class JWTVC {
14 | @JSONField(name = "@context")
15 | public String[] context;
16 | public String[] type;
17 | public Object issuer;
18 | public Object credentialSubject;
19 | public CredentialStatus credentialStatus;
20 | public Proof proof;
21 |
22 | public JWTVC() {
23 | }
24 |
25 | public JWTVC(VerifiableCredential credential) throws Exception {
26 | this.context = credential.context;
27 | this.type = credential.type;
28 | this.credentialStatus = credential.credentialStatus;
29 | if (credential.proof != null) {
30 | this.proof = credential.proof.genJWTProof();
31 | }
32 | if (credential.issuer.getClass().isPrimitive() || credential.issuer.getClass().isArray() ||
33 | credential.issuer instanceof JSONArray) {
34 | throw new SDKException("illegal credential issuer");
35 | }
36 | if (!(credential.issuer instanceof String)) {
37 | JSONObject jsonObject = (JSONObject) JSONObject.toJSON(credential.issuer);
38 | jsonObject.remove("id");
39 | if (jsonObject.size() > 0) {
40 | this.issuer = jsonObject;
41 | }
42 | }
43 | // remove id attribute
44 | if (credential.credentialSubject != null && !credential.credentialSubject.getClass().isArray()
45 | && !(credential.credentialSubject instanceof JSONArray)) {
46 | JSONObject credentialSubject = (JSONObject) JSONObject.toJSON(credential.credentialSubject);
47 | credentialSubject.remove("id");
48 | if (credentialSubject.size() > 0) {
49 | this.credentialSubject = credentialSubject;
50 | }
51 | } else {
52 | this.credentialSubject = credential.credentialSubject;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/jwt/JWTVP.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.ontid.jwt;
2 |
3 | import com.alibaba.fastjson.JSONArray;
4 | import com.alibaba.fastjson.JSONObject;
5 | import com.alibaba.fastjson.annotation.JSONField;
6 | import com.alibaba.fastjson.annotation.JSONType;
7 | import com.github.ontio.ontid.Proof;
8 | import com.github.ontio.ontid.VerifiablePresentation;
9 | import com.github.ontio.sdk.exception.SDKException;
10 |
11 | @JSONType(orders = {"@context", "type", "challenge", "verifiableCredential", "holder", "proof"})
12 | public class JWTVP {
13 | @JSONField(name = "@context")
14 | public String[] context;
15 | public String[] type;
16 | public String[] verifiableCredential; // base64url encoded JWTVC as string
17 | public Object holder;
18 | public Proof proof;
19 |
20 | public JWTVP() {
21 | }
22 |
23 | public JWTVP(VerifiablePresentation presentation, Proof proof) throws Exception {
24 | if (presentation.holder.getClass().isPrimitive() || presentation.holder.getClass().isArray() ||
25 | presentation.holder instanceof JSONArray) {
26 | throw new SDKException("illegal presentation holder");
27 | }
28 | if (!(presentation.holder instanceof String)) {
29 | JSONObject jsonObject = (JSONObject) JSONObject.toJSON(presentation.holder);
30 | jsonObject.remove("id");
31 | if (jsonObject.size() > 0) {
32 | this.holder = jsonObject;
33 | }
34 | }
35 | this.context = presentation.context;
36 | this.type = presentation.type;
37 | if (presentation.verifiableCredential != null) {
38 | String[] verifiableCredential = new String[presentation.verifiableCredential.length];
39 | for (int i = 0; i < presentation.verifiableCredential.length; i++) {
40 | JWTCredential jwtCred = new JWTCredential(presentation.verifiableCredential[i]);
41 | verifiableCredential[i] = jwtCred.toString();
42 | }
43 | this.verifiableCredential = verifiableCredential;
44 | }
45 | this.proof = proof.genJWTProof();
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/ontid/roles.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-java-sdk/8a35a0823068bcb035f1229a290cc65acdd49263/src/main/java/com/github/ontio/ontid/roles.png
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sdk/exception/SDKException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.sdk.exception;
21 |
22 |
23 | import com.alibaba.fastjson.JSON;
24 |
25 | public class SDKException extends Exception {
26 |
27 | private static final long serialVersionUID = -3056715808373341597L;
28 |
29 | public SDKException(String message) {
30 | super(message);
31 | initExMsg(message);
32 | }
33 | public SDKException(String message, Throwable ex) {
34 | super(message, ex);
35 | initExMsg(message);
36 | }
37 | public SDKException(Throwable ex) {
38 | super(ex);
39 | }
40 |
41 | private void initExMsg(String message) {
42 | }
43 |
44 | }
45 |
46 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sdk/exception/SDKRuntimeException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.sdk.exception;
21 |
22 | import com.alibaba.fastjson.JSON;
23 |
24 | public class SDKRuntimeException extends RuntimeException {
25 |
26 | private static final long serialVersionUID = 2005335065357755315L;
27 | public SDKRuntimeException(String message) {
28 | super(message);
29 | initExMsg(message);
30 | }
31 |
32 | public SDKRuntimeException(String message, Throwable ex) {
33 | super(message, ex);
34 | initExMsg(message);
35 | }
36 |
37 | private void initExMsg(String message) {
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sdk/info/AccountInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.sdk.info;
21 |
22 | import com.alibaba.fastjson.JSON;
23 |
24 | /**
25 | *
26 | */
27 | public class AccountInfo {
28 | public String addressBase58;
29 | public String pubkey;
30 |
31 | public String encryptedPrikey;
32 | public String addressU160;
33 | private String prikey;
34 | private String prikeyWif;
35 |
36 | public void setPrikey(String prikey) {
37 | //this.prikey = prikey;
38 | }
39 |
40 | public void setPriwif(String priwif) {
41 | //this.prikeyWif = priwif;
42 | }
43 |
44 | public String getPrikeyWif() {
45 | return prikeyWif;
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return JSON.toJSONString(this);
51 | }
52 |
53 |
54 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sdk/info/IdentityInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.sdk.info;
21 |
22 | import com.alibaba.fastjson.JSON;
23 |
24 | /**
25 | *
26 | */
27 | public class IdentityInfo {
28 | public String ontid;
29 | public String pubkey;
30 |
31 | public String encryptedPrikey;
32 | public String addressU160;
33 | private String prikey;
34 | private String prikeyWif;
35 |
36 | public void setPrikey(String prikey) {
37 | //this.prikey = prikey;
38 | }
39 |
40 | public void setPriwif(String priwif) {
41 | //this.prikeyWif = priwif;
42 | }
43 |
44 | public String getPrikeyWif() {
45 | return prikeyWif;
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return JSON.toJSONString(this);
51 | }
52 |
53 |
54 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sdk/wallet/Identity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.sdk.wallet;
21 |
22 | import com.alibaba.fastjson.JSON;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | /**
28 | *
29 | */
30 | public class Identity {
31 | public String label = "";
32 | public String ontid = "";
33 | public boolean isDefault = false;
34 | public boolean lock = false;
35 | public List controls = new ArrayList();
36 | public Object extra = null;
37 |
38 | public Object getExtra(){
39 | return extra;
40 | }
41 | public void setExtra(Object extra){
42 | this.extra = extra;
43 | }
44 | @Override
45 | public String toString() {
46 | return JSON.toJSONString(this);
47 | }
48 | }
49 |
50 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sdk/wallet/Scrypt.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.sdk.wallet;
21 |
22 | import com.alibaba.fastjson.JSON;
23 |
24 | /**
25 | */
26 | public class Scrypt implements Cloneable{
27 | private int n = 16384;
28 | private int r = 8;
29 | private int p = 8;
30 | private int DkLen = 64;
31 | private String Salt;
32 |
33 | public int getDkLen() {
34 | return DkLen;
35 | }
36 |
37 | public void setDkLen(int dkLen) {
38 | DkLen = dkLen;
39 | }
40 |
41 |
42 | public Scrypt() {
43 | }
44 |
45 | public Scrypt(int n, int r, int p) {
46 | this.n = n;
47 | this.r = r;
48 | this.p = p;
49 | }
50 |
51 | public int getN() {
52 | return n;
53 | }
54 |
55 | public void setN(int n) {
56 | this.n = n;
57 | }
58 |
59 | public int getR() {
60 | return r;
61 | }
62 |
63 | public void setR(int r) {
64 | this.r = r;
65 | }
66 |
67 | public int getP() {
68 | return p;
69 | }
70 |
71 | public void setP(int p) {
72 | this.p = p;
73 | }
74 |
75 | @Override
76 | public Scrypt clone() {
77 | Scrypt o = null;
78 | try {
79 | o = (Scrypt) super.clone();
80 | } catch (CloneNotSupportedException e) {
81 | e.printStackTrace();
82 | }
83 | return o;
84 | }
85 | @Override
86 | public String toString() {
87 | return JSON.toJSONString(this);
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sidechain/SidechainVm.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.sidechain;
2 |
3 | import com.github.ontio.OntSdk;
4 | import com.github.ontio.sidechain.smartcontract.governance.Governance;
5 | import com.github.ontio.sidechain.smartcontract.ongx.OngX;
6 |
7 | public class SidechainVm {
8 | private Governance governance;
9 | private OngX ongX;
10 | private OntSdk sdk;
11 | public SidechainVm(OntSdk sdk){
12 | this.sdk = sdk;
13 | }
14 |
15 | public Governance governance() {
16 | if (governance == null){
17 | governance = new Governance(sdk);
18 | }
19 | return governance;
20 | }
21 |
22 | public OngX ongX() {
23 | if (ongX == null){
24 | ongX = new OngX(sdk);
25 | }
26 | return ongX;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/sidechain/smartcontract/ongx/Swap.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.sidechain.smartcontract.ongx;
2 |
3 | import com.github.ontio.common.Address;
4 |
5 | public class Swap {
6 | public Address address;
7 | public long value;
8 | public Swap(Address address, long value){
9 | this.address = address;
10 | this.value = value;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/smartcontract/nativevm/abi/AbiEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.smartcontract.nativevm.abi;
21 |
22 | import com.alibaba.fastjson.JSON;
23 | import com.github.ontio.common.ErrorCode;
24 | import com.github.ontio.sdk.exception.SDKException;
25 |
26 | import java.util.List;
27 |
28 | /**
29 | * smartcode abi event
30 | */
31 | public class AbiEvent {
32 | public String name;
33 | public String returntype;
34 | public List parameters;
35 |
36 | public String getName() {
37 | return name;
38 | }
39 |
40 | public List getParameters() {
41 | return parameters;
42 | }
43 | public void setParamsValue(Object... objs) throws Exception{
44 | if(objs.length != parameters.size()){
45 | throw new SDKException(ErrorCode.ParamError);
46 | }
47 | for (int i = 0; i < objs.length; i++) {
48 | parameters.get(i).setValue(objs[i]);
49 | }
50 | }
51 | public Parameter getParameter(String name) {
52 | for (Parameter e : parameters) {
53 | if (e.getName().equals(name)) {
54 | return e;
55 | }
56 | }
57 | return null;
58 | }
59 | public void clearParamsValue() {
60 | for (Parameter e : parameters) {
61 | e.setValue(null);
62 | }
63 | }
64 | @Override
65 | public String toString() {
66 | return JSON.toJSONString(this);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/smartcontract/nativevm/abi/AbiFunction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.smartcontract.nativevm.abi;
21 |
22 |
23 | import com.alibaba.fastjson.JSON;
24 | import com.github.ontio.common.ErrorCode;
25 | import com.github.ontio.sdk.exception.SDKException;
26 |
27 | import java.util.List;
28 |
29 | /**
30 | * smartcode abi function
31 | */
32 | public class AbiFunction {
33 | public String name;
34 | public String returntype;
35 | public List parameters;
36 |
37 | public String getName() {
38 | return name;
39 | }
40 |
41 | public List getParameters() {
42 | return parameters;
43 | }
44 | public void setParamsValue(Object... objs) throws Exception{
45 | if(objs.length != parameters.size()){
46 | throw new SDKException(ErrorCode.ParamError);
47 | }
48 | for (int i = 0; i < objs.length; i++) {
49 | parameters.get(i).setValue(objs[i]);
50 | }
51 | }
52 | public Parameter getParameter(String name) {
53 | for (Parameter e : parameters) {
54 | if (e.getName().equals(name)) {
55 | return e;
56 | }
57 | }
58 | return null;
59 | }
60 | public void clearParamsValue() {
61 | for (Parameter e : parameters) {
62 | e.setValue(null);
63 | }
64 | }
65 | @Override
66 | public String toString() {
67 | return JSON.toJSONString(this);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/smartcontract/nativevm/abi/Struct.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.smartcontract.nativevm.abi;
2 |
3 | import com.github.ontio.common.Address;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 |
9 | public class Struct {
10 | public List list = new ArrayList();
11 | public Struct(){
12 |
13 | }
14 | public Struct add(Object... objs){
15 | for(int i=0;i.
17 | *
18 | */
19 |
20 | package com.github.ontio.smartcontract.nativevm.abi;
21 |
22 | import com.alibaba.fastjson.JSON;
23 | import com.github.ontio.common.ErrorCode;
24 | import com.github.ontio.sdk.exception.SDKException;
25 |
26 | import java.util.List;
27 |
28 | public class SubType {
29 | public List parameters;
30 |
31 | public List getParameters() {
32 | return parameters;
33 | }
34 | public void setParamsValue(Object... objs) throws Exception{
35 | if(objs.length != parameters.size()){
36 | throw new SDKException(ErrorCode.ParamError);
37 | }
38 | for (int i = 0; i < objs.length; i++) {
39 | parameters.get(i).setValue(objs[i]);
40 | }
41 | }
42 | public Parameter getParameter(String name) {
43 | for (Parameter e : parameters) {
44 | if (e.getName().equals(name)) {
45 | return e;
46 | }
47 | }
48 | return null;
49 | }
50 | public void clearParamsValue() {
51 | for (Parameter e : parameters) {
52 | e.setValue(null);
53 | }
54 | }
55 | @Override
56 | public String toString() {
57 | return JSON.toJSONString(this);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/smartcontract/neovm/abi/AbiEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.smartcontract.neovm.abi;
21 |
22 | import com.alibaba.fastjson.JSON;
23 | import com.github.ontio.common.ErrorCode;
24 | import com.github.ontio.sdk.exception.SDKException;
25 | import java.util.List;
26 |
27 | /**
28 | * smartcode abi event
29 | */
30 | public class AbiEvent {
31 | public String name;
32 | public String returntype;
33 | public List parameters;
34 |
35 | public String getName() {
36 | return name;
37 | }
38 |
39 | public List getParameters() {
40 | return parameters;
41 | }
42 | public void setParamsValue(Object... objs) throws Exception{
43 | if(objs.length != parameters.size()){
44 | throw new SDKException(ErrorCode.ParamError);
45 | }
46 | for (int i = 0; i < objs.length; i++) {
47 | parameters.get(i).setValue(objs[i]);
48 | }
49 | }
50 | public Parameter getParameter(String name) {
51 | for (Parameter e : parameters) {
52 | if (e.getName().equals(name)) {
53 | return e;
54 | }
55 | }
56 | return null;
57 | }
58 | public void clearParamsValue() {
59 | for (Parameter e : parameters) {
60 | e.setValue(null);
61 | }
62 | }
63 | @Override
64 | public String toString() {
65 | return JSON.toJSONString(this);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ontio/smartcontract/neovm/abi/Struct.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.smartcontract.neovm.abi;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 |
7 | public class Struct {
8 | public List list = new ArrayList();
9 | public Struct(){
10 |
11 | }
12 | public Struct add(Object... objs){
13 | for(int i=0;i States;
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/demo/ledger/p2p/BlkHeader.java:
--------------------------------------------------------------------------------
1 | package demo.ledger.p2p;
2 |
3 | import com.github.ontio.common.Helper;
4 | import com.github.ontio.io.BinaryReader;
5 | import demo.ledger.common.BlockHeader;
6 |
7 | import java.io.ByteArrayInputStream;
8 | import java.io.IOException;
9 |
10 | /**
11 | *
12 | *
13 | */
14 | public class BlkHeader {
15 | public BlockHeader[] headers;
16 | public BlkHeader(){
17 |
18 | }
19 | public void deserialization(byte[] data){
20 | ByteArrayInputStream ms = new ByteArrayInputStream(data);
21 | BinaryReader reader = new BinaryReader(ms);
22 | try {
23 | int count = reader.readInt();
24 | headers = new BlockHeader[count];
25 | for(int i=0;i map = new HashMap<>();
12 |
13 | public MapItem() {
14 | }
15 |
16 | public void Add(StackItems key, StackItems value) {
17 | map.put(key, value);
18 | }
19 |
20 | public void Clear() {
21 | map.clear();
22 | }
23 |
24 | public boolean ContainsKey(StackItems item) {
25 | return map.get(item) != null;
26 | }
27 |
28 | public void Remove(StackItems item) {
29 | map.remove(item);
30 | }
31 |
32 | @Override
33 | public boolean Equals(StackItems item) {
34 | return this.equals(item);
35 | }
36 |
37 | @Override
38 | public BigInteger GetBigInteger() {
39 | return null;
40 | }
41 |
42 | @Override
43 | public boolean GetBoolean() {
44 | return true;
45 | }
46 |
47 | @Override
48 | public byte[] GetByteArray() {
49 | return null;
50 | }
51 |
52 | @Override
53 | public InteropItem GetInterface() {
54 | return null;
55 | }
56 |
57 | @Override
58 | public StackItems[] GetArray() {
59 | return null;
60 | }
61 |
62 | @Override
63 | public StackItems[] GetStruct() {
64 | return null;
65 | }
66 |
67 | @Override
68 | public Map GetMap() {
69 | return map;
70 | }
71 |
72 | public StackItems TryGetValue(StackItems key) {
73 | for (Map.Entry e : map.entrySet()) {
74 | if (e.getKey() instanceof ByteArrayItem) {
75 | if (key instanceof ByteArrayItem) {
76 | if (Helper.toHexString(e.getKey().GetByteArray()).equals(Helper.toHexString(key.GetByteArray()))) {
77 | return e.getValue();
78 | }
79 | } else if (key instanceof IntegerItem) {
80 | if (e.getKey().GetBigInteger().compareTo(key.GetBigInteger()) > 0) {
81 | return e.getValue();
82 | }
83 | }
84 |
85 | }
86 | }
87 | return null;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/main/java/demo/vmtest/types/StackItems.java:
--------------------------------------------------------------------------------
1 | package demo.vmtest.types;
2 |
3 | import java.math.BigInteger;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 |
7 | public class StackItems {
8 | public boolean Equals(StackItems other) {
9 | return false;
10 | }
11 |
12 | public BigInteger GetBigInteger() {
13 | return new BigInteger("");
14 | }
15 |
16 | public boolean GetBoolean() {
17 | return false;
18 | }
19 |
20 | public byte[] GetByteArray() {
21 | return new byte[]{};
22 | }
23 |
24 | public InteropItem GetInterface() {
25 | return null;
26 | }
27 |
28 | public StackItems[] GetArray() {
29 | return new StackItems[0];
30 | }
31 |
32 | public StackItems[] GetStruct() {
33 | return new StackItems[0];
34 | }
35 |
36 | public Map GetMap() {
37 | return new HashMap<>();
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/java/demo/vmtest/types/StructItem.java:
--------------------------------------------------------------------------------
1 | package demo.vmtest.types;
2 |
3 | import java.math.BigInteger;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | public class StructItem extends StackItems {
9 | public List stackItems = new ArrayList<>();
10 |
11 | public StructItem(List stackItems) {
12 | this.stackItems = stackItems;
13 | }
14 |
15 | @Override
16 | public boolean Equals(StackItems item) {
17 | return this.equals(item);
18 | }
19 |
20 | @Override
21 | public BigInteger GetBigInteger() {
22 | return null;
23 | }
24 |
25 | @Override
26 | public boolean GetBoolean() {
27 | return true;
28 | }
29 |
30 | @Override
31 | public byte[] GetByteArray() {
32 | return null;
33 | }
34 |
35 | @Override
36 | public InteropItem GetInterface() {
37 | return null;
38 | }
39 |
40 | @Override
41 | public StackItems[] GetArray() {
42 | return null;
43 | }
44 |
45 | @Override
46 | public StackItems[] GetStruct() {
47 | return stackItems.toArray(new StackItems[stackItems.size()]);
48 | }
49 |
50 | @Override
51 | public Map GetMap() {
52 | return null;
53 | }
54 |
55 | public void Add(StackItems items) {
56 | stackItems.add(items);
57 | }
58 |
59 | public int Count() {
60 | return stackItems.size();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/demo/vmtest/utils/Config.java:
--------------------------------------------------------------------------------
1 | package demo.vmtest.utils;
2 |
3 | import com.github.ontio.common.Address;
4 | import com.github.ontio.core.payload.InvokeCode;
5 | import com.github.ontio.core.transaction.Transaction;
6 |
7 | import java.util.ArrayList;
8 | import java.util.HashMap;
9 | import java.util.List;
10 | import java.util.Map;
11 |
12 | public class Config {
13 | public String ContractAddress = "ContractAddress";
14 | public Map storageMap = new HashMap<>();
15 | public Transaction tx = new InvokeCode();
16 |
17 | public Map getStorageMap() {
18 | return storageMap;
19 | }
20 |
21 | public List GetSignatureAddresses() {
22 | if (tx.sigs == null) {
23 | return null;
24 | }
25 | List list = new ArrayList();
26 | for (int i = 0; i < tx.sigs.length; i++) {
27 | for (int j = 0; j < tx.sigs[i].pubKeys.length; j++) {
28 | if (tx.sigs[i].M == 1) {
29 | Address address = Address.addressFromPubKey(tx.sigs[i].pubKeys[0]);
30 | list.add(address);
31 | }
32 | }
33 | }
34 | return list;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/demo/vmtest/vm/ExecutionContext.java:
--------------------------------------------------------------------------------
1 | package demo.vmtest.vm;
2 |
3 | import com.github.ontio.core.scripts.ScriptOp;
4 | import demo.vmtest.utils.VmReader;
5 |
6 | public class ExecutionContext {
7 | public byte[] Code;
8 | public VmReader OpReader;
9 | public int InstructionPointer;
10 | public ExecutionEngine engine;
11 |
12 | public ExecutionContext(ExecutionEngine engine, byte[] code) {
13 | this.engine = engine;
14 | Code = code;
15 | OpReader = new VmReader(code);
16 | InstructionPointer = 0;
17 | }
18 |
19 | public int GetInstructionPointer() {
20 | return OpReader.Position();
21 | }
22 |
23 | public long SetInstructionPointer(long offset) {
24 | return OpReader.Seek(offset);
25 | }
26 |
27 | public ScriptOp NextInstruction() {
28 | return ScriptOp.valueOf(Code[OpReader.Position()]);
29 | }
30 |
31 | public ExecutionContext Clone() {
32 | ExecutionContext executionContext = new ExecutionContext(engine, Code);
33 | executionContext.InstructionPointer = this.InstructionPointer;
34 | executionContext.SetInstructionPointer(this.GetInstructionPointer());
35 | return executionContext;
36 | }
37 | }
--------------------------------------------------------------------------------
/src/main/java/demo/vmtest/vm/OpExec.java:
--------------------------------------------------------------------------------
1 | package demo.vmtest.vm;
2 |
3 | import com.github.ontio.core.scripts.ScriptOp;
4 | import demo.vmtest.utils.PushData;
5 |
6 | import java.lang.reflect.InvocationTargetException;
7 | import java.lang.reflect.Method;
8 |
9 | public class OpExec {
10 | public ScriptOp Opcode;
11 | public String Name;
12 | public Method ExecFunc;
13 | public Method ValidatorFunc;
14 |
15 | public OpExec(ScriptOp opcode, String name, Method execMethod, Method validatorMethod) throws Exception {
16 | Opcode = opcode;
17 | Name = name;
18 | ExecFunc = execMethod;
19 | ValidatorFunc = validatorMethod;
20 | }
21 |
22 | public VMState Exec(ExecutionEngine engine) {
23 | try {
24 | ExecFunc.invoke(PushData.class.newInstance(), engine);
25 | } catch (IllegalAccessException e) {
26 | e.printStackTrace();
27 | System.exit(0);
28 | } catch (InvocationTargetException e) {
29 | e.printStackTrace();
30 | System.exit(0);
31 | } catch (InstantiationException e) {
32 | e.printStackTrace();
33 | System.exit(0);
34 | }
35 | return VMState.NONE;
36 | }
37 |
38 | boolean Validator(ExecutionEngine engine) {
39 | try {
40 | if (ValidatorFunc == null) {
41 | return true;
42 | }
43 | ValidatorFunc.invoke(OpExecList.class.newInstance(), engine);
44 | } catch (IllegalAccessException e) {
45 | e.printStackTrace();
46 | System.exit(0);
47 | } catch (InvocationTargetException e) {
48 | e.printStackTrace();
49 | System.exit(0);
50 | } catch (InstantiationException e) {
51 | e.printStackTrace();
52 | System.exit(0);
53 | }
54 | return false;
55 | }
56 |
57 | }
--------------------------------------------------------------------------------
/src/main/java/demo/vmtest/vm/VMState.java:
--------------------------------------------------------------------------------
1 | package demo.vmtest.vm;
2 |
3 | import com.github.ontio.common.ErrorCode;
4 |
5 | public enum VMState {
6 | NONE(0x00),
7 | HALT(0x01),
8 | FAULT(0x02),
9 | BREAK(0x04),
10 | INSUFFICIENT_RESOURCE(0x10);
11 | public int value;
12 |
13 | private VMState(int b) {
14 | this.value = b;
15 | }
16 |
17 | public int getValue() {
18 | return value;
19 | }
20 |
21 | public static VMState valueOf(int b) throws Exception {
22 | for (VMState k : VMState.values()) {
23 | if (k.value == b) {
24 | return k;
25 | }
26 | }
27 | throw new Exception(ErrorCode.ParamError);
28 | }
29 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/OntSdkTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio;
2 |
3 | import org.junit.After;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class OntSdkTest {
10 | private OntSdk ontSdk;
11 | // public static String URL = "http://polaris1.ont.io:20334";
12 | // public static String URL = "http://139.219.129.26:20334";
13 | public static String URL = "http://127.0.0.1:20334";
14 |
15 | public static String PRIVATEKEY3 = "c19f16785b8f3543bbaf5e1dbb5d398dfa6c85aaad54fc9d71203ce83e505c07";//有钱的账号的私钥
16 | public static String PRIVATEKEY2 = "f1442d5e7f4e2061ff9a6884d6d05212e2aa0f6a6284f0a28ae82a29cdb3d656";//有钱的账号的私钥
17 | public static String PRIVATEKEY = "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf";
18 |
19 | public static String PASSWORD = "111111";//有钱账号的密码
20 |
21 | @Before
22 | public void setUp() throws Exception {
23 | ontSdk = OntSdk.getInstance();
24 | }
25 |
26 | @After
27 | public void tearDown() throws Exception {
28 | }
29 |
30 | @Test
31 | public void getInstance() {
32 | OntSdk ontSdk = OntSdk.getInstance();
33 | assertNotNull(ontSdk);
34 | assertSame(ontSdk,this.ontSdk);
35 | }
36 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/account/AccountTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.account;
2 |
3 | import com.github.ontio.common.Helper;
4 | import com.github.ontio.crypto.SignatureScheme;
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class AccountTest {
10 |
11 |
12 | @Test
13 | public void generateSignature() throws Exception {
14 | Account account = new Account(SignatureScheme.SHA256WITHECDSA);
15 | byte[] signature = account.generateSignature("hello".getBytes(),SignatureScheme.SHA256WITHECDSA,null);
16 | boolean b = account.verifySignature("hello".getBytes(),signature);
17 | assertTrue(b);
18 | }
19 |
20 | @Test
21 | public void serializePublicKey() throws Exception {
22 | Account account = new Account(SignatureScheme.SHA256WITHECDSA);
23 | byte[] publickey = account.serializePublicKey();
24 | assertNotNull(publickey);
25 | }
26 |
27 | @Test
28 | public void serializePrivateKey() throws Exception {
29 | Account account = new Account(SignatureScheme.SHA256WITHECDSA);
30 | byte[] privateKey = account.serializePrivateKey();
31 | assertNotNull(privateKey);
32 | }
33 |
34 | @Test
35 | public void compareTo() throws Exception {
36 | Account account1 = new Account(SignatureScheme.SHA256WITHECDSA);
37 | Account account2 = new Account(SignatureScheme.SHA256WITHECDSA);
38 | int res = account1.compareTo(account2);
39 | assertNotNull(res);
40 | }
41 |
42 | @Test
43 | public void exportCtrEncryptedPrikey1() throws Exception {
44 | Account account = new Account(SignatureScheme.SHA256WITHECDSA);
45 | String encruPri = account.exportCtrEncryptedPrikey("111111",16384);
46 | String privateKey = Account.getCtrDecodedPrivateKey(encruPri,"111111",account.getAddressU160().toBase58(),16384,SignatureScheme.SHA256WITHECDSA);
47 | assertEquals(privateKey,Helper.toHexString(account.serializePrivateKey()));
48 | }
49 |
50 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/common/AddressTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.common;
2 |
3 | import com.github.ontio.account.Account;
4 | import com.github.ontio.crypto.SignatureScheme;
5 | import com.github.ontio.sdk.exception.SDKException;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 |
9 | import static org.junit.Assert.*;
10 |
11 | public class AddressTest {
12 | Account account;
13 | @Before
14 | public void setUp() throws Exception {
15 | account = new Account(SignatureScheme.SHA256WITHECDSA);
16 | }
17 |
18 | @Test
19 | public void compareTo() throws Exception {
20 | Account account2 = new Account(SignatureScheme.SHA256WITHECDSA);
21 | int res = account2.getAddressU160().compareTo(account.getAddressU160());
22 | assertNotNull(res);
23 | }
24 |
25 | @Test
26 | public void parse() {
27 | // Address address = Address.parse(account.getAddressU160().toHexString());
28 | // assertEquals(address,account.getAddressU160());
29 | }
30 |
31 | @Test
32 | public void addressFromPubKey() {
33 | Address address = Address.addressFromPubKey(account.serializePublicKey());
34 | assertEquals(address,account.getAddressU160());
35 | }
36 |
37 | @Test
38 | public void addressFromPubKey1() {
39 | Address address = Address.addressFromPubKey(Helper.toHexString(account.serializePublicKey()));
40 | assertEquals(address,account.getAddressU160());
41 | }
42 |
43 | @Test
44 | public void addressFromMultiPubKeys() throws Exception {
45 | Account account2 = new Account(SignatureScheme.SHA256WITHECDSA);
46 | Address res = Address.addressFromMultiPubKeys(2,account.serializePublicKey(),account2.serializePublicKey());
47 | assertNotNull(res);
48 | }
49 |
50 | @Test
51 | public void toBase58() throws SDKException {
52 | String res = account.getAddressU160().toBase58();
53 | Address addr = Address.decodeBase58(res);
54 | assertEquals(addr,account.getAddressU160());
55 | }
56 |
57 | @Test
58 | public void toScriptHash() {
59 | Address addr = Address.toScriptHash(Helper.hexToBytes("12a67b"));
60 | assertNotNull(addr);
61 | }
62 |
63 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/VmTypeTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.core;
21 |
22 | import org.junit.Assert;
23 | import org.junit.Test;
24 |
25 | public class VmTypeTest {
26 |
27 | @Test
28 | public void valueOf() throws IllegalArgumentException {
29 | Assert.assertEquals(VmType.NEOVM, VmType.valueOf((byte) 0x01));
30 | Assert.assertEquals(VmType.WASMVM, VmType.valueOf((byte) 0x03));
31 | }
32 |
33 | @Test
34 | public void value() {
35 | Assert.assertEquals(1, VmType.NEOVM.value());
36 | Assert.assertEquals(3, VmType.WASMVM.value());
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/asset/ContractTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.asset;
2 |
3 | import com.github.ontio.common.Address;
4 | import com.github.ontio.io.BinaryReader;
5 | import com.github.ontio.io.BinaryWriter;
6 | import com.github.ontio.sdk.exception.SDKException;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 |
10 | import java.io.ByteArrayInputStream;
11 | import java.io.ByteArrayOutputStream;
12 | import java.io.DataInputStream;
13 | import java.io.IOException;
14 |
15 | import static org.junit.Assert.*;
16 |
17 | public class ContractTest {
18 |
19 | Address address;
20 |
21 | @Before
22 | public void setUp() throws SDKException {
23 | // address = Address.decodeBase58("TA6nRD9DqGkE8xRJaB37bW2KQEz59ovKRH");
24 | }
25 |
26 | @Test
27 | public void serialize() throws IOException {
28 | // Contract contract = new Contract((byte)1,address,"test","t".getBytes());
29 | // ByteArrayOutputStream bs = new ByteArrayOutputStream();
30 | // BinaryWriter binaryWriter = new BinaryWriter(bs);
31 | // contract.serialize(binaryWriter);
32 | // binaryWriter.flush();
33 | // byte[] seril = bs.toByteArray();
34 | // assertNotNull(seril);
35 | //
36 | // Contract contract1 = new Contract((byte)1,address,"test2","t2".getBytes());
37 | // ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(seril);
38 | // BinaryReader binaryReader = new BinaryReader(byteArrayInputStream);
39 | // contract1.deserialize(binaryReader);
40 | // assertNotNull(binaryReader);
41 |
42 | }
43 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/asset/StateTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.asset;
2 |
3 | import com.github.ontio.common.Address;
4 | import com.github.ontio.common.Helper;
5 | import com.github.ontio.io.BinaryReader;
6 | import com.github.ontio.io.BinaryWriter;
7 | import com.github.ontio.sdk.exception.SDKException;
8 | import org.junit.Test;
9 |
10 | import java.io.ByteArrayInputStream;
11 | import java.io.ByteArrayOutputStream;
12 | import java.io.IOException;
13 |
14 | import static org.junit.Assert.*;
15 |
16 | public class StateTest {
17 | @Test
18 | public void deserialize() throws SDKException, IOException {
19 | // State state = new State(Address.decodeBase58("TRj9g9kvq8pL3L8M1F2KpsqWQHiC7u4xLP"),Address.decodeBase58("TRj9g9kvq8pL3L8M1F2KpsqWQHiC7u4xLP"),1000L);
20 | // ByteArrayOutputStream bais = new ByteArrayOutputStream();
21 | // BinaryWriter bw = new BinaryWriter(bais);
22 | // Transfers transfers = new Transfers(new State[]{state});
23 | // System.out.println(Helper.toHexString(transfers.toArray()));
24 | //
25 | //
26 | // state.serialize(bw);
27 | //
28 | // State state1 = new State();
29 | // ByteArrayInputStream baos = new ByteArrayInputStream(bais.toByteArray());
30 | // BinaryReader br = new BinaryReader(baos);
31 | // state1.deserialize(br);
32 | }
33 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/block/BlockTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.block;
2 |
3 | import com.github.ontio.common.Address;
4 | import com.github.ontio.common.Helper;
5 | import com.github.ontio.common.UInt256;
6 | import com.github.ontio.core.transaction.Transaction;
7 | import com.github.ontio.io.BinaryReader;
8 | import com.github.ontio.io.BinaryWriter;
9 | import com.github.ontio.sdk.exception.SDKException;
10 | import org.junit.Test;
11 |
12 | import java.io.ByteArrayInputStream;
13 | import java.io.ByteArrayOutputStream;
14 | import java.io.IOException;
15 |
16 | import static org.junit.Assert.*;
17 |
18 | public class BlockTest {
19 |
20 | @Test
21 | public void deserialize() {
22 | }
23 |
24 |
25 | @Test
26 | public void serialize() throws IOException, SDKException {
27 | // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
28 | // BinaryWriter binaryWriter = new BinaryWriter(byteArrayOutputStream);
29 | // Block block = new Block();
30 | // block.version = 1;
31 | // block.height = 1;
32 | // block.consensusPayload = "test".getBytes();
33 | // block.prevBlockHash = new UInt256(Helper.hexToBytes("1d46ec977e10d297a53d77dfcb5fe5904734f2c62e156d0e893d1b7c050524a2"));
34 | // block.blockRoot = new UInt256(Helper.hexToBytes("37614956f598e0b3c4c3105d9e94c8cf4aa0ac6adce4eff4189dd348d1f3dac2"));
35 | // block.consensusData = 111;
36 | // block.nextBookkeeper = Address.decodeBase58("TAERF2G54oJxN4U52CVns7kTYAMFxAFsGg");
37 | // block.bookkeepers = new byte[][]{"test".getBytes()};
38 | // block.transactionsRoot = new UInt256(Helper.hexToBytes("b5b8cb62d5c1ccea510ce1c268259fab33069c343532c804743bd4c6029dbd35"));
39 | // block.sigData = new String[]{"123ab2"};
40 | // block.transactions = new Transaction[]{};
41 | // block.serialize(binaryWriter);
42 | // binaryWriter.flush();
43 | // byte[] seril = byteArrayOutputStream.toByteArray();
44 | // Block block1 = new Block();
45 | //
46 | // ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(seril);
47 | // block1.deserializeUnsigned(new BinaryReader(byteArrayInputStream));
48 | // assertEquals(block,block1);
49 | }
50 |
51 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/payload/DeployCodeTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.payload;
2 |
3 | import com.github.ontio.core.VmType;
4 | import com.github.ontio.io.BinaryReader;
5 | import com.github.ontio.io.BinaryWriter;
6 | import org.junit.Test;
7 |
8 | import java.io.ByteArrayInputStream;
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.DataOutputStream;
11 | import java.io.IOException;
12 |
13 | import static org.junit.Assert.*;
14 |
15 | public class DeployCodeTest {
16 |
17 | @Test
18 | public void serializeExclusiveData() throws IOException {
19 | DeployCode deployCode = new DeployCode();
20 | deployCode.version = "1";
21 | deployCode.author = "sss";
22 | deployCode.name = "sss";
23 | deployCode.code = "test".getBytes();
24 | deployCode.description = "test";
25 | deployCode.email = "test";
26 | deployCode.needStorage = true;
27 |
28 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
29 | BinaryWriter binaryWriter = new BinaryWriter(byteArrayOutputStream);
30 | deployCode.serializeExclusiveData(binaryWriter);
31 |
32 | byte[] selr = byteArrayOutputStream.toByteArray();
33 |
34 | DeployCode deployCode1 = new DeployCode();
35 | deployCode1.deserializeExclusiveData(new BinaryReader(new ByteArrayInputStream(selr)));
36 | assertEquals(deployCode.version,deployCode1.version);
37 | }
38 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/payload/InvokeCodeTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.payload;
2 |
3 | import com.github.ontio.core.VmType;
4 | import com.github.ontio.io.BinaryReader;
5 | import com.github.ontio.io.BinaryWriter;
6 | import org.junit.Test;
7 |
8 | import java.io.ByteArrayInputStream;
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.IOException;
11 |
12 | import static org.junit.Assert.*;
13 |
14 | public class InvokeCodeTest {
15 |
16 | @Test
17 | public void serializeExclusiveData() throws IOException {
18 | InvokeCode invokeCode = new InvokeCode();
19 | invokeCode.code = "test".getBytes();
20 |
21 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
22 | BinaryWriter binaryWriter = new BinaryWriter(byteArrayOutputStream);
23 | invokeCode.serializeExclusiveData(binaryWriter);
24 |
25 | assertNotNull(byteArrayOutputStream);
26 |
27 | InvokeCode invokeCode1 = new InvokeCode();
28 | invokeCode1.deserializeExclusiveData(new BinaryReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
29 | }
30 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/payload/InvokeWasmCodeTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.payload;
2 |
3 | import com.github.ontio.common.Helper;
4 | import com.github.ontio.core.transaction.Transaction;
5 | import org.junit.Test;
6 |
7 | import java.io.IOException;
8 | public class InvokeWasmCodeTest {
9 | @Test
10 | public void deserialize() throws IOException {
11 | byte[] bs = Helper.hexToBytes("00d2757ecf5ef40100000000000000127a0000000000c849169e39c9cfac6d4a82a3b21e0eb8c566f6d7fda2025e0f5b1dcea427947f4e8f4824fb73908d5041b2fd8b020962617463685f61646414288fb05ca7a85bf4172f288a9f5f35ee0ca5abbe6e10117ae814cd20be059d8ac4d14c8fa03483fbd7e5b98d84cd8b6fda5d19ba09c5d212845fe67d0a5242a017d33f25f6e68ed510580a5938c8322792acf1040c143acb0f659c97f2220b5155f330f2770d2fb3b34cffea292ef8f2e1d72296a8fb0f58f18d215b5cda953646f24e54d749389e41b3774a7f06848c22804ffea65863c77622e8f13c3dc0feef8736a191fd1170b218a973a3745bf7e8e1021980a1f8cd2b37969d645cc894924a5c93c86896f2a68aae36a80e133fd0edebcf455ac21465ecea12557845823fab61b6c047a9bb4fa881ef20c44992666b5eb9245296b05a18a0c8f2d4b7433e90c34c2435c631814bcacb3cafc6ba7ba94409a2f257becd3549aa1e2fe441b28efae906a665c13c0753d09a2adeebb28b943dd971bebbaa5c585d7b83c617e5f61e4d6a00f2dd97e7991916a869ed9d148369a99dd73d648ef6e7793ea39615247ae2cdf5721f902f70df6d96c345ae8dba7e6c52aa1a5d367fd6c57412eb065fb8126bfdc65519bf2a49c5bb685cd318bccb3f05ef58e2ac2bdb0dc58f8079cfd710d6bf3aed6b9f3594695609245556c468da6a9db033856096cfdcb561a16f9954b40372c257e736118b94fd482df0ab65cbcbf309cc8e168789eedfbfe8fd722097f944e9928ae217e5af9734c7f8457117c6b8ee300dd772533fa5f94511dc34914100f8fa953246915f3a98c69e911b522e9adf9ac26295209aeb0efa831acfc21885a5480a0b71542167038871bc1851e506fdd1aca85b3a5d717606d7b479a2781b137cf1cf8cff9d45365015111d562dbcc975ad3f1edf4dce0b80ff8787d2915db41de8e86442485d8c4279711e5e8837d4ff76cf76430d503600014140fdb08e2548222f01cff1226fbf562efa5e88af988877e6a26494e6ba6606637c73802af2eb4b5c1b76ddeec88961d5aa31693ffafb6a2300c9b770f762f17a11232102795dd24e035bd3072708920e833654ccd672d4f646358939778b279cf84e6abaac");
12 | Transaction tx = Transaction.deserializeFrom(bs);
13 | System.out.println(tx.txType);
14 | System.out.println((InvokeWasmCode)tx);
15 | }
16 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/scripts/ScriptBuilderTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.scripts;
2 |
3 | import org.junit.Before;
4 | import org.junit.Test;
5 |
6 | import java.math.BigInteger;
7 |
8 | import static org.junit.Assert.*;
9 |
10 | public class ScriptBuilderTest {
11 |
12 | ScriptBuilder scriptBuilder;
13 |
14 | @Before
15 | public void setUp(){
16 | scriptBuilder = new ScriptBuilder();
17 | }
18 |
19 | @Test
20 | public void add() {
21 | ScriptBuilder sb = scriptBuilder.add("test".getBytes());
22 | assertNotNull(sb);
23 |
24 | }
25 |
26 | @Test
27 | public void push() {
28 | ScriptBuilder sb = scriptBuilder.emitPushBool(true);
29 | assertNotNull(sb);
30 | assertNotNull(scriptBuilder.emitPushByteArray("test".getBytes()));
31 | assertNotNull(scriptBuilder.emitPushInteger(new BigInteger("11")));
32 | }
33 |
34 |
35 | @Test
36 | public void pushPack() {
37 | assertNotNull(scriptBuilder.pushPack());
38 |
39 | }
40 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/core/transaction/TransactionTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.core.transaction;
2 |
3 | import com.github.ontio.OntSdk;
4 | import com.github.ontio.account.Account;
5 | import com.github.ontio.common.Address;
6 | import com.github.ontio.common.Helper;
7 | import com.github.ontio.crypto.SignatureScheme;
8 | import com.github.ontio.sdk.exception.SDKException;
9 | import com.github.ontio.smartcontract.Vm;
10 | import org.junit.Before;
11 | import org.junit.Test;
12 |
13 | import java.io.IOException;
14 |
15 | import static org.junit.Assert.*;
16 |
17 | public class TransactionTest {
18 |
19 | OntSdk ontSdk;
20 | Vm vm;
21 | String ontContract = "ff00000000000000000000000000000000000001";
22 |
23 | @Before
24 | public void setUp(){
25 | ontSdk = OntSdk.getInstance();
26 | vm = new Vm(ontSdk);
27 | }
28 |
29 | @Test
30 | public void serialize() throws Exception {
31 | Transaction tx = vm.buildNativeParams(Address.parse(ontContract),"init","1".getBytes(),null,0,0);
32 | Account account = new Account(Helper.hexToBytes("0bc8c1f75a028672cd42c221bf81709dfc7abbbaf0d87cb6fdeaf9a20492c194"),SignatureScheme.SHA256WITHECDSA);
33 | ontSdk.signTx(tx,new Account[][]{{account}});
34 |
35 | String t = tx.toHexString();
36 | System.out.println(t);
37 |
38 | Transaction tx2 = Transaction.deserializeFrom(Helper.hexToBytes(t));
39 | System.out.println(tx2.json());
40 |
41 |
42 | }
43 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/crypto/MnemonicTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018-2019 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.crypto;
21 |
22 | import io.github.novacrypto.bip39.Words;
23 | import org.junit.Assert;
24 | import org.junit.Test;
25 |
26 | public class MnemonicTest {
27 | @Test
28 | public void TestMnemonic() {
29 | Assert.assertEquals(12, MnemonicCode.generateMnemonicCodesStr().split(" ").length);
30 | Assert.assertEquals(12, MnemonicCode.generateMnemonicCodesStr(Words.TWELVE).split(" ").length);
31 | Assert.assertEquals(15, MnemonicCode.generateMnemonicCodesStr(Words.FIFTEEN).split(" ").length);
32 | Assert.assertEquals(18, MnemonicCode.generateMnemonicCodesStr(Words.EIGHTEEN).split(" ").length);
33 | Assert.assertEquals(21, MnemonicCode.generateMnemonicCodesStr(Words.TWENTY_ONE).split(" ").length);
34 | Assert.assertEquals(24, MnemonicCode.generateMnemonicCodesStr(Words.TWENTY_FOUR).split(" ").length);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/io/BinaryWriterTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.io;
2 |
3 | import com.github.ontio.common.Address;
4 | import com.github.ontio.common.Helper;
5 | import com.github.ontio.sdk.exception.SDKException;
6 | import org.junit.Test;
7 |
8 | import java.io.ByteArrayOutputStream;
9 | import java.io.IOException;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | public class BinaryWriterTest {
14 |
15 | @Test
16 | public void writeVarInt() throws IOException {
17 | ByteArrayOutputStream ms = new ByteArrayOutputStream();
18 | BinaryWriter binaryWriter = new BinaryWriter(ms);
19 | binaryWriter.writeVarInt((long) 2544);
20 | binaryWriter.flush();
21 | assertNotNull(ms);
22 | }
23 |
24 | @Test
25 | public void write() throws IOException {
26 | ByteArrayOutputStream ms = new ByteArrayOutputStream();
27 | BinaryWriter binaryWriter = new BinaryWriter(ms);
28 | binaryWriter.write("test".getBytes());
29 | binaryWriter.flush();
30 | assertNotNull(ms);
31 | }
32 |
33 | @Test
34 | public void writeInt() throws IOException {
35 | ByteArrayOutputStream ms = new ByteArrayOutputStream();
36 | BinaryWriter binaryWriter = new BinaryWriter(ms);
37 | binaryWriter.writeInt(1);
38 | binaryWriter.flush();
39 | assertNotNull(ms);
40 | }
41 |
42 | @Test
43 | public void writeSerializable() throws IOException, SDKException {
44 | // ByteArrayOutputStream ms = new ByteArrayOutputStream();
45 | // BinaryWriter binaryWriter = new BinaryWriter(ms);
46 | // Address address = Address.decodeBase58("TA6nRD9DqGkE8xRJaB37bW2KQEz59ovKRH");
47 | // binaryWriter.writeSerializable(address);
48 | // binaryWriter.flush();
49 | // assertNotNull(ms);
50 | }
51 |
52 | @Test
53 | public void writeVarBytes() throws IOException {
54 | ByteArrayOutputStream ms = new ByteArrayOutputStream();
55 | BinaryWriter binaryWriter = new BinaryWriter(ms);
56 | binaryWriter.writeVarBytes("test".getBytes());
57 | binaryWriter.flush();
58 | assertNotNull(ms);
59 | }
60 |
61 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/sdk/wallet/WalletTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.sdk.wallet;
2 |
3 | import com.github.ontio.OntSdk;
4 | import org.junit.After;
5 | import org.junit.Assert;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 |
9 | import java.io.File;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | public class WalletTest {
14 |
15 | OntSdk ontSdk;
16 | Identity id1;
17 | Identity id2;
18 | Account acct1;
19 | Account acct2;
20 |
21 | String walletFile = "WalletTest.json";
22 |
23 | @Before
24 | public void setUp() throws Exception {
25 | ontSdk = OntSdk.getInstance();
26 | ontSdk.openWalletFile(walletFile);
27 |
28 |
29 | id1 = ontSdk.getWalletMgr().createIdentity("passwordtest");
30 | id2 = ontSdk.getWalletMgr().createIdentity("passwordtest");
31 |
32 | acct1 = ontSdk.getWalletMgr().createAccount("passwordtest");
33 | acct2 = ontSdk.getWalletMgr().createAccount("passwordtest");
34 | }
35 |
36 | @After
37 | public void removeWallet(){
38 | File file = new File(walletFile);
39 | if(file.exists()){
40 | if(file.delete()){
41 | System.out.println("delete wallet file success");
42 | }
43 | }
44 | }
45 |
46 |
47 | @Test
48 | public void getAccount() throws Exception {
49 | Account acct = ontSdk.getWalletMgr().getWallet().getAccount(acct1.address);
50 | Assert.assertNotNull(acct);
51 |
52 | ontSdk.getWalletMgr().getWallet().setDefaultIdentity(id1.ontid);
53 | ontSdk.getWalletMgr().getWallet().setDefaultIdentity(1);
54 | ontSdk.getWalletMgr().getWallet().setDefaultAccount(acct1.address);
55 | ontSdk.getWalletMgr().getWallet().setDefaultAccount(1);
56 | Identity did = ontSdk.getWalletMgr().getWallet().getIdentity(id1.ontid);
57 | Assert.assertNotNull(did);
58 | boolean b = ontSdk.getWalletMgr().getWallet().removeIdentity(id1.ontid);
59 | Assert.assertTrue(b);
60 |
61 | boolean b2 = ontSdk.getWalletMgr().getWallet().removeAccount(acct1.address);
62 | Assert.assertTrue(b2);
63 |
64 |
65 | }
66 |
67 |
68 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/smartcontract/VmTest.java:
--------------------------------------------------------------------------------
1 | package com.github.ontio.smartcontract;
2 |
3 | import com.github.ontio.OntSdk;
4 | import com.github.ontio.common.Address;
5 | import com.github.ontio.sdk.exception.SDKException;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 |
9 | import static org.junit.Assert.*;
10 |
11 | public class VmTest {
12 |
13 | OntSdk ontSdk;
14 | Vm vm;
15 | @Before
16 | public void setUp(){
17 | ontSdk = OntSdk.getInstance();
18 | vm = new Vm(ontSdk);
19 |
20 | }
21 |
22 | @Test
23 | public void buildNativeParams() throws SDKException {
24 | // Address addr = Address.decodeBase58("TA9MXtwAcXkUMuujJh2iNRaWoXrvzfrmZb");
25 | // vm.buildNativeParams(addr,"init","1".getBytes(),null,0,0);
26 | }
27 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/ontio/smartcontract/nativevm/GovernanceTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018-2019 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | *
18 | */
19 |
20 | package com.github.ontio.smartcontract.nativevm;
21 |
22 | import org.junit.Test;
23 |
24 | import static org.junit.Assert.assertEquals;
25 | import static org.junit.Assert.assertNull;
26 |
27 | public class GovernanceTest {
28 | @Test
29 | public void newPeerAttributes() {
30 | PeerAttributes peerAttributes1 = new PeerAttributes();
31 | assertNull(peerAttributes1.peerPubkey);
32 | assertEquals(0, peerAttributes1.maxAuthorize);
33 | assertEquals(0, peerAttributes1.t1PeerCost);
34 | assertEquals(0, peerAttributes1.t2PeerCost);
35 | assertEquals(0, peerAttributes1.tPeerCost);
36 | String peerPubKey = "0379eff8cc07441daad01234291ba3f3da3e323119d97d6f1875da5f414be470b9";
37 | PeerAttributes peerAttributes2 = new PeerAttributes(peerPubKey);
38 | assertEquals(peerPubKey, peerAttributes2.peerPubkey);
39 | assertEquals(0, peerAttributes2.maxAuthorize);
40 | assertEquals(100, peerAttributes2.t1PeerCost);
41 | assertEquals(100, peerAttributes2.t2PeerCost);
42 | assertEquals(100, peerAttributes2.tPeerCost);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------