11package io.arcblock.forge
22
33import com.google.common.io.BaseEncoding
4- import com.google.protobuf.ByteString
54import forge_abi.Rpc
6- import forge_abi.Type
7- import io.arcblock.forge.did.HashType
8- import io.arcblock.forge.did.KeyType
9- import io.arcblock.forge.sign.Signer
105import org.spongycastle.asn1.ASN1Integer
116import org.spongycastle.asn1.DERSequenceGenerator
127import org.spongycastle.crypto.ec.CustomNamedCurves
@@ -16,15 +11,6 @@ import java.io.ByteArrayOutputStream
1611
1712private val CURVE_PARAMS = CustomNamedCurves .getByName(" secp256k1" )
1813
19- /* *
20- * binary to hex string
21- */
22- fun ByteArray.toHexString () = asUByteArray().joinToString(" " ) { it.toString(16 ).padStart(2 , ' 0' ) }
23-
24- /* *
25- * base16 string to binary
26- */
27- fun String.deBase16 () = BaseEncoding .base16().decode(this )
2814
2915/* *
3016 * ECDSAs signature to DER
@@ -53,63 +39,11 @@ fun ECKeyPair.getPK(): ByteArray {
5339 * Simple way to sent a transaction.
5440 * avoid to send by a token.
5541 */
56- fun ForgeSDK.sendTx (tx : Type .Transaction ): Rpc .ResponseSendTx {
57- return sendTx(Rpc .RequestSendTx .newBuilder().setTx(tx).build())
58- }
59-
60- /* *
61- * add delegatee ,must before sign
62- */
63- fun Type.Transaction.delegatee (delegatee : String? ) = delegatee?.let {
64- val from = this .from
65- this .toBuilder().setFrom(delegatee)
66- .setDelegator(from).clearSignature().build()
67- } ? : this
6842
6943
70- /* *
71- * base58btc address to DID
72- */
73- fun String.addrToDID (): String {
74- if (this .startsWith(" did:abt:z" )) {
75- return this
76- } else return " did:abt:" .plus(this )
77- }
7844
79- /* *
80- * DID to base58btc address
81- */
82- fun String.didToAddr (): String {
83- return this .removePrefix(" did:abt:" )
84- }
8545
86- /* *
87- * Hash extension for bytes
88- */
89- fun ByteArray.hash (type : HashType ) = Hasher .hash(type, this )
90-
91- /* *
92- * ED25519 Sign extension for bytes
93- */
94- fun ByteArray.sign (sk : ByteArray ) = Signer .sign(KeyType .ED25519 , this , sk)
95-
96- /* *
97- * Sign extension for bytes
98- */
99- fun ByteArray.sign (sk : ByteArray , type : KeyType ) = Signer .sign(type, this , sk)
100-
101- /* *
102- * Bytes to byteString extension
103- */
104- fun ByteArray.toByteString () = ByteString .copyFrom(this )
46+ data class Result (val response : Rpc .ResponseSendTx , val address : String )
10547
106- /* *
107- * Extension of transaction for signature
108- */
109- fun Type.Transaction.signTx (sk : ByteArray ): Type .Transaction {
110- val sig = this .toBuilder().clearSignature().build().toByteArray().hash(HashType .SHA3 ).sign(sk)
111- return this .toBuilder().setSignature(sig.toByteString()).build()
112- }
11348
114- data class Result (val response : Rpc .ResponseSendTx , val address : String )
11549
0 commit comments