修改创世块json字符串输出类

This commit is contained in:
jiangbuyun@sina.com 2019-03-17 10:07:56 +08:00
parent 5316ef294a
commit 0cd6bafe98
7 changed files with 137 additions and 90 deletions

View File

@ -97,7 +97,7 @@ system {
vote_node_list = ["121000005l35120456.node1","2","3","4"]
}
account{
chaincodename = "ACCOUNTCHAINCODENAME"//账户管理合约的名称
chaincodename = "REPCHAINACCOUNTMGR"//账户管理合约的名称
}
diskspaceManager{

View File

@ -82,17 +82,17 @@ message ChaincodeId {
// ,/
message ChaincodeDeploy {
//Type only support javascript
int32 timeout = 3;
int32 timeout = 1;
//
string code_package = 4;
string code_package = 2;
//
string legal_prose =5;
string legal_prose =3;
enum CodeType {
CODE_UNDEFINED = 0;
CODE_JAVASCRIPT = 1;
CODE_SCALA = 2;
}
CodeType ctype = 6;
CodeType ctype = 4;
}
/*

View File

@ -23,6 +23,7 @@ import java.io._
import java.util.{ ArrayList, List }
import rep.app.conf.SystemProfile
import scala.util.control.Breaks._
import fastparse.utils.Base64
/**
* 负责签名和验签的工具了所有相关的功能都调用该类
@ -168,4 +169,19 @@ object SignTool {
})
r
}
def getCertByPem(pemcert: String): Certificate = {
val cf = CertificateFactory.getInstance("X.509")
val cert = cf.generateCertificate(
new ByteArrayInputStream(
Base64.Decoder(pemcert.replaceAll("\r\n", "").stripPrefix("-----BEGIN CERTIFICATE-----").stripSuffix("-----END CERTIFICATE-----")).toByteArray))
cert
}
def getCertByFile(path:String):Certificate = {
val certF = CertificateFactory.getInstance("X.509")
val fileInputStream = new FileInputStream(path)
certF.generateCertificate(fileInputStream)
}
}

View File

@ -97,7 +97,10 @@ object PeerHelper {
if (chaincodeId == null) t
val txid = IdTool.getUUID()
val cip = new ChaincodeDeploy(timeout, spcPackage, legal_prose, ctype)
var cip = new ChaincodeDeploy(timeout)
cip = cip.withCodePackage(spcPackage)
cip = cip.withLegalProse(legal_prose)
cip = cip.withCtype(ctype)
t = t.withId(txid)
t = t.withCid(chaincodeId)
t = t.withSpec(cip)

View File

@ -18,6 +18,10 @@ import rep.utils.SerializeUtils
/**
* @author zyf
*/
// 证书状态
case class CertStatus(credit_code: String, name: String, status: Boolean)
case class CertInfo(credit_code: String,name: String, cert: Certificate)
class ContractCert extends IContract {
implicit val formats = DefaultFormats
@ -39,9 +43,7 @@ class ContractCert extends IContract {
val UpdateCertStatus = "UpdateCertStatus"
}
// 证书状态
case class CertStatus(credit_code: String, name: String, status: Boolean)
case class CertInfo(credit_code: String,name: String, cert: Certificate)
/**
* 注册Signer账户

View File

@ -30,6 +30,7 @@ import org.json4s.DefaultFormats._
import rep.network.consensus.block.BlockHelper
import rep.crypto.cert.SignTool
import scala.collection.mutable
import rep.sc.tpl._
/**
* 用于生成创世块json文件,该json文件可以在链初始化时由节点加载
@ -42,39 +43,62 @@ object GenesisBuilder {
implicit val formats = DefaultFormats
def main(args: Array[String]): Unit = {
SignTool.loadPrivateKey("121000005L35120456.node1", "123", "jks/121000005L35120456.node1.jks")
SignTool.loadPrivateKey("121000005l35120456.node1", "123", "jks/121000005l35120456.node1.jks")
SignTool.loadNodeCertList("changeme", "jks/mytruststore.jks")
SignTool.loadPrivateKey("951002007L78123233.super_admin", "super_admin", "jks/951002007L78123233.super_admin.jks")
SignTool.loadPrivateKey("951002007l78123233.super_admin", "super_admin", "jks/951002007l78123233.super_admin.jks")
val sysName = "121000005l35120456.node1"
//交易发起人是超级管理员
//增加scala的资产管理合约
// read deploy funcs
val s1 = scala.io.Source.fromFile("src/main/scala/ContractAssetsTPL.scala")
val s1 = scala.io.Source.fromFile("src/main/scala/rep/sc/tpl/ContractCert.scala")
val l1 = try s1.mkString finally s1.close()
//todo 运行时请定义名称
val chaincodeId = new ChaincodeId("chaincodename_1",1)
val t1 = PeerHelper.createTransaction4Deploy("super_admin", chaincodeId, l1, "",
100, rep.protos.peer.ChaincodeDeploy.CodeType.CODE_SCALA)
//val t1 = transactionCreator("super_admin", rep.protos.peer.Transaction.Type.CHAINCODE_DEPLOY,
// "", "", List(), l1, None, rep.protos.peer.ChaincodeSpec.CodeType.CODE_SCALA)
// read invoke scala contract
val s2 = scala.io.Source.fromFile("scripts/set.json")
val l2 = try s2.mkString finally s2.close()
val cid = new ChaincodeId("REPCHAINACCOUNTMGR",1)
val t2 = PeerHelper.createTransaction4Invoke("super_admin", chaincodeId, "set",Seq(l2))
var translist : Array[Transaction] = new Array[Transaction] (13)
val dep_trans = PeerHelper.createTransaction4Deploy(sysName, cid,
l1, "",5000, rep.protos.peer.ChaincodeDeploy.CodeType.CODE_SCALA)
translist(0) = dep_trans
System.out.println(Json4s.compactJson(dep_trans))
var signers : Array[Signer] = new Array[Signer](6)
signers(0) = Signer("node1","121000005l35120456","18912345678",List("node1"))
signers(1) = Signer("node2","12110107bi45jh675g","18912345678",List("node2"))
signers(2) = Signer("node3","122000002n00123567","18912345678",List("node3"))
signers(3) = Signer("node4","921000005k36123789","18912345678",List("node4"))
signers(4) = Signer("node5","921000006e0012v696","18912345678",List("node5"))
signers(5) = Signer("super_admin","951002007l78123233","18912345678",List("super_admin"))
for(i<-0 to 5){
translist(i+1) = PeerHelper.createTransaction4Invoke("951002007l78123233.super_admin", cid,
"signUpSigner", Seq(Json4s.compactJson(signers(i))))
}
for(i<-0 to 5){
//val certfile = scala.io.Source.fromFile("jks/"+signers(i).creditCode+"."+signers(i).name+".cer")
//val certstr = try certfile.mkString finally certfile.close()
val cert = SignTool.getCertByFile("jks/"+signers(i).creditCode+"."+signers(i).name+".cer")
val millis = System.currentTimeMillis()
val tmp = rep.protos.peer.Certificate(Json4s.compactJson(cert),"SHA1withECDSA",true,Option(Timestamp(millis/1000 , ((millis % 1000) * 1000000).toInt)))
val a : CertInfo = CertInfo(signers(i).creditCode,signers(i).name,tmp)
translist(i+7) = PeerHelper.createTransaction4Invoke("951002007l78123233.super_admin", cid,
"signUpSigner", Seq(Json4s.compactJson(a)))
}
//val t2 = transactionCreator("super_admin",rep.protos.peer.Transaction.Type.CHAINCODE_INVOKE,
// "", "set" ,Seq(l2),"", Option(t1.payload.get.chaincodeID.get.name))
//create gensis block
val millis = ConfigFactory.load().getLong("akka.genesisblock.creationBlockTime")
var blk = new Block(1, 1,
List(t1,t2),
Seq(),
_root_.com.google.protobuf.ByteString.EMPTY, _root_.com.google.protobuf.ByteString.EMPTY)
var blk = new Block(1,1,translist,Seq(),_root_.com.google.protobuf.ByteString.EMPTY,
_root_.com.google.protobuf.ByteString.EMPTY)
//获得管理员证书和签名
// val (priKA, pubKA, certA) = ECDSASign.getKeyPair("super_admin")
// val (prik, pubK, cert) = ECDSASign.getKeyPair("1")
@ -83,8 +107,9 @@ object GenesisBuilder {
//val blk_hash = Sha256.hash(blk.toByteArray)
//超级管理员背书角色
//创建者背书1
blk = blk.withEndorsements(Seq(BlockHelper.endorseBlock4NonHash(blk_hash,"super_admin"),
BlockHelper.endorseBlock4NonHash(blk_hash,"1")))
blk = blk.withEndorsements(Seq(
BlockHelper.endorseBlock4NonHash(blk_hash,"951002007l78123233.super_admin"),
BlockHelper.endorseBlock4NonHash(blk_hash,"121000005l35120456.node1")))
// blk = blk.withConsensusMetadata(Seq(Endorsement(ByteString.copyFromUtf8(ECDSASign.getBitcoinAddrByCert(certA)),
// ByteString.copyFrom(ECDSASign.sign(priKA, blk_hash))),
// Endorsement(ByteString.copyFromUtf8(ECDSASign.getBitcoinAddrByCert(cert)),ByteString.copyFrom(ECDSASign.sign(prik,blk_hash)))))
@ -92,6 +117,7 @@ object GenesisBuilder {
val r = JsonFormat.toJson(blk)
val rstr = pretty(render(r))
println(rstr)
// println(Json4s.compactJson(blk))
val blk2 = JsonFormat.fromJsonString[Block](rstr)
val t = blk2.transactions.head
// println(t.cert.toStringUtf8)