-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathhandle.test.ts
222 lines (205 loc) · 7.71 KB
/
handle.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* eslint-disable sonarjs/no-duplicate-string */
import { BaseWallet } from '@cardano-sdk/wallet';
import { Cardano, metadatum } from '@cardano-sdk/core';
import { KeyAgent, KeyPurpose, TransactionSigner } from '@cardano-sdk/key-management';
import {
bip32Ed25519Factory,
burnTokens,
coinsRequiredByHandleMint,
createHandleMetadata,
createHandlePolicy,
createStandaloneKeyAgent,
getEnv,
getHandlePolicyId,
getWallet,
handleNames,
mint,
mintCIP25andCIP68Handles,
txConfirmed,
walletReady,
walletVariables
} from '../../../src';
import { createLogger } from '@cardano-sdk/util-dev';
import { firstValueFrom } from 'rxjs';
import path from 'path';
const env = getEnv(walletVariables);
const logger = createLogger();
const toHex = (value: string) =>
value
.split('')
.map((s) => s.charCodeAt(0).toString(16))
.join('');
describe('Ada handle', () => {
let wallet: BaseWallet;
let keyAgent: KeyAgent;
let receivingWallet: BaseWallet;
let policySigner: TransactionSigner;
let policyId: Cardano.PolicyId;
let policyScript: Cardano.NativeScript;
let cip25AssetIds: Cardano.AssetId[];
const coins = coinsRequiredByHandleMint + 10_000_000n; // maximum number of coins to use in each transaction
const initPolicyId = async () => {
wallet = (await getWallet({ env, idx: 0, logger, name: 'Handle Init Wallet' })).wallet;
await walletReady(wallet, coins);
keyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
await firstValueFrom(wallet.genesisParameters$),
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger),
KeyPurpose.STANDARD
);
({ policyScript, policySigner, policyId } = await createHandlePolicy(keyAgent));
const handleProviderPolicyId = await getHandlePolicyId(
path.join(__dirname, '..', '..', '..', 'local-network', 'sdk-ipc')
);
expect(policyId).toEqual(handleProviderPolicyId);
wallet.shutdown();
};
const restartWallet = async () => {
wallet.shutdown();
wallet = (
await getWallet({
env,
handlePolicyIds: [policyId],
idx: 0,
logger,
name: 'Minting Wallet',
polling: { interval: 50 }
})
).wallet;
await walletReady(wallet, coins);
};
beforeAll(async () => {
await initPolicyId();
wallet = (
await getWallet({
env,
handlePolicyIds: [policyId],
idx: 0,
logger,
name: 'Minting Wallet',
polling: { interval: 50 }
})
).wallet;
receivingWallet = (
await getWallet({
env,
handlePolicyIds: [policyId],
idx: 1,
logger,
name: 'Receiving Wallet',
polling: { interval: 50 }
})
).wallet;
await Promise.all([walletReady(wallet, coins), walletReady(receivingWallet, 0n)]);
cip25AssetIds = [
Cardano.AssetId(`${policyId}${toHex(handleNames[0])}`),
Cardano.AssetId(`${policyId}${toHex(handleNames[1])}`)
];
});
afterEach(async () => {
await burnTokens({
policySigners: [policySigner],
scripts: [policyScript],
wallet
});
await burnTokens({
policySigners: [policySigner],
scripts: [policyScript],
wallet: receivingWallet
});
});
afterAll(async () => {
wallet.shutdown();
receivingWallet.shutdown();
});
// eslint-disable-next-line max-statements
it("BaseWallet discovers it's own cip25 and cip68 handles", async () => {
await mintCIP25andCIP68Handles(wallet, keyAgent, policyId);
let utxo = await firstValueFrom(wallet.balance.utxo.available$);
let receivingUtxo = await firstValueFrom(receivingWallet.balance.utxo.available$);
expect(utxo.assets?.size).toEqual(6);
expect(receivingUtxo.assets).toBeUndefined();
let handles = await firstValueFrom(wallet.handles$);
let receivingHandles = await firstValueFrom(receivingWallet.handles$);
expect(handles.length).toEqual(4);
expect(receivingHandles.length).toEqual(0);
// send handle to another wallet
const token = new Map([[cip25AssetIds[0], 1n]]);
const destAddresses = (await firstValueFrom(receivingWallet.addresses$))[0].address;
const txBuilder = wallet.createTxBuilder();
const { tx } = await txBuilder
.addOutput(await txBuilder.buildOutput().address(destAddresses).coin(coins).assets(token).build())
.build()
.sign();
await wallet.submitTx(tx);
await txConfirmed(receivingWallet, tx);
utxo = await firstValueFrom(wallet.balance.utxo.available$);
receivingUtxo = await firstValueFrom(receivingWallet.balance.utxo.available$);
expect(utxo.assets?.size).toEqual(5);
expect(receivingUtxo.assets?.size).toEqual(1);
expect(receivingUtxo.assets?.keys().next().value).toEqual(cip25AssetIds[0]);
handles = await firstValueFrom(wallet.handles$);
receivingHandles = await firstValueFrom(receivingWallet.handles$);
expect(handles.length).toEqual(3);
expect(receivingHandles.length).toEqual(1);
// send ada using handle
const txBuilder2 = wallet.createTxBuilder();
const { tx: tx2 } = await txBuilder2
.addOutput(await txBuilder2.buildOutput().handle(receivingHandles[0].handle).coin(coins).build())
.build()
.sign();
await wallet.submitTx(tx2);
await txConfirmed(receivingWallet, tx2);
const receivingUtxoAfter = await firstValueFrom(receivingWallet.balance.utxo.available$);
expect(receivingUtxoAfter.coins).toEqual(receivingUtxo.coins + coins);
});
describe('double mint handling', () => {
it('filters out double mints in separate transactions', async () => {
const tokens: Cardano.TokenMap = new Map([[cip25AssetIds[0], 1n]]);
const txMetadatum: Cardano.Metadatum = metadatum.jsonToMetadatum(
createHandleMetadata(policyId, [handleNames[0]])
);
await mint(wallet, keyAgent, tokens, txMetadatum);
let handles = await firstValueFrom(wallet.handles$);
expect(handles.length).toEqual(1);
await mint(wallet, keyAgent, tokens, txMetadatum);
await restartWallet();
const utxo = await firstValueFrom(wallet.balance.utxo.available$);
expect(utxo.assets?.values().next().value).toEqual(2n);
handles = await firstValueFrom(wallet.handles$);
expect(handles).toEqual([]);
});
it('filters out double mints from within the same transaction', async () => {
const tokens = new Map([[cip25AssetIds[0], 2n]]);
const txMetadatum: Cardano.Metadatum = metadatum.jsonToMetadatum(
createHandleMetadata(policyId, [handleNames[0]])
);
await mint(wallet, keyAgent, tokens, txMetadatum);
await restartWallet();
const utxo = await firstValueFrom(wallet.balance.utxo.available$);
expect(utxo.assets?.values().next().value).toEqual(2n);
const handles = await firstValueFrom(wallet.handles$);
expect(handles).toEqual([]);
});
it('shows handle after corrective burn', async () => {
const tokens = new Map([[cip25AssetIds[0], 2n]]);
const txMetadatum: Cardano.Metadatum = metadatum.jsonToMetadatum(
createHandleMetadata(policyId, [handleNames[0]])
);
await mint(wallet, keyAgent, tokens, txMetadatum);
let utxo = await firstValueFrom(wallet.balance.utxo.available$);
expect(utxo.assets?.values().next().value).toEqual(2n);
await burnTokens({
policySigners: [policySigner],
scripts: [policyScript],
tokens: new Map([[cip25AssetIds[0], 1n]]),
wallet
});
await restartWallet();
utxo = await firstValueFrom(wallet.balance.utxo.available$);
expect(utxo.assets?.values().next().value).toEqual(1n);
const correctedHandles = await firstValueFrom(wallet.handles$);
expect(correctedHandles.length).toEqual(1);
});
});
});