Yuanben Chain SDK for Java developers
jdk version :1.7
<dependency>
<groupId>com.yuanbenlian</groupId>
<artifactId>universe-java-sdk</artifactId>
<version>1.4.4-SNAPSHOT</version>
</dependency>
code | describe |
---|---|
ok | Success |
3001 | Invalid parameter |
3002 | Empty parameter |
3003 | Record does not exist |
3004 | Record already exists |
3005 | Permission denied: please register the public key first |
3006 | Permission denied: please contract Yuanben chain support |
3007 | Incorrect data |
3009 | Data storage fail |
3010 | Data not on YuanBen chain |
3011 | block information is empty |
3012 | Query fail |
3020 | Signature verification fail |
3023 | Parameter verification fail |
3021 | Invalid public key |
3022 | License's parameters are empty |
4001 | Error connecting to redis server |
4002 | Error connecting to the first-level node |
4003 | Broadcast transaction fail |
4004 | ABCI query fail |
4005 | Redis handling error |
5000 | Unknown error |
TestNet address: https://testnet.yuanbenlian.com
The Java-SDK provides three processors: Service/KeyProcessor, Service/DTCP Processor and Service/NodeProcessor.
1. Service/KeyProcessor
This is a base service, it supports: generating key pair, calculating signatures, verifying signatures, and more
2. Service/DTCP Processor
Processing metadata
3. Service/NodeProcessor
To access the YuanBen Chain Node, supports: query and saving metadata, query licenses, query latest BlockHash、registering public key
name | type | must | comment | source |
---|---|---|---|---|
type | string | Y | eg:image,article,audio,video,custom,private | user-defined |
language | string | Y | 'zh-CN', | user-defined,default:zh-CN |
title | string | N | title | user-defined |
signature | string | Y | sign by secp256k1 | generate by system |
abstract | string | N | Content summary | default:content[:200],user-defined |
category | string | N | eg:"news" | user-defined,if there is content, the system will add five more |
dna | string | Y | metadata dna | generate by system |
parent_dna | string | N | link an other metadata | user-defined |
block_hash | string | Y | block_hash on YuanBen chain | user-defined |
block_height | string | Y | block_hash corresponding block_height | user-defined |
created | integer | Y | timestamp, eg:1506302092 | generate by system |
content_hash | string | Y | Keccak256(content) | user-defined. default:Keccak256(content) |
extra | TreeMap<String, Object> | N | user-defined content | user-defined |
license | Metadata.License | Y | user-defined | |
license.type | string | Y | the type of license | user-defined |
license.parameters | TreeMap<String, Object> | N | the parameters of license | user-defined |
source | string | N | source link. | user-defined |
data | TreeMap<String, Object> | N | extension data of the type | user-defined |
id | string | N | business id | user-defined |
pubkey | string | N | public key | generate by private key |
/**
* Generate a key pair
*
* @return secp256k1 key pair
*/
public static SecretKey GeneratorSecp256k1Key() {
SecretKey secretKey = new SecretKey();
ECKey ecKey = new ECKey();
secretKey.setPrivateKey(Hex.toHexString(ecKey.getPrivKeyBytes()));
ECPoint pubKeyPoint = ecKey.getPubKeyPoint();
byte[] encoded = pubKeyPoint.getEncoded(true);
secretKey.setPublicKey(Hex.toHexString(encoded));
return secretKey;
}
location:KeyProcessor.java,return a key pair.
/**
* completing metadata
*
* @param privateKey
* @param metadata include(license\title\type\block_hash|block_height|category.
* if content is empty,you must pass content;
* if type isn't article,you must pass contentHash;
* if type isn't private,you must pass category)
* @return full metadata
* @throws InvalidException invalid parameters
*/
public static Metadata FullMetadata(String privateKey, Metadata metadata) throws InvalidException, UnsupportedEncodingException {
//......
}
input metadata:
name | type | must | comment | source |
---|---|---|---|---|
type | string | Y | eg:image,article,audio,video,custom,private | user-defined |
title | string | Y | content title | user-defined,private can be empty |
category | string | N | eg:"news,article" | user-defined,private can be empty |
block_hash | string | Y | block_hash on YuanBen chain (get by QueryLatestBlockHash) | user-defined |
block_height | string | Y | block_hash corresponding block_height (get by QueryLatestBlockHash) | user-defined |
content | string | N | content | user-defined,content and content_hash can't be empty at same time |
content_hash | string | N | Keccak256(content) | user-defined. default:Keccak256(content),content and content_hash can't be empty at same time |
data | TreeMap<String, Object> | Y | extension data of the type | user-defined,private\custom\article can be nil |
license | Metadata.License | Y | user-defined | |
license.type | string | Y | the type of license | user-defined |
license.parameters | TreeMap<String, Object> | Y | the parameters of license | user-defined,none can be nil |
created | integer | N | timestamp, eg:1506302092 | generate by system |
language | string | N | 'zh-CN', | user-defined,default:zh-CN |
parent_dna | string | N | link an other metadata | user-defined |
abstract | string | N | Content summary | default:content[:200],user-defined |
source | string | N | source link. | user-defined |
id | string | N | business id | user-defined |
pubkey | string | N | public key | generate by private key |
extra | TreeMap<String, Object> | N | more information by user defined | user-defined |
signature | string | N | sign by secp256k1 | generate by system |
dna | string | N | metadata dna | generate by system |
location:DTCPProcessor.java,return a full metadata.
/**
* submit metadata to YuanBen chain
*
* @param url node address(http://localhost:9000/v1)
* @param md metadata
* @return result
* @throws InvalidException
*/
public static MetadataSaveResp SaveMetadata(String url, Metadata md) throws InvalidException {
//......
}
input metadata:
name | type | must | comment |
---|---|---|---|
content_hash | string | Y | hash(content) |
created | integer | Y | timestamp, eg:1506302092 |
license | Metadata.License | Y | license information |
license.type | string | Y | the type of license |
license.parameters | TreeMap<String, Object> | Y | the parameters of license |
type | string | Y | eg:image,article,audio,video,custom,private |
block_hash | string | Y | block_hash on YuanBen chain |
block_height | string | Y | block_hash corresponding block_height |
pubkey | string | Y | public key |
signature | string | Y | sign by secp256k1 |
language | string | Y | 'zh-CN' |
dna | string | N | metadata dna |
title | string | N | content title |
category | string | N | eg:"news,article" |
data | TreeMap<String, Object> | N | extension data of the type |
parent_dna | string | N | link an other metadata |
abstract | string | N | Content summary |
source | string | N | source link. |
id | string | N | business id |
extra | TreeMap<String, Object> | N | more information by user defined |
location:NodeProcessor.java.
/**
* query license to YuanBen chain
*
* @param url node address (http://localhost:9000/v1)
* @param licenseType license's type
* @return result
* @throws InvalidException
*/
public static LicenseQueryResp QueryLicense(String url, String licenseType, String licenseVersion) throws InvalidException {
//.....
}
location:NodeProcessor.java。Because of network latency, it may not be the latest block information. If you can't get it, please fill it with a fixed value. The value of the last request is a good choice.
/**
* query metadata from YuanBen chain
*
* @param url node address (http://localhost:9000/v1)
* @param dna DNA
* @return result include metadata and transaction information
* @throws InvalidException
*/
public static MetadataQueryResp QueryMetadata(String url, String dna) throws InvalidException {
if (StringUtils.isBlank(url) || StringUtils.isBlank(dna)) {
throw new InvalidException("url or DNA is empty");
}
url += "/metadata/" + dna;
String s = HttpUtil.sendGet(url);
return GsonUtil.getInstance().fromJson(s, MetadataQueryResp.class);
}
location:NodeProcessor.java。
/**
* register public key
*
* @param privateKey
* @param subPubKeys
* @return request
* @throws InvalidException invalid parameters
*/
public static RegisterAccountReq GenRegisterAccountReq(String privateKey, String[] subPubKeys) throws InvalidException {
//......
}
location:DTCPProcessor.java. If the node does not open authentication mode, there is no need to use this method.
/**
* register public key to YuanBen chain
*
* @param url node address (http://localhost:9000/v1)
* @param req request
* @return result
* @throws InvalidException
*/
public static RegisterAccountResp RegisterAccount(String url, RegisterAccountReq req) throws InvalidException {
//....
}
location:NodeProcessor.java.