Skip to content

Commit b2f5a97

Browse files
feat: add and update TxMetadata examples
1 parent 4176624 commit b2f5a97

File tree

2 files changed

+211
-7
lines changed

2 files changed

+211
-7
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/**
2+
* Copyright (c) 2022-present, Dash Core Group
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
package dashj.org.platform
8+
9+
import org.bitcoinj.params.TestNet3Params
10+
import org.bitcoinj.wallet.AuthenticationKeyChain
11+
import org.bitcoinj.wallet.DerivationPathFactory
12+
import org.bitcoinj.wallet.DeterministicKeyChain
13+
import org.bitcoinj.wallet.DeterministicSeed
14+
import org.bitcoinj.wallet.KeyChainGroup
15+
import org.bitcoinj.wallet.Wallet
16+
import org.bitcoinj.wallet.authentication.AuthenticationGroupExtension
17+
import org.dashj.platform.contracts.wallet.TxMetadataDocument
18+
import org.dashj.platform.wallet.TxMetadataItem
19+
import org.dashj.platform.dashpay.BlockchainIdentity
20+
import org.dashj.platform.dpp.util.Converters
21+
import org.dashj.platform.dpp.util.Entropy
22+
import org.dashj.platform.sdk.Client
23+
import org.dashj.platform.sdk.client.ClientOptions
24+
import org.dashj.platform.sdk.client.WalletOptions
25+
import org.dashj.platform.sdk.dashsdk
26+
import org.dashj.platform.sdk.platform.Platform
27+
import org.dashj.platform.wallet.TxMetadata
28+
import org.json.JSONObject
29+
import java.util.*
30+
31+
object CreateTxMetadataTest {
32+
33+
val platform = Platform(TestNet3Params.get())
34+
//val platform = Platform(MainNetParams.get())
35+
val txMetadata = TxMetadata(platform)
36+
val seed = "caution kitchen month seven humble olympic author session dutch direct decrease moment"
37+
// val seed = "half sample spirit edit dawn humor eight refuse hundred suit critic print"
38+
val wallet: Wallet = Wallet(
39+
platform.params,
40+
KeyChainGroup.builder(platform.params)
41+
.addChain(
42+
DeterministicKeyChain.builder()
43+
.accountPath(DerivationPathFactory.get(platform.params).bip44DerivationPath(0))
44+
.seed(DeterministicSeed(seed, null, "", Date().time))
45+
.build()
46+
)
47+
.build()
48+
)
49+
val authenticationGroupExtension = AuthenticationGroupExtension(platform.params)
50+
val blockchainIdentity = BlockchainIdentity(platform, 0, wallet, authenticationGroupExtension)
51+
52+
val txMetadataItem = TxMetadataItem(
53+
Entropy.generateRandomBytes(32),
54+
System.currentTimeMillis() / 1000,
55+
"Alice's Pizza Party",
56+
51.00,
57+
"USD",
58+
"expense",
59+
null
60+
)
61+
62+
val txMetadataItemTwo = TxMetadataItem(
63+
Converters.fromHex("c44d1077cd4628d0ac06e22032a4e8458f9d01be6342453de3eef88657b193ce"),
64+
System.currentTimeMillis() / 1000,
65+
"Bob's Burger Joint",
66+
52.23,
67+
"USD",
68+
"expense",
69+
"DashDirect"
70+
)
71+
72+
val txMetadataItemThree = TxMetadataItem(
73+
Entropy.generateRandomBytes(32),
74+
System.currentTimeMillis() / 1000,
75+
null,
76+
52.23,
77+
"USD",
78+
"expense"
79+
)
80+
81+
val txMetadataItemFour = TxMetadataItem(
82+
Entropy.generateRandomBytes(32),
83+
System.currentTimeMillis() / 1000,
84+
"Bob's Burger Joint",
85+
52.23,
86+
"USD",
87+
"expense",
88+
"DashDirect",
89+
customIconUrl = "https://storage.googleapis.com/mash-potato-fireplace.appspot.com/alhi323/alhi/alhirafiyun.png",
90+
giftCardNumber = "123456-7899-abcdef",
91+
giftCardPin = "1234",
92+
merchantName = "1-800-HotSoup",
93+
originalPrice = 54.99,
94+
barcodeValue = "3249u3ou234238403820820840238082304823047234098234023802384",
95+
barcodeFormat = "EAN_8",
96+
merchantUrl = "https://1-800-hotsoup.com"
97+
)
98+
val txMetadataItems = listOf(txMetadataItem, txMetadataItemTwo, txMetadataItemThree, txMetadataItemFour)
99+
init {
100+
authenticationGroupExtension.addKeyChains(
101+
platform.params, wallet.keyChainSeed,
102+
EnumSet.of(
103+
AuthenticationKeyChain.KeyChainType.BLOCKCHAIN_IDENTITY,
104+
AuthenticationKeyChain.KeyChainType.BLOCKCHAIN_IDENTITY_FUNDING,
105+
AuthenticationKeyChain.KeyChainType.BLOCKCHAIN_IDENTITY_TOPUP,
106+
AuthenticationKeyChain.KeyChainType.INVITATION_FUNDING
107+
)
108+
)
109+
wallet.addExtension(authenticationGroupExtension)
110+
111+
println("initializing platform")
112+
platform.useValidNodes()
113+
if (blockchainIdentity.recoverIdentity(authenticationGroupExtension.identityKeyChain.getKey(0).pubKeyHash)) {
114+
println("recovered blockchain identity: ${blockchainIdentity.uniqueIdString}")
115+
} else {
116+
error("recovery failed to find identity")
117+
}
118+
}
119+
120+
121+
122+
lateinit var client: Client
123+
124+
@JvmStatic
125+
fun main(args: Array<String>) {
126+
println("CreateTxMetadataTest")
127+
client = Client(ClientOptions(network = "testnet"))
128+
client.platform.useValidNodes()
129+
runTests(args.size >= 2 && args[1] == "true")
130+
}
131+
132+
fun publishTest() {
133+
val balanceBefore = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
134+
blockchainIdentity.publishTxMetaData(txMetadataItems, null, 1, TxMetadataDocument.VERSION_PROTOBUF)
135+
val balanceAfter = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
136+
println("before: $balanceBefore")
137+
println("after: $balanceAfter")
138+
}
139+
140+
private fun publishTransactions(txMetadata: TxMetadataItem, count: Int) {
141+
val txMetadataList = arrayListOf<TxMetadataItem>()
142+
for (i in IntRange(0, count)) {
143+
txMetadataList.add(txMetadata)
144+
}
145+
val balanceBefore = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
146+
blockchainIdentity.publishTxMetaData(txMetadataList, null, 1, TxMetadataDocument.VERSION_PROTOBUF)
147+
val balanceAfter = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
148+
println("$count tx before: $balanceBefore")
149+
println("$count tx after: $balanceAfter")
150+
}
151+
152+
fun publishTxsTest() {
153+
154+
var balanceBefore = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
155+
blockchainIdentity.publishTxMetaData(listOf(txMetadataItemThree), null, 1, TxMetadataDocument.VERSION_PROTOBUF)
156+
var balanceAfter = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
157+
println("1 tx before: $balanceBefore")
158+
println("1 tx after: $balanceAfter")
159+
balanceBefore = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
160+
blockchainIdentity.publishTxMetaData(listOf(txMetadataItemThree, txMetadataItemThree), null, 1, TxMetadataDocument.VERSION_PROTOBUF)
161+
balanceAfter = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
162+
println("2 tx before: $balanceBefore")
163+
println("2 tx after: $balanceAfter")
164+
balanceBefore = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
165+
blockchainIdentity.publishTxMetaData(listOf(txMetadataItemThree, txMetadataItemThree, txMetadataItemThree), null, 1, TxMetadataDocument.VERSION_PROTOBUF)
166+
balanceAfter = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
167+
println("3 tx before: $balanceBefore")
168+
println("3 tx after: $balanceAfter")
169+
// balanceBefore = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
170+
// blockchainIdentity.publishTxMetaData(listOf(txMetadataItemFour), null, 1, TxMetadataDocument.VERSION_PROTOBUF)
171+
// balanceAfter = dashsdk.platformMobileFetchIdentityFetchIdentityBalanceWithSdk(platform.rustSdk, blockchainIdentity.uniqueIdentifier.toNative())
172+
// println("1 big tx before: $balanceBefore")
173+
// println("1 big tx after: $balanceAfter")
174+
175+
publishTransactions(txMetadataItemThree, 10)
176+
publishTransactions(txMetadataItemThree, 20)
177+
publishTransactions(txMetadataItemThree, 39)
178+
}
179+
180+
private fun runTests(showOnly: Boolean) {
181+
//val blockchainIdentity = BlockchainIdentity(client.platform, 0, client.wallet!!, client.authenticationExtension)
182+
//blockchainIdentity.recoverIdentity()
183+
val identity = blockchainIdentity.identity!!
184+
185+
publishTest()
186+
publishTxsTest()
187+
188+
val documents = txMetadata.get(identity.id)
189+
190+
println("Tx Metadata: -----------------------------------")
191+
for (doc in documents) {
192+
val txDoc = TxMetadataDocument(doc)
193+
if (txDoc.encryptedMetadata[0] != 0.toByte()) {
194+
println(JSONObject(doc.toJSON()).toString(2))
195+
val txList = blockchainIdentity.decryptTxMetadata(txDoc, null)
196+
println(" $txList")
197+
}
198+
}
199+
}
200+
}

examples/src/main/kotlin/dashj/org/platform/DisplayTxMetadata.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ class DisplayTxMetadata {
2626
println("Usage: DisplayTxMetadata network")
2727
return
2828
}
29-
println("Enter a recovery phrase: ")
30-
val scanner = Scanner(System.`in`)
31-
val phrase = scanner.nextLine()
32-
33-
val recoveryPhrase = if (phrase == "default") { DefaultIdentity(args[0]).seed } else phrase
29+
val phrase = if (args.size == 1) {
30+
println("Enter a recovery phrase: ")
31+
val scanner = Scanner(System.`in`)
32+
scanner.nextLine()
33+
} else {
34+
args[1]
35+
}
36+
val recoveryPhrase = if (phrase == "default") {
37+
DefaultIdentity(args[0]).seed
38+
} else phrase
3439
client = Client(ClientOptions(network = args[0], walletOptions = WalletOptions(recoveryPhrase)))
3540
client.platform.useValidNodes()
3641
displayDocuments()
@@ -47,12 +52,11 @@ class DisplayTxMetadata {
4752

4853
println("Tx Metadata for ${identity.id}: -----------------------------------")
4954
for (doc in documents) {
50-
println("new document: ${doc.id}; createdAt ${Date(doc.createdAt!!)}")
55+
println("new document: ${doc.id}; updatedAt ${Date(doc.updatedAt!!)}")
5156
val txDoc = TxMetadataDocument(doc)
5257
try {
5358
println("txDoc ${txDoc.encryptedMetadata.toBase64()}")
5459
val txList = blockchainIdentity.decryptTxMetadata(txDoc, null)
55-
// println(" $txList")
5660
for (txmd in txList) {
5761
println("* $txmd")
5862
}

0 commit comments

Comments
 (0)