操作指南
网关接入说明
集成Corda相关依赖
用户需在项目中依赖corda-node、corda-rpc等相关jar以及您开发的dapp.jar。例如:
dependencies {
// 引入SDK
implementation "$corda_release_group:corda-rpc:$corda_release_version"
implementation("$corda_release_group:corda-node:$corda_release_version")
// 自己开发的SDK
compile "com.slabs:slabs-user_dapp_test:1.0-release"
compile "org.jetbrains.kotlin:kotlin-stdlib"
compile("org.springframework.boot:spring-boot-starter-web"){
exclude group: "org.springframework.boot", module: "spring-boot-starter-logging"
}
testImplementation("org.springframework.boot:spring-boot-starter-test")
testCompile "junit:junit:$junit_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
使用SDK调用DAPP
用户需配置链账户地址、链账户私钥、网关证书、网关证书密码、网关地址以及网关端口进行DAPP的调用。
- 链账户地址:用户创建链账户成功后,系统自动返回链账户地址,并在链账户管理页面的链账户列表中展示。
- 网关证书:在链账户管理页面—链账户列表中,点击操作列的【查看】按钮,进入链账户详情页面,点击链账户类型右侧的【下载】按钮,即可将网关证书下载到本地。
网关证书密码:用户下载网关证书后,可以从证书中查看网关证书密码。
网关地址及端口:在项目管理页面—项目列表中,点击操作列的【下载接入参数】按钮,即可将接入参数下载到本地,用户可以从接入参数配置中查看网关地址及端口。
例如我们搭建了一个springboot项目,在application.yml文件中添加了如下配置:
config:
rpc:
username: 840a3cdd37a1493ab00951769f5d030e_O=NodeA,L=Beijing,C=CN
privateKeyString: "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQggDYiW40NMcB45YI6VLqy83uPip8k6LlxEjSItvqU4wWgCgYIKoZIzj0DAQehRANCAATbKdwykGqYPmT3n1IruMXz0jYbSKcHkFHam5q4cFdS1UNUAUrVO89+UMQRgPAMbfB1QL9qqu4DHZaP5LZfvKJv"
host: ccn-relayer.bsnbase.com
port: 9001
sslpath: C:\Users\Administrator\Desktop\rpcssltruststore.jks
sslpwd: cordauser
然后我们构建了一个rpc连接,每次调用dapp之前需要连接到节点。
private const val CORDA_USER_NAME = "config.rpc.username"
private const val CORDA_USER_PRIVATEKEY = "config.rpc.privateKeyString"
private const val CORDA_NODE_HOST = "config.rpc.host"
private const val CORDA_RPC_PORT = "config.rpc.port"
private const val CORDA_SSL_PATH = "config.rpc.sslpath"
private const val CORDA_SSL_PWD = "config.rpc.sslpwd"
@Component
open class NodeRPCConnection(
@Value("\${$CORDA_NODE_HOST}") private val host: String,
@Value("\${$CORDA_USER_NAME}") private val username: String,
@Value("\${$CORDA_USER_PRIVATEKEY}") private val privateKey: String,
@Value("\${$CORDA_RPC_PORT}") private val rpcPort: Int,
@Value("\${$CORDA_SSL_PATH}") private val sslPath: String,
@Value("\${$CORDA_SSL_PWD}") private val sslPwd: String,
) : AutoCloseable {
lateinit var rpcConnection: CordaRPCConnection
private set
lateinit var proxy: CordaRPCOps
private set
@PostConstruct
fun initialiseNodeRPCConnection() {
val rpcAddress = NetworkHostAndPort(host, rpcPort)
val toPath = File(sslPath).toPath()
val clientSslOptions = ClientRpcSslOptions(toPath, sslPwd)
val rpcClient = CordaRPCClient(rpcAddress,sslConfiguration = clientSslOptions)
val decode: ByteArray = Base64.getDecoder().decode(privateKey)
val privateKey = decodePrivateKey(decode)
// step 1 获取当前时间戳
val current = System.currentTimeMillis()
// step 2 拼接签名内容
val signString = "$username&$current"
// step 3 签名
val bytes = doSign(privateKey, signString.toByteArray(StandardCharsets.UTF_8))
// step 4 将签名内容base64和时间戳拼接成password
val connectPwd = current.toString() + "&" + Base64.getEncoder().encodeToString(bytes)
val rpcConnection = rpcClient.start(username, connectPwd)
proxy = rpcConnection.proxy
}
@PreDestroy
override fun close() {
rpcConnection.notifyServerAndClose()
}
}
最后我们通过创建好的rpc连接访问我们的dapp。
@Service
class UpHashService(
@Autowired private val nodeRPCConnection: NodeRPCConnection
) {
fun upHash(hash: String): String {
return nodeRPCConnection.proxy.startFlowDynamic(
UpHashFlow::class.java,
hash).returnValue.getOrThrow(Duration.ofSeconds(30))
}
}