1+ package jvmMain.kotlin
2+
3+ import kotlinx.coroutines.delay
4+ import kotlinx.coroutines.runBlocking
5+ import net.i2p.crypto.eddsa.EdDSAPrivateKey
6+ import net.i2p.crypto.eddsa.EdDSAPublicKey
7+ import one.mixin.bot.HttpClient
8+ import one.mixin.bot.SessionToken
9+ import one.mixin.bot.extension.base64Decode
10+ import one.mixin.bot.extension.base64Encode
11+ import one.mixin.bot.util.calculateAgreement
12+ import one.mixin.bot.util.decryASEKey
13+ import one.mixin.bot.util.generateEd25519KeyPair
14+ import one.mixin.bot.util.getEdDSAPrivateKeyFromString
15+
16+ fun main () = runBlocking {
17+ val key = getEdDSAPrivateKeyFromString(Config .privateKey)
18+ val pinToken = decryASEKey(Config .pinTokenPem, key) ? : return @runBlocking
19+ val client =
20+ HttpClient .Builder ().useCNServer().configEdDSA(Config .userId, Config .sessionId, key).build()
21+
22+ // create alice keys
23+ val aliceSessionKey = generateEd25519KeyPair()
24+ val alicePublicKey = aliceSessionKey.public as EdDSAPublicKey
25+ val aliceSessionSecret = alicePublicKey.abyte.base64Encode()
26+
27+ // create alice
28+ val alice = createUser(client, aliceSessionSecret)
29+ alice ? : return @runBlocking
30+ val aliceToken = SessionToken .EdDSA (
31+ alice.userId, alice.sessionId,
32+ (aliceSessionKey.private as EdDSAPrivateKey ).seed.base64Encode()
33+ )
34+ client.setUserToken(aliceToken)
35+ // decrypt pin token
36+ val alicePrivateKey = aliceSessionKey.private as EdDSAPrivateKey
37+ val aliceAesKey = calculateAgreement(alice.pinToken.base64Decode(), alicePrivateKey).base64Encode()
38+ // create alice's pin
39+ createPin(client, aliceAesKey)
40+
41+ // use bot's token
42+ client.setUserToken(null )
43+ // create bob keys
44+ val bobSessionKey = generateEd25519KeyPair()
45+ val bobPublicKey = bobSessionKey.public as EdDSAPublicKey
46+ val bobSessionSecret = bobPublicKey.abyte.base64Encode()
47+
48+ // create bob
49+ val bob = createUser(client, bobSessionSecret)
50+ bob ? : return @runBlocking
51+ val bobToken = SessionToken .EdDSA (
52+ bob.userId, bob.sessionId,
53+ (bobSessionKey.private as EdDSAPrivateKey ).seed.base64Encode()
54+ )
55+ client.setUserToken(bobToken)
56+ // decrypt pin token
57+ val bobPrivateKey = bobSessionKey.private as EdDSAPrivateKey
58+ val bobAesKey = calculateAgreement(bob.pinToken.base64Decode(), bobPrivateKey).base64Encode()
59+ // create bob's pin
60+ createPin(client, bobAesKey)
61+
62+ // use bot's token
63+ client.setUserToken(null )
64+ // bot transfer to alice
65+ val snapshotBot2Alice = transferToUser(client, alice.userId, pinToken, Config .pin)
66+
67+ delay(4000 )
68+
69+ // use alice's token
70+ client.setUserToken(aliceToken)
71+ if (snapshotBot2Alice != null ) {
72+ // alice check transfer
73+ networkSnapshot(client, snapshotBot2Alice.snapshotId)
74+
75+ val aliceNetworkSnapshots = networkSnapshots(client, CNB_ID , limit = 5 )
76+ assert (aliceNetworkSnapshots?.find { it.snapshotId == snapshotBot2Alice.snapshotId } != null )
77+ }
78+
79+ // alice transfer to bob
80+ val snapshotAlice2Bob = transferToUser(client, bob.userId, aliceAesKey, DEFAULT_PIN )
81+
82+ delay(4000 )
83+
84+ // use bob's token
85+ client.setUserToken(bobToken)
86+ if (snapshotAlice2Bob != null ) {
87+ networkSnapshot(client, snapshotAlice2Bob.snapshotId)
88+
89+ val bobNetworkSnapshots = networkSnapshots(client, CNB_ID , limit = 5 )
90+ assert (bobNetworkSnapshots?.find { it.snapshotId == snapshotAlice2Bob.snapshotId } != null )
91+ }
92+
93+ }
0 commit comments