modify digibank/magnetocorp application-java to fit fabric-gateway-java:2.0.0 and fabric-chaincode-java:2.0.0

Signed-off-by: linqili <charlielin06@gmail.com>
This commit is contained in:
linqili 2020-04-04 02:06:12 +08:00
parent 0eb52a0ac7
commit cee549edec
13 changed files with 612 additions and 669 deletions

View file

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View file

@ -1,61 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>commercial-paper</groupId>
<artifactId>commercial-paper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>hyperledger</id>
<name>Hyperledger Artifactory</name>
<url>https://hyperledger.jfrog.io/hyperledger/fabric-maven</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<properties>
<fabric-chaincode-java.version>1.4.2</fabric-chaincode-java.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View file

@ -1,99 +1,95 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>commercial-paper</groupId> <groupId>commercial-paper</groupId>
<artifactId>commercial-paper</artifactId> <artifactId>commercial-paper</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<properties> <properties>
<!-- Generic properties --> <!-- Generic properties -->
<java.version>1.8</java.version> <java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- fabric-chaincode-java --> <!-- fabric-chaincode-java -->
<fabric-chaincode-java.version>[2.0.0,2.1)</fabric-chaincode-java.version> <fabric-chaincode-java.version>[2.0.0,2.1)</fabric-chaincode-java.version>
<fabric-gateway-java.version>2.0.0</fabric-gateway-java.version>
</properties> </properties>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version> <version>3.8.0</version>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version> <version>3.2.0</version>
<executions> <executions>
<!-- Attach the shade goal into the package phase --> <!-- Attach the shade goal into the package phase -->
<execution> <execution>
<phase>package</phase> <phase>package</phase>
<goals> <goals>
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
<configuration> <configuration>
<filters> <createDependencyReducedPom>false</createDependencyReducedPom>
<filter> <filters>
<artifact>*:*</artifact> <filter>
<excludes> <artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude> <exclude>META-INF/*.RSA</exclude>
</excludes> </excludes>
</filter> </filter>
</filters> </filters>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<repositories> <repositories>
<repository> <repository>
<id>hyperledger</id> <id>jitpack.io</id>
<name>Hyperledger Nexus</name> <url>https://jitpack.io</url>
<url>https://hyperledger.jfrog.io/hyperledger/fabric-maven</url> </repository>
</repository> </repositories>
<repository> <dependencies>
<id>jitpack.io</id> <dependency>
<url>https://jitpack.io</url> <groupId>org.hyperledger.fabric</groupId>
</repository> <artifactId>fabric-gateway-java</artifactId>
</repositories> <version>${fabric-gateway-java.version}</version>
</dependency>
<dependencies> <!-- Used for datatype annotations only -->
<dependency> <dependency>
<groupId>org.hyperledger.fabric-gateway-java</groupId> <groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-gateway-java</artifactId> <artifactId>fabric-chaincode-shim</artifactId>
<version>2.0.0-SNAPSHOT</version> <version>${fabric-chaincode-java.version}</version>
</dependency> <scope>compile</scope>
</dependency>
<!-- Used for datatype annotations only --> <!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency> <dependency>
<groupId>org.hyperledger.fabric-chaincode-java</groupId> <groupId>org.json</groupId>
<artifactId>fabric-chaincode-shim</artifactId> <artifactId>json</artifactId>
<version>${fabric-chaincode-java.version}</version> <version>20180813</version>
<scope>compile</scope> </dependency>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json --> </dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
</dependencies>
</project> </project>

View file

@ -5,37 +5,61 @@ SPDX-License-Identifier: Apache-2.0
package org.digibank; package org.digibank;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.InvalidKeyException;
import org.hyperledger.fabric.gateway.GatewayException; import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.hyperledger.fabric.gateway.Identities;
import org.hyperledger.fabric.gateway.Identity;
import org.hyperledger.fabric.gateway.Wallet; import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallet.Identity; import org.hyperledger.fabric.gateway.Wallets;
public class AddToWallet { public class AddToWallet {
private static X509Certificate readX509Certificate(final Path certificatePath) throws IOException, CertificateException {
try (Reader certificateReader = Files.newBufferedReader(certificatePath, StandardCharsets.UTF_8)) {
return Identities.readX509Certificate(certificateReader);
}
}
private static PrivateKey getPrivateKey(final Path privateKeyPath) throws IOException, InvalidKeyException {
try (Reader privateKeyReader = Files.newBufferedReader(privateKeyPath, StandardCharsets.UTF_8)) {
return Identities.readPrivateKey(privateKeyReader);
}
}
public static void main(String[] args) { public static void main(String[] args) {
try { try {
// A wallet stores a collection of identities // A wallet stores a collection of identities
Path walletPath = Paths.get("..", "identity", "user", "balaji", "wallet"); Path walletPath = Paths.get(".", "wallet");
Wallet wallet = Wallet.createFileSystemWallet(walletPath); Wallet wallet = Wallets.newFileSystemWallet(walletPath);
// Location of credentials to be stored in the wallet Path credentialPath = Paths.get("..", "..", "..",".." ,"test-network", "organizations",
Path credentialPath = Paths.get("..", "..", "..", "..","basic-network", "crypto-config", "peerOrganizations", "org1.example.com", "users", "User1@org1.example.com", "msp");
"peerOrganizations", "org1.example.com", "users", "Admin@org1.example.com", "msp"); System.out.println("credentialPath: " + credentialPath.toString());
Path certificatePem = credentialPath.resolve(Paths.get("signcerts", Path certificatePath = credentialPath.resolve(Paths.get("signcerts",
"Admin@org1.example.com-cert.pem")); "User1@org1.example.com-cert.pem"));
Path privateKey = credentialPath.resolve(Paths.get("keystore", System.out.println("certificatePem: " + certificatePath.toString());
"cd96d5260ad4757551ed4a5a991e62130f8008a0bf996e4e4b84cd097a747fec_sk")); Path privateKeyPath = credentialPath.resolve(Paths.get("keystore",
"priv_sk"));
// Load credentials into wallet X509Certificate certificate = readX509Certificate(certificatePath);
String identityLabel = "Admin@org1.example.com"; PrivateKey privateKey = getPrivateKey(privateKeyPath);
Identity identity = Identity.createIdentity("Org1MSP", Files.newBufferedReader(certificatePem), Files.newBufferedReader(privateKey));
Identity identity = Identities.newX509Identity("Org1MSP", certificate, privateKey);
String identityLabel = "User1@org1.example.com";
wallet.put(identityLabel, identity); wallet.put(identityLabel, identity);
} catch (IOException e) { System.out.println("Write wallet info into " + walletPath.toString() + " successfully.");
} catch (IOException | CertificateException | InvalidKeyException e) {
System.err.println("Error adding to wallet"); System.err.println("Error adding to wallet");
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -15,58 +15,60 @@ import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.GatewayException; import org.hyperledger.fabric.gateway.GatewayException;
import org.hyperledger.fabric.gateway.Network; import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.gateway.Wallet; import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
import org.papernet.CommercialPaper; import org.papernet.CommercialPaper;
public class Buy { public class Buy {
private static final String ENVKEY="CONTRACT_NAME"; private static final String ENVKEY="CONTRACT_NAME";
public static void main(String[] args) { public static void main(String[] args) {
Gateway.Builder builder = Gateway.createBuilder(); Gateway.Builder builder = Gateway.createBuilder();
String contractName="papercontract"; String contractName="papercontract";
// get the name of the contract, in case it is overridden // get the name of the contract, in case it is overridden
Map<String,String> envvar = System.getenv(); Map<String,String> envvar = System.getenv();
if (envvar.containsKey(ENVKEY)){ if (envvar.containsKey(ENVKEY)){
contractName=envvar.get(ENVKEY); contractName=envvar.get(ENVKEY);
} }
try { try {
// A wallet stores a collection of identities // A wallet stores a collection of identities
Path walletPath = Paths.get("..", "identity", "user", "balaji", "wallet"); Path walletPath = Paths.get(".", "wallet");
Wallet wallet = Wallet.createFileSystemWallet(walletPath); Wallet wallet = Wallets.newFileSystemWallet(walletPath);
System.out.println("Read wallet info from: " + walletPath);
String userName = "Admin@org1.example.com"; String userName = "User1@org1.example.com";
Path connectionProfile = Paths.get("..", "gateway", "networkConnection.yaml"); Path connectionProfile = Paths.get("..", "gateway", "connection-org1.yaml");
// Set connection options on the gateway builder // Set connection options on the gateway builder
builder.identity(wallet, userName).networkConfig(connectionProfile).discovery(false); builder.identity(wallet, userName).networkConfig(connectionProfile).discovery(false);
// Connect to gateway using application specified parameters // Connect to gateway using application specified parameters
try(Gateway gateway = builder.connect()) { try(Gateway gateway = builder.connect()) {
// Access PaperNet network // Access PaperNet network
System.out.println("Use network channel: mychannel."); System.out.println("Use network channel: mychannel.");
Network network = gateway.getNetwork("mychannel"); Network network = gateway.getNetwork("mychannel");
// Get addressability to commercial paper contract // Get addressability to commercial paper contract
System.out.println("Use org.papernet.commercialpaper smart contract."); System.out.println("Use org.papernet.commercialpaper smart contract.");
Contract contract = network.getContract(contractName, "org.papernet.commercialpaper"); Contract contract = network.getContract(contractName, "org.papernet.commercialpaper");
// Buy commercial paper // Buy commercial paper
System.out.println("Submit commercial paper buy transaction."); System.out.println("Submit commercial paper buy transaction.");
byte[] response = contract.submitTransaction("buy", "MagnetoCorp", "00001", "MagnetoCorp", "DigiBank", "4900000", "2020-05-31"); byte[] response = contract.submitTransaction("buy", "MagnetoCorp", "00001", "MagnetoCorp", "DigiBank", "4900000", "2020-05-31");
// Process response // Process response
System.out.println("Process buy transaction response."); System.out.println("Process buy transaction response.");
CommercialPaper paper = CommercialPaper.deserialize(response); CommercialPaper paper = CommercialPaper.deserialize(response);
System.out.println(paper); System.out.println(paper);
} }
} catch (GatewayException | IOException | TimeoutException | InterruptedException e) { } catch (GatewayException | IOException | TimeoutException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(-1); System.exit(-1);
} }
} }
} }

View file

@ -15,58 +15,59 @@ import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.GatewayException; import org.hyperledger.fabric.gateway.GatewayException;
import org.hyperledger.fabric.gateway.Network; import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.gateway.Wallet; import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
import org.papernet.CommercialPaper; import org.papernet.CommercialPaper;
public class Redeem { public class Redeem {
private static final String ENVKEY="CONTRACT_NAME"; private static final String ENVKEY="CONTRACT_NAME";
public static void main(String[] args) { public static void main(String[] args) {
Gateway.Builder builder = Gateway.createBuilder(); Gateway.Builder builder = Gateway.createBuilder();
String contractName="papercontract"; String contractName="papercontract";
// get the name of the contract, in case it is overridden // get the name of the contract, in case it is overridden
Map<String,String> envvar = System.getenv(); Map<String,String> envvar = System.getenv();
if (envvar.containsKey(ENVKEY)){ if (envvar.containsKey(ENVKEY)){
contractName=envvar.get(ENVKEY); contractName=envvar.get(ENVKEY);
} }
try { try {
// A wallet stores a collection of identities // A wallet stores a collection of identities
Path walletPath = Paths.get("..", "identity", "user", "balaji", "wallet"); Path walletPath = Paths.get(".", "wallet");
Wallet wallet = Wallet.createFileSystemWallet(walletPath); Wallet wallet = Wallets.newFileSystemWallet(walletPath);
String userName = "Admin@org1.example.com"; String userName = "User1@org1.example.com";
Path connectionProfile = Paths.get("..", "gateway", "networkConnection.yaml"); Path connectionProfile = Paths.get("..", "gateway", "connection-org1.yaml");
// Set connection options on the gateway builder // Set connection options on the gateway builder
builder.identity(wallet, userName).networkConfig(connectionProfile).discovery(false); builder.identity(wallet, userName).networkConfig(connectionProfile).discovery(false);
// Connect to gateway using application specified parameters // Connect to gateway using application specified parameters
try(Gateway gateway = builder.connect()) { try(Gateway gateway = builder.connect()) {
// Access PaperNet network // Access PaperNet network
System.out.println("Use network channel: mychannel."); System.out.println("Use network channel: mychannel.");
Network network = gateway.getNetwork("mychannel"); Network network = gateway.getNetwork("mychannel");
// Get addressability to commercial paper contract // Get addressability to commercial paper contract
System.out.println("Use org.papernet.commercialpaper smart contract."); System.out.println("Use org.papernet.commercialpaper smart contract.");
Contract contract = network.getContract("papercontract", "org.papernet.commercialpaper"); Contract contract = network.getContract("papercontract", "org.papernet.commercialpaper");
// Redeem commercial paper // Redeem commercial paper
System.out.println("Submit commercial paper redeem transaction."); System.out.println("Submit commercial paper redeem transaction.");
byte[] response = contract.submitTransaction("redeem", "MagnetoCorp", "00001", "DigiBank", "2020-11-30"); byte[] response = contract.submitTransaction("redeem", "MagnetoCorp", "00001", "DigiBank", "2020-11-30");
// Process response // Process response
System.out.println("Process redeem transaction response."); System.out.println("Process redeem transaction response.");
CommercialPaper paper = CommercialPaper.deserialize(response); CommercialPaper paper = CommercialPaper.deserialize(response);
System.out.println(paper); System.out.println(paper);
} }
} catch (GatewayException | IOException | TimeoutException | InterruptedException e) { } catch (GatewayException | IOException | TimeoutException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(-1); System.exit(-1);
} }
} }
} }

View file

@ -14,168 +14,168 @@ import org.json.JSONPropertyIgnore;
@DataType() @DataType()
public class CommercialPaper extends State { public class CommercialPaper extends State {
// Enumerate commercial paper state values // Enumerate commercial paper state values
public final static String ISSUED = "ISSUED"; public final static String ISSUED = "ISSUED";
public final static String TRADING = "TRADING"; public final static String TRADING = "TRADING";
public final static String REDEEMED = "REDEEMED"; public final static String REDEEMED = "REDEEMED";
@Property() @Property()
private String state=""; private String state="";
public String getState() { public String getState() {
return state; return state;
} }
public CommercialPaper setState(String state) { public CommercialPaper setState(String state) {
this.state = state; this.state = state;
return this; return this;
} }
@JSONPropertyIgnore() @JSONPropertyIgnore()
public boolean isIssued() { public boolean isIssued() {
return this.state.equals(CommercialPaper.ISSUED); return this.state.equals(CommercialPaper.ISSUED);
} }
@JSONPropertyIgnore() @JSONPropertyIgnore()
public boolean isTrading() { public boolean isTrading() {
return this.state.equals(CommercialPaper.TRADING); return this.state.equals(CommercialPaper.TRADING);
} }
@JSONPropertyIgnore() @JSONPropertyIgnore()
public boolean isRedeemed() { public boolean isRedeemed() {
return this.state.equals(CommercialPaper.REDEEMED); return this.state.equals(CommercialPaper.REDEEMED);
} }
public CommercialPaper setIssued() { public CommercialPaper setIssued() {
this.state = CommercialPaper.ISSUED; this.state = CommercialPaper.ISSUED;
return this; return this;
} }
public CommercialPaper setTrading() { public CommercialPaper setTrading() {
this.state = CommercialPaper.TRADING; this.state = CommercialPaper.TRADING;
return this; return this;
} }
public CommercialPaper setRedeemed() { public CommercialPaper setRedeemed() {
this.state = CommercialPaper.REDEEMED; this.state = CommercialPaper.REDEEMED;
return this; return this;
} }
@Property() @Property()
private String paperNumber; private String paperNumber;
@Property() @Property()
private String issuer; private String issuer;
@Property() @Property()
private String issueDateTime; private String issueDateTime;
@Property() @Property()
private int faceValue; private int faceValue;
@Property() @Property()
private String maturityDateTime; private String maturityDateTime;
@Property() @Property()
private String owner; private String owner;
public String getOwner() { public String getOwner() {
return owner; return owner;
} }
public CommercialPaper setOwner(String owner) { public CommercialPaper setOwner(String owner) {
this.owner = owner; this.owner = owner;
return this; return this;
} }
public CommercialPaper() { public CommercialPaper() {
super(); super();
} }
public CommercialPaper setKey() { public CommercialPaper setKey() {
this.key = State.makeKey(new String[] { this.paperNumber }); this.key = State.makeKey(new String[] { this.paperNumber });
return this; return this;
} }
public String getPaperNumber() { public String getPaperNumber() {
return paperNumber; return paperNumber;
} }
public CommercialPaper setPaperNumber(String paperNumber) { public CommercialPaper setPaperNumber(String paperNumber) {
this.paperNumber = paperNumber; this.paperNumber = paperNumber;
return this; return this;
} }
public String getIssuer() { public String getIssuer() {
return issuer; return issuer;
} }
public CommercialPaper setIssuer(String issuer) { public CommercialPaper setIssuer(String issuer) {
this.issuer = issuer; this.issuer = issuer;
return this; return this;
} }
public String getIssueDateTime() { public String getIssueDateTime() {
return issueDateTime; return issueDateTime;
} }
public CommercialPaper setIssueDateTime(String issueDateTime) { public CommercialPaper setIssueDateTime(String issueDateTime) {
this.issueDateTime = issueDateTime; this.issueDateTime = issueDateTime;
return this; return this;
} }
public int getFaceValue() { public int getFaceValue() {
return faceValue; return faceValue;
} }
public CommercialPaper setFaceValue(int faceValue) { public CommercialPaper setFaceValue(int faceValue) {
this.faceValue = faceValue; this.faceValue = faceValue;
return this; return this;
} }
public String getMaturityDateTime() { public String getMaturityDateTime() {
return maturityDateTime; return maturityDateTime;
} }
public CommercialPaper setMaturityDateTime(String maturityDateTime) { public CommercialPaper setMaturityDateTime(String maturityDateTime) {
this.maturityDateTime = maturityDateTime; this.maturityDateTime = maturityDateTime;
return this; return this;
} }
@Override @Override
public String toString() { public String toString() {
return "Paper::" + this.key + " " + this.getPaperNumber() + " " + getIssuer() + " " + getFaceValue(); return "Paper::" + this.key + " " + this.getPaperNumber() + " " + getIssuer() + " " + getFaceValue();
} }
/** /**
* Deserialize a state data to commercial paper * Deserialize a state data to commercial paper
* *
* @param {Buffer} data to form back into the object * @param {Buffer} data to form back into the object
*/ */
public static CommercialPaper deserialize(byte[] data) { public static CommercialPaper deserialize(byte[] data) {
JSONObject json = new JSONObject(new String(data, UTF_8)); JSONObject json = new JSONObject(new String(data, UTF_8));
String issuer = json.getString("issuer"); String issuer = json.getString("issuer");
String paperNumber = json.getString("paperNumber"); String paperNumber = json.getString("paperNumber");
String issueDateTime = json.getString("issueDateTime"); String issueDateTime = json.getString("issueDateTime");
String maturityDateTime = json.getString("maturityDateTime"); String maturityDateTime = json.getString("maturityDateTime");
String owner = json.getString("owner"); String owner = json.getString("owner");
int faceValue = json.getInt("faceValue"); int faceValue = json.getInt("faceValue");
String state = json.getString("state"); String state = json.getString("state");
return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue,owner,state); return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue,owner,state);
} }
public static byte[] serialize(CommercialPaper paper) { public static byte[] serialize(CommercialPaper paper) {
return State.serialize(paper); return State.serialize(paper);
} }
/** /**
* Factory method to create a commercial paper object * Factory method to create a commercial paper object
*/ */
public static CommercialPaper createInstance(String issuer, String paperNumber, String issueDateTime, public static CommercialPaper createInstance(String issuer, String paperNumber, String issueDateTime,
String maturityDateTime, int faceValue, String owner, String state) { String maturityDateTime, int faceValue, String owner, String state) {
return new CommercialPaper().setIssuer(issuer).setPaperNumber(paperNumber).setMaturityDateTime(maturityDateTime) return new CommercialPaper().setIssuer(issuer).setPaperNumber(paperNumber).setMaturityDateTime(maturityDateTime)
.setFaceValue(faceValue).setKey().setIssueDateTime(issueDateTime).setOwner(owner).setState(state); .setFaceValue(faceValue).setKey().setIssueDateTime(issueDateTime).setOwner(owner).setState(state);
} }
} }

View file

@ -13,48 +13,48 @@ import org.json.JSONObject;
*/ */
public class State { public class State {
protected String key; protected String key;
/** /**
* @param {String|Object} class An identifiable class of the instance * @param {String|Object} class An identifiable class of the instance
* @param {keyParts[]} elements to pull together to make a key for the objects * @param {keyParts[]} elements to pull together to make a key for the objects
*/ */
public State() { public State() {
} }
String getKey() { String getKey() {
return this.key; return this.key;
} }
public String[] getSplitKey() { public String[] getSplitKey() {
return State.splitKey(this.key); return State.splitKey(this.key);
} }
/** /**
* Convert object to buffer containing JSON data serialization Typically used * Convert object to buffer containing JSON data serialization Typically used
* before putState()ledger API * before putState()ledger API
* *
* @param {Object} JSON object to serialize * @param {Object} JSON object to serialize
* @return {buffer} buffer with the data to store * @return {buffer} buffer with the data to store
*/ */
public static byte[] serialize(Object object) { public static byte[] serialize(Object object) {
String jsonStr = new JSONObject(object).toString(); String jsonStr = new JSONObject(object).toString();
return jsonStr.getBytes(UTF_8); return jsonStr.getBytes(UTF_8);
} }
/** /**
* Join the keyParts to make a unififed string * Join the keyParts to make a unififed string
* *
* @param (String[]) keyParts * @param (String[]) keyParts
*/ */
public static String makeKey(String[] keyParts) { public static String makeKey(String[] keyParts) {
return String.join(":", keyParts); return String.join(":", keyParts);
} }
public static String[] splitKey(String key) { public static String[] splitKey(String key) {
System.out.println("splitting key " + key + " " + java.util.Arrays.asList(key.split(":"))); System.out.println("splitting key " + key + " " + java.util.Arrays.asList(key.split(":")));
return key.split(":"); return key.split(":");
} }
} }

View file

@ -1,95 +1,96 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>commercial-paper</groupId> <groupId>commercial-paper</groupId>
<artifactId>commercial-paper</artifactId> <artifactId>commercial-paper</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<properties> <properties>
<!-- Generic properties --> <!-- Generic properties -->
<java.version>1.8</java.version> <java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- fabric-chaincode-java --> <!-- fabric-chaincode-java -->
<fabric-chaincode-java.version>2.0.0</fabric-chaincode-java.version> <fabric-chaincode-java.version>2.0.0</fabric-chaincode-java.version>
<!-- fabric-gateway-java --> <!-- fabric-gateway-java -->
<fabric-gateway-java.version>2.0.0</fabric-gateway-java.version> <fabric-gateway-java.version>2.0.0</fabric-gateway-java.version>
</properties> </properties>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version> <version>3.8.0</version>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version> <version>3.2.0</version>
<executions> <executions>
<!-- Attach the shade goal into the package phase --> <!-- Attach the shade goal into the package phase -->
<execution> <execution>
<phase>package</phase> <phase>package</phase>
<goals> <goals>
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
<configuration> <configuration>
<filters> <createDependencyReducedPom>false</createDependencyReducedPom>
<filter> <filters>
<artifact>*:*</artifact> <filter>
<excludes> <artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude> <exclude>META-INF/*.RSA</exclude>
</excludes> </excludes>
</filter> </filter>
</filters> </filters>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<repositories> <repositories>
<repository> <repository>
<id>jitpack.io</id> <id>jitpack.io</id>
<url>https://jitpack.io</url> <url>https://jitpack.io</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.hyperledger.fabric</groupId> <groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId> <artifactId>fabric-gateway-java</artifactId>
<version>${fabric-gateway-java.version}</version> <version>${fabric-gateway-java.version}</version>
</dependency> </dependency>
<!-- Used for datatype annotations only --> <!-- Used for datatype annotations only -->
<dependency> <dependency>
<groupId>org.hyperledger.fabric-chaincode-java</groupId> <groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-chaincode-shim</artifactId> <artifactId>fabric-chaincode-shim</artifactId>
<version>${fabric-chaincode-java.version}</version> <version>${fabric-chaincode-java.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.json/json --> <!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>org.json</groupId>
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>20180813</version> <version>20180813</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -46,7 +46,7 @@ public class AddToWallet {
"User1@org2.example.com-cert.pem")); "User1@org2.example.com-cert.pem"));
System.out.println("certificatePem: " + certificatePath.toString()); System.out.println("certificatePem: " + certificatePath.toString());
Path privateKeyPath = credentialPath.resolve(Paths.get("keystore", Path privateKeyPath = credentialPath.resolve(Paths.get("keystore",
"c7077781c776d90009b3f68a75b15109e1cb82ae5dbdcc0b79644ff47d280db8_sk")); "priv_sk"));
X509Certificate certificate = readX509Certificate(certificatePath); X509Certificate certificate = readX509Certificate(certificatePath);
PrivateKey privateKey = getPrivateKey(privateKeyPath); PrivateKey privateKey = getPrivateKey(privateKeyPath);
@ -57,7 +57,7 @@ public class AddToWallet {
String identityLabel = "User1@org2.example.com"; String identityLabel = "User1@org2.example.com";
wallet.put(identityLabel, identity); wallet.put(identityLabel, identity);
System.out.println("成功写入钱包信息:" + walletPath.toString()); System.out.println("Write wallet info into " + walletPath.toString() + " successfully.");
} catch (IOException | CertificateException | InvalidKeyException e) { } catch (IOException | CertificateException | InvalidKeyException e) {
System.err.println("Error adding to wallet"); System.err.println("Error adding to wallet");

View file

@ -59,7 +59,7 @@ public class Issue {
// Issue commercial paper // Issue commercial paper
System.out.println("Submit commercial paper issue transaction."); System.out.println("Submit commercial paper issue transaction.");
byte[] response = contract.submitTransaction("issue", "MagnetoCorp", "00002", "2020-05-31", "2020-11-30", "5000000"); byte[] response = contract.submitTransaction("issue", "MagnetoCorp", "00001", "2020-05-31", "2020-11-30", "5000000");
// Process response // Process response
System.out.println("Process issue transaction response."); System.out.println("Process issue transaction response.");

View file

@ -15,169 +15,169 @@ import org.papernet.ledgerapi.State;
@DataType() @DataType()
public class CommercialPaper extends State { public class CommercialPaper extends State {
// Enumerate commercial paper state values // Enumerate commercial paper state values
public final static String ISSUED = "ISSUED"; public final static String ISSUED = "ISSUED";
public final static String TRADING = "TRADING"; public final static String TRADING = "TRADING";
public final static String REDEEMED = "REDEEMED"; public final static String REDEEMED = "REDEEMED";
@Property() @Property()
private String state=""; private String state="";
public String getState() { public String getState() {
return state; return state;
} }
public CommercialPaper setState(String state) { public CommercialPaper setState(String state) {
this.state = state; this.state = state;
return this; return this;
} }
@JSONPropertyIgnore() @JSONPropertyIgnore()
public boolean isIssued() { public boolean isIssued() {
return this.state.equals(CommercialPaper.ISSUED); return this.state.equals(CommercialPaper.ISSUED);
} }
@JSONPropertyIgnore() @JSONPropertyIgnore()
public boolean isTrading() { public boolean isTrading() {
return this.state.equals(CommercialPaper.TRADING); return this.state.equals(CommercialPaper.TRADING);
} }
@JSONPropertyIgnore() @JSONPropertyIgnore()
public boolean isRedeemed() { public boolean isRedeemed() {
return this.state.equals(CommercialPaper.REDEEMED); return this.state.equals(CommercialPaper.REDEEMED);
} }
public CommercialPaper setIssued() { public CommercialPaper setIssued() {
this.state = CommercialPaper.ISSUED; this.state = CommercialPaper.ISSUED;
return this; return this;
} }
public CommercialPaper setTrading() { public CommercialPaper setTrading() {
this.state = CommercialPaper.TRADING; this.state = CommercialPaper.TRADING;
return this; return this;
} }
public CommercialPaper setRedeemed() { public CommercialPaper setRedeemed() {
this.state = CommercialPaper.REDEEMED; this.state = CommercialPaper.REDEEMED;
return this; return this;
} }
@Property() @Property()
private String paperNumber; private String paperNumber;
@Property() @Property()
private String issuer; private String issuer;
@Property() @Property()
private String issueDateTime; private String issueDateTime;
@Property() @Property()
private int faceValue; private int faceValue;
@Property() @Property()
private String maturityDateTime; private String maturityDateTime;
@Property() @Property()
private String owner; private String owner;
public String getOwner() { public String getOwner() {
return owner; return owner;
} }
public CommercialPaper setOwner(String owner) { public CommercialPaper setOwner(String owner) {
this.owner = owner; this.owner = owner;
return this; return this;
} }
public CommercialPaper() { public CommercialPaper() {
super(); super();
} }
public CommercialPaper setKey() { public CommercialPaper setKey() {
this.key = State.makeKey(new String[] { this.paperNumber }); this.key = State.makeKey(new String[] { this.paperNumber });
return this; return this;
} }
public String getPaperNumber() { public String getPaperNumber() {
return paperNumber; return paperNumber;
} }
public CommercialPaper setPaperNumber(String paperNumber) { public CommercialPaper setPaperNumber(String paperNumber) {
this.paperNumber = paperNumber; this.paperNumber = paperNumber;
return this; return this;
} }
public String getIssuer() { public String getIssuer() {
return issuer; return issuer;
} }
public CommercialPaper setIssuer(String issuer) { public CommercialPaper setIssuer(String issuer) {
this.issuer = issuer; this.issuer = issuer;
return this; return this;
} }
public String getIssueDateTime() { public String getIssueDateTime() {
return issueDateTime; return issueDateTime;
} }
public CommercialPaper setIssueDateTime(String issueDateTime) { public CommercialPaper setIssueDateTime(String issueDateTime) {
this.issueDateTime = issueDateTime; this.issueDateTime = issueDateTime;
return this; return this;
} }
public int getFaceValue() { public int getFaceValue() {
return faceValue; return faceValue;
} }
public CommercialPaper setFaceValue(int faceValue) { public CommercialPaper setFaceValue(int faceValue) {
this.faceValue = faceValue; this.faceValue = faceValue;
return this; return this;
} }
public String getMaturityDateTime() { public String getMaturityDateTime() {
return maturityDateTime; return maturityDateTime;
} }
public CommercialPaper setMaturityDateTime(String maturityDateTime) { public CommercialPaper setMaturityDateTime(String maturityDateTime) {
this.maturityDateTime = maturityDateTime; this.maturityDateTime = maturityDateTime;
return this; return this;
} }
@Override @Override
public String toString() { public String toString() {
return "Paper::" + this.key + " " + this.getPaperNumber() + " " + getIssuer() + " " + getFaceValue(); return "Paper::" + this.key + " " + this.getPaperNumber() + " " + getIssuer() + " " + getFaceValue();
} }
/** /**
* Deserialize a state data to commercial paper * Deserialize a state data to commercial paper
* *
* @param {Buffer} data to form back into the object * @param {Buffer} data to form back into the object
*/ */
public static CommercialPaper deserialize(byte[] data) { public static CommercialPaper deserialize(byte[] data) {
JSONObject json = new JSONObject(new String(data, UTF_8)); JSONObject json = new JSONObject(new String(data, UTF_8));
String issuer = json.getString("issuer"); String issuer = json.getString("issuer");
String paperNumber = json.getString("paperNumber"); String paperNumber = json.getString("paperNumber");
String issueDateTime = json.getString("issueDateTime"); String issueDateTime = json.getString("issueDateTime");
String maturityDateTime = json.getString("maturityDateTime"); String maturityDateTime = json.getString("maturityDateTime");
String owner = json.getString("owner"); String owner = json.getString("owner");
int faceValue = json.getInt("faceValue"); int faceValue = json.getInt("faceValue");
String state = json.getString("state"); String state = json.getString("state");
return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue, owner, state); return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue, owner, state);
} }
public static byte[] serialize(CommercialPaper paper) { public static byte[] serialize(CommercialPaper paper) {
return State.serialize(paper); return State.serialize(paper);
} }
/** /**
* Factory method to create a commercial paper object * Factory method to create a commercial paper object
*/ */
public static CommercialPaper createInstance(String issuer, String paperNumber, String issueDateTime, public static CommercialPaper createInstance(String issuer, String paperNumber, String issueDateTime,
String maturityDateTime, int faceValue, String owner, String state) { String maturityDateTime, int faceValue, String owner, String state) {
return new CommercialPaper().setIssuer(issuer).setPaperNumber(paperNumber).setMaturityDateTime(maturityDateTime) return new CommercialPaper().setIssuer(issuer).setPaperNumber(paperNumber).setMaturityDateTime(maturityDateTime)
.setFaceValue(faceValue).setKey().setIssueDateTime(issueDateTime).setOwner(issuer).setState(state); .setFaceValue(faceValue).setKey().setIssueDateTime(issueDateTime).setOwner(issuer).setState(state);
} }
} }

View file

@ -13,48 +13,48 @@ import org.json.JSONObject;
*/ */
public class State { public class State {
protected String key; protected String key;
/** /**
* @param {String|Object} class An identifiable class of the instance * @param {String|Object} class An identifiable class of the instance
* @param {keyParts[]} elements to pull together to make a key for the objects * @param {keyParts[]} elements to pull together to make a key for the objects
*/ */
public State() { public State() {
} }
String getKey() { String getKey() {
return this.key; return this.key;
} }
public String[] getSplitKey() { public String[] getSplitKey() {
return State.splitKey(this.key); return State.splitKey(this.key);
} }
/** /**
* Convert object to buffer containing JSON data serialization Typically used * Convert object to buffer containing JSON data serialization Typically used
* before putState()ledger API * before putState()ledger API
* *
* @param {Object} JSON object to serialize * @param {Object} JSON object to serialize
* @return {buffer} buffer with the data to store * @return {buffer} buffer with the data to store
*/ */
public static byte[] serialize(Object object) { public static byte[] serialize(Object object) {
String jsonStr = new JSONObject(object).toString(); String jsonStr = new JSONObject(object).toString();
return jsonStr.getBytes(UTF_8); return jsonStr.getBytes(UTF_8);
} }
/** /**
* Join the keyParts to make a unififed string * Join the keyParts to make a unififed string
* *
* @param (String[]) keyParts * @param (String[]) keyParts
*/ */
public static String makeKey(String[] keyParts) { public static String makeKey(String[] keyParts) {
return String.join(":", keyParts); return String.join(":", keyParts);
} }
public static String[] splitKey(String key) { public static String[] splitKey(String key) {
System.out.println("splitting key " + key + " " + java.util.Arrays.asList(key.split(":"))); System.out.println("splitting key " + key + " " + java.util.Arrays.asList(key.split(":")));
return key.split(":"); return key.split(":");
} }
} }