Skip to content

Commit b590753

Browse files
feat: update TxMetadata to use protobuf storage
1 parent 7362d3d commit b590753

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

dpp/src/main/java/org/dashj/platform/dashpay/BlockchainIdentity.kt

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,17 +2249,26 @@ class BlockchainIdentity {
22492249
return "assetlocktx=$txid&pk=$wif&du=${currentUsername!!}&islock=${cftx.confidence.instantSendlock.toStringHex()}"
22502250
}
22512251

2252-
// Transaction Metadata Methods
22532252
@Throws(KeyCrypterException::class)
2254-
fun publishTxMetaData(txMetadataItems: List<TxMetadataItem>, keyParameter: KeyParameter?, keyIndex: Int, version: Int) {
2253+
fun createTxMetadata(txMetadataItems: List<TxMetadataItem>, keyParameter: KeyParameter?, encryptionKeyIndex: Int, version: Int): List<Document> {
22552254
if (!platform.hasApp("wallet-utils")) {
2256-
return
2255+
return arrayListOf()
22572256
}
2258-
val encryptionKeyIndex = 0
2259-
val encryptionKey = privateKeyAtPath(keyIndex, TxMetadataDocument.childNumber, encryptionKeyIndex, KeyType.ECDSA_SECP256K1, keyParameter)
2257+
val encryptionIdentityPublicKey = identity!!.getFirstPublicKey(Purpose.ENCRYPTION, SecurityLevel.MEDIUM)
2258+
?: identity!!.getFirstPublicKey(Purpose.AUTHENTICATION, SecurityLevel.HIGH)
2259+
?: error("can't find a authentication public key with HIGH security level or encryption key")
2260+
val keyIndex = encryptionIdentityPublicKey.id
2261+
val encryptionKey = privateKeyAtPath(
2262+
keyIndex,
2263+
TxMetadataDocument.childNumber,
2264+
encryptionKeyIndex,
2265+
KeyType.ECDSA_SECP256K1,
2266+
keyParameter
2267+
)
22602268

22612269
var lastItem: TxMetadataItem? = null
22622270
var currentIndex = 0
2271+
val result = arrayListOf<Document>()
22632272
log.info("publish ${txMetadataItems.size} by breaking it up into pieces")
22642273
while (currentIndex < txMetadataItems.size) {
22652274
var estimatedDocSize = 0
@@ -2291,16 +2300,33 @@ class BlockchainIdentity {
22912300
allEncryptedData[0] = version.toByte()
22922301
encryptedData.initialisationVector.copyInto(allEncryptedData, 1)
22932302
encryptedData.encryptedBytes.copyInto(allEncryptedData, encryptedData.initialisationVector.size + 1)
2294-
txMetadata.create(
2303+
val txMetadataDocument = txMetadata.createDocument(
22952304
keyIndex,
22962305
encryptionKeyIndex,
22972306
allEncryptedData,
22982307
identity!!,
2299-
KeyIndexPurpose.AUTHENTICATION.ordinal,
2300-
WalletSignerCallback(wallet!!, keyParameter)
23012308
)
2309+
result.add(txMetadataDocument)
23022310
currentMetadataItems.clear()
23032311
}
2312+
return result
2313+
}
2314+
2315+
// Transaction Metadata Methods
2316+
@Throws(KeyCrypterException::class)
2317+
fun publishTxMetaData(txMetadataItems: List<TxMetadataItem>, keyParameter: KeyParameter?, encryptionKeyIndex: Int, version: Int) {
2318+
if (!platform.hasApp("wallet-utils")) {
2319+
return
2320+
}
2321+
val documentsToPublish = createTxMetadata(txMetadataItems, keyParameter, encryptionKeyIndex, version)
2322+
val txMetadata = TxMetadata(platform)
2323+
documentsToPublish.forEach { txMetadataDocument ->
2324+
txMetadata.publish(
2325+
txMetadataDocument,
2326+
identity!!,
2327+
WalletSignerCallback(wallet!!, keyParameter)
2328+
)
2329+
}
23042330
}
23052331

23062332
fun getTxMetaData(createdAfter: Long = -1, keyParameter: KeyParameter?): Map<TxMetadataDocument, List<TxMetadataItem>> {
@@ -2319,6 +2345,7 @@ class BlockchainIdentity {
23192345

23202346
@Throws(KeyCrypterException::class)
23212347
fun decryptTxMetadata(txMetadataDocument: TxMetadataDocument, keyParameter: KeyParameter?): List<TxMetadataItem> {
2348+
checkArgument(wallet!!.isEncrypted == (keyParameter != null), "isEncrypted must match keyParameter")
23222349
val cipher = KeyCrypterAESCBC()
23232350
val encryptionKey = privateKeyAtPath(
23242351
txMetadataDocument.keyIndex,

dpp/src/main/java/org/dashj/platform/wallet/TxMetadata.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ class TxMetadata(
6666
return Document(domain, profileDocument.dataContractId!!)
6767
}
6868

69+
fun publish(
70+
txMetadataDocument: Document,
71+
identity: Identity,
72+
signer: Signer
73+
): Document {
74+
val highIdentityPublicKey = identity.getFirstPublicKey(Purpose.AUTHENTICATION, SecurityLevel.HIGH)
75+
?: error("can't find a public key with HIGH security level")
76+
77+
val documentResult = dashsdk.platformMobilePutPutDocumentSdk(
78+
platform.rustSdk,
79+
txMetadataDocument.toNative(),
80+
txMetadataDocument.dataContractId!!.toNative(),
81+
txMetadataDocument.type,
82+
highIdentityPublicKey.toNative(),
83+
BlockHeight(10000),
84+
CoreBlockHeight(platform.coreBlockHeight),
85+
signer.nativeContext,
86+
BigInteger.valueOf(signer.signerCallback),
87+
)
88+
val domain = documentResult.unwrap()
89+
90+
return Document(domain, txMetadataDocument.dataContractId!!)
91+
}
92+
6993
fun createDocument(
7094
keyIndex: Int,
7195
encryptionKeyIndex: Int,

dpp/src/main/proto/wallet-utils.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ message TxMetadataItem {
1818
optional string barcodeValue = 13;
1919
optional string barcodeFormat = 14;
2020
optional string merchantUrl = 15;
21-
map<string, string> otherData = 20;
21+
map<string, string> otherData = 16;
2222
}
2323

2424
message TxMetadataBatch {

0 commit comments

Comments
 (0)