@cardano-sdk/e2e 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +1 -7
- package/CHANGELOG.md +24 -0
- package/README.md +0 -29
- package/dist/cjs/environment.d.ts.map +1 -1
- package/dist/cjs/environment.js.map +1 -1
- package/dist/cjs/factories.d.ts.map +1 -1
- package/dist/cjs/factories.js +7 -1
- package/dist/cjs/factories.js.map +1 -1
- package/dist/cjs/measurement-util.d.ts.map +1 -1
- package/dist/cjs/measurement-util.js.map +1 -1
- package/dist/cjs/scripts/is-local-network-ready.js.map +1 -1
- package/dist/cjs/scripts/mnemonic.js.map +1 -1
- package/dist/cjs/tools/multi-delegation-data-gen/utils/files.d.ts.map +1 -1
- package/dist/cjs/tools/multi-delegation-data-gen/utils/files.js.map +1 -1
- package/dist/cjs/tools/multi-delegation-data-gen/utils/terminal-progress-monitor.d.ts.map +1 -1
- package/dist/cjs/tools/multi-delegation-data-gen/utils/terminal-progress-monitor.js.map +1 -1
- package/dist/cjs/tools/multi-delegation-data-gen/utils/utils.d.ts.map +1 -1
- package/dist/cjs/tools/multi-delegation-data-gen/utils/utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/environment.d.ts.map +1 -1
- package/dist/esm/environment.js.map +1 -1
- package/dist/esm/factories.d.ts.map +1 -1
- package/dist/esm/factories.js +7 -1
- package/dist/esm/factories.js.map +1 -1
- package/dist/esm/measurement-util.d.ts.map +1 -1
- package/dist/esm/measurement-util.js.map +1 -1
- package/dist/esm/scripts/is-local-network-ready.js.map +1 -1
- package/dist/esm/scripts/mnemonic.js.map +1 -1
- package/dist/esm/tools/multi-delegation-data-gen/utils/files.d.ts.map +1 -1
- package/dist/esm/tools/multi-delegation-data-gen/utils/files.js.map +1 -1
- package/dist/esm/tools/multi-delegation-data-gen/utils/terminal-progress-monitor.d.ts.map +1 -1
- package/dist/esm/tools/multi-delegation-data-gen/utils/terminal-progress-monitor.js.map +1 -1
- package/dist/esm/tools/multi-delegation-data-gen/utils/utils.d.ts.map +1 -1
- package/dist/esm/tools/multi-delegation-data-gen/utils/utils.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/docker-compose.yml +4 -0
- package/jest.config.js +0 -1
- package/local-network/scripts/common.sh +19 -0
- package/local-network/scripts/make-babbage.sh +2 -1
- package/local-network/scripts/mint-handles.sh +2 -18
- package/local-network/scripts/mint-tokens.sh +2 -18
- package/local-network/scripts/mnemonic_keys.sh +0 -0
- package/local-network/scripts/setup-wallets.sh +2 -17
- package/local-network/templates/babbage/submit-api-config.json +115 -0
- package/package.json +27 -29
- package/src/environment.ts +2 -6
- package/src/factories.ts +11 -5
- package/src/measurement-util.ts +1 -4
- package/src/scripts/is-local-network-ready.ts +1 -3
- package/src/scripts/mnemonic.ts +1 -3
- package/src/tools/multi-delegation-data-gen/utils/files.ts +1 -3
- package/src/tools/multi-delegation-data-gen/utils/terminal-progress-monitor.ts +2 -6
- package/src/tools/multi-delegation-data-gen/utils/utils.ts +1 -3
- package/test/artillery/StakePoolSearch.ts +5 -16
- package/test/artillery/artillery.ts +20 -61
- package/test/artillery/wallet-restoration/WalletRestoration.ts +1 -3
- package/test/artillery/wallet-restoration/queries.ts +1 -3
- package/test/artillery/wallet-restoration/types.ts +1 -3
- package/test/load-test-custom/stake-pool-search/stake-pool-search.test.ts +1 -3
- package/test/load-test-custom/wallet-init/wallet-init.test.ts +10 -1
- package/test/load-test-custom/wallet-restoration/wallet-restoration.test.ts +1 -4
- package/test/local-network/register-pool.test.ts +24 -14
- package/test/long-running/cache-invalidation.test.ts +7 -4
- package/test/long-running/multisig-wallet/MultiSigTx.ts +117 -0
- package/test/long-running/multisig-wallet/MultiSigWallet.ts +491 -0
- package/test/long-running/multisig-wallet/multisig-delegation-rewards.test.ts +318 -0
- package/test/projection/offline-fork.test.ts +47 -20
- package/test/projection/single-tenant-utxo.test.ts +40 -27
- package/test/providers/StakePoolProvider.test.ts +22 -17
- package/test/tsconfig.json +3 -0
- package/test/wallet/PersonalWallet/delegation.test.ts +6 -3
- package/test/wallet/PersonalWallet/handle.test.ts +2 -1
- package/test/wallet/PersonalWallet/mint.test.ts +2 -1
- package/test/wallet/PersonalWallet/multiAddress.test.ts +2 -1
- package/test/wallet/PersonalWallet/multisignature.test.ts +4 -2
- package/test/wallet/PersonalWallet/nft.test.ts +2 -1
- package/test/web-extension/extension/ui.ts +2 -8
- package/test/load-testing/tx-submit-load.test.ts +0 -341
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
import * as Crypto from '@cardano-sdk/crypto';
|
|
2
|
+
import { AddressType, GroupedAddress, InMemoryKeyAgent, KeyRole } from '@cardano-sdk/key-management';
|
|
3
|
+
import {
|
|
4
|
+
Cardano,
|
|
5
|
+
ChainHistoryProvider,
|
|
6
|
+
Reward,
|
|
7
|
+
RewardsProvider,
|
|
8
|
+
Serialization,
|
|
9
|
+
TxSubmitProvider,
|
|
10
|
+
UtxoProvider,
|
|
11
|
+
coalesceValueQuantities,
|
|
12
|
+
nativeScriptPolicyId,
|
|
13
|
+
util
|
|
14
|
+
} from '@cardano-sdk/core';
|
|
15
|
+
import { InputSelector, StaticChangeAddressResolver, roundRobinRandomImprove } from '@cardano-sdk/input-selection';
|
|
16
|
+
import { MultiSigTx } from './MultiSigTx';
|
|
17
|
+
import { Observable, firstValueFrom, interval, map, switchMap } from 'rxjs';
|
|
18
|
+
import { WalletNetworkInfoProvider } from '@cardano-sdk/wallet';
|
|
19
|
+
import { defaultSelectionConstraints } from '@cardano-sdk/tx-construction';
|
|
20
|
+
|
|
21
|
+
const randomHexChar = () => Math.floor(Math.random() * 16).toString(16);
|
|
22
|
+
const randomPublicKey = () => Crypto.Ed25519PublicKeyHex(Array.from({ length: 64 }).map(randomHexChar).join(''));
|
|
23
|
+
|
|
24
|
+
// eslint-disable-next-line max-len
|
|
25
|
+
const DUMMY_HEX_BYTES =
|
|
26
|
+
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
|
|
27
|
+
|
|
28
|
+
const DERIVATION_PATH = {
|
|
29
|
+
index: 0,
|
|
30
|
+
role: KeyRole.External
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export class MultiSigWalletProps {
|
|
34
|
+
expectedSigners: Array<Crypto.Ed25519PublicKeyHex> = [];
|
|
35
|
+
inMemoryKeyAgent: InMemoryKeyAgent;
|
|
36
|
+
utxoProvider: UtxoProvider;
|
|
37
|
+
chainHistoryProvider: ChainHistoryProvider;
|
|
38
|
+
rewardsProvider: RewardsProvider;
|
|
39
|
+
txSubmitProvider: TxSubmitProvider;
|
|
40
|
+
networkInfoProvider: WalletNetworkInfoProvider;
|
|
41
|
+
networkId: Cardano.NetworkId;
|
|
42
|
+
pollingInterval: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Represents a multi-signature wallet for Cardano blockchain. */
|
|
46
|
+
export class MultiSigWallet {
|
|
47
|
+
#expectedSigners: Array<Crypto.Ed25519PublicKeyHex> = [];
|
|
48
|
+
#inMemoryKeyAgent: InMemoryKeyAgent;
|
|
49
|
+
#multisigScript: Cardano.NativeScript;
|
|
50
|
+
#inputSelector: InputSelector;
|
|
51
|
+
#address: GroupedAddress;
|
|
52
|
+
#utxoProvider: UtxoProvider;
|
|
53
|
+
#chainHistoryProvider: ChainHistoryProvider;
|
|
54
|
+
#rewardsProvider: RewardsProvider;
|
|
55
|
+
#txSubmitProvider: TxSubmitProvider;
|
|
56
|
+
#networkInfoProvider: WalletNetworkInfoProvider;
|
|
57
|
+
#pollingInterval: number;
|
|
58
|
+
|
|
59
|
+
/** Creates a new MultiSigWallet instance with the specified signers and network configuration. */
|
|
60
|
+
static async createMultiSigWallet(props: MultiSigWalletProps) {
|
|
61
|
+
const script = await MultiSigWallet.#buildScript(props.expectedSigners, props.inMemoryKeyAgent);
|
|
62
|
+
const address = await MultiSigWallet.#getAddress(script, props.networkId);
|
|
63
|
+
|
|
64
|
+
const inputSelector = roundRobinRandomImprove({
|
|
65
|
+
changeAddressResolver: new StaticChangeAddressResolver(() => Promise.resolve([address]))
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return new MultiSigWallet(props, script, address, inputSelector);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Constructs a new MultiSigWallet. */
|
|
72
|
+
constructor(
|
|
73
|
+
props: MultiSigWalletProps,
|
|
74
|
+
script: Cardano.NativeScript,
|
|
75
|
+
address: GroupedAddress,
|
|
76
|
+
inputSelector: InputSelector
|
|
77
|
+
) {
|
|
78
|
+
this.#multisigScript = script;
|
|
79
|
+
this.#address = address;
|
|
80
|
+
this.#inputSelector = inputSelector;
|
|
81
|
+
this.#expectedSigners = props.expectedSigners;
|
|
82
|
+
this.#inMemoryKeyAgent = props.inMemoryKeyAgent;
|
|
83
|
+
this.#utxoProvider = props.utxoProvider;
|
|
84
|
+
this.#chainHistoryProvider = props.chainHistoryProvider;
|
|
85
|
+
this.#rewardsProvider = props.rewardsProvider;
|
|
86
|
+
this.#txSubmitProvider = props.txSubmitProvider;
|
|
87
|
+
this.#networkInfoProvider = props.networkInfoProvider;
|
|
88
|
+
this.#pollingInterval = props.pollingInterval;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves the list of signers' public keys.
|
|
93
|
+
*
|
|
94
|
+
* @returns {Array<Crypto.Ed25519PublicKeyHex>} An array of signers' public keys.
|
|
95
|
+
*/
|
|
96
|
+
getSigners(): Array<Crypto.Ed25519PublicKeyHex> {
|
|
97
|
+
return this.#expectedSigners;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Retrieves the payment address of the wallet.
|
|
102
|
+
*
|
|
103
|
+
* @returns {Cardano.PaymentAddress} The payment address.
|
|
104
|
+
*/
|
|
105
|
+
getPaymentAddress(): Cardano.PaymentAddress {
|
|
106
|
+
return this.#address.address;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Retrieves the reward account associated with the wallet.
|
|
111
|
+
*
|
|
112
|
+
* @returns {Cardano.RewardAccount} The reward account.
|
|
113
|
+
*/
|
|
114
|
+
getRewardAccount(): Cardano.RewardAccount {
|
|
115
|
+
return this.#address.rewardAccount;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Delegates the stake to a specified pool.
|
|
120
|
+
*
|
|
121
|
+
* @param {Cardano.PoolId} pool - The pool ID to delegate to.
|
|
122
|
+
* @returns {Promise<MultiSigTx>} A multi-signature transaction object for the delegation.
|
|
123
|
+
*/
|
|
124
|
+
async delegate(pool: Cardano.PoolId): Promise<MultiSigTx> {
|
|
125
|
+
const certificates: Cardano.Certificate[] = [];
|
|
126
|
+
|
|
127
|
+
certificates.push(
|
|
128
|
+
{
|
|
129
|
+
__typename: Cardano.CertificateType.StakeRegistration,
|
|
130
|
+
stakeCredential: {
|
|
131
|
+
hash: Cardano.RewardAccount.toHash(this.getRewardAccount()) as unknown as Crypto.Hash28ByteBase16,
|
|
132
|
+
type: Cardano.CredentialType.ScriptHash
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
__typename: Cardano.CertificateType.StakeDelegation,
|
|
137
|
+
poolId: pool,
|
|
138
|
+
stakeCredential: {
|
|
139
|
+
hash: Cardano.RewardAccount.toHash(this.getRewardAccount()) as unknown as Crypto.Hash28ByteBase16,
|
|
140
|
+
type: Cardano.CredentialType.ScriptHash
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const { body, id } = await this.#createTransaction(
|
|
146
|
+
// Add dummy output. This is not needed, but probably ok for POC.
|
|
147
|
+
new Set<Cardano.TxOut>([
|
|
148
|
+
{
|
|
149
|
+
address: this.getPaymentAddress(),
|
|
150
|
+
value: { coins: 1_000_000n }
|
|
151
|
+
}
|
|
152
|
+
]),
|
|
153
|
+
certificates
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
return new MultiSigTx(
|
|
157
|
+
{
|
|
158
|
+
body,
|
|
159
|
+
id,
|
|
160
|
+
witness: {
|
|
161
|
+
scripts: [this.#multisigScript],
|
|
162
|
+
signatures: new Map<Crypto.Ed25519PublicKeyHex, Crypto.Ed25519SignatureHex>()
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
this.#expectedSigners
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Transfers funds to a specified address.
|
|
171
|
+
*
|
|
172
|
+
* @param {Cardano.PaymentAddress} address - The address to transfer funds to.
|
|
173
|
+
* @param {Cardano.Value} value - The amount to be transferred.
|
|
174
|
+
* @returns {Promise<MultiSigTx>} A multi-signature transaction object for the transfer.
|
|
175
|
+
*/
|
|
176
|
+
async transferFunds(address: Cardano.PaymentAddress, value: Cardano.Value): Promise<MultiSigTx> {
|
|
177
|
+
const { body, id } = await this.#createTransaction(
|
|
178
|
+
new Set<Cardano.TxOut>([
|
|
179
|
+
{
|
|
180
|
+
address,
|
|
181
|
+
value
|
|
182
|
+
}
|
|
183
|
+
])
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
return new MultiSigTx(
|
|
187
|
+
{
|
|
188
|
+
body,
|
|
189
|
+
id,
|
|
190
|
+
witness: {
|
|
191
|
+
scripts: [this.#multisigScript],
|
|
192
|
+
signatures: new Map<Crypto.Ed25519PublicKeyHex, Crypto.Ed25519SignatureHex>()
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
this.#expectedSigners
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Signs a multi-signature transaction.
|
|
201
|
+
*
|
|
202
|
+
* @param {MultiSigTx} multiSigTx - The multi-signature transaction to sign.
|
|
203
|
+
* @returns {Promise<MultiSigTx>} The signed multi-signature transaction.
|
|
204
|
+
*/
|
|
205
|
+
async sign(multiSigTx: MultiSigTx): Promise<MultiSigTx> {
|
|
206
|
+
const currentSignatures = multiSigTx.getTransaction().witness.signatures;
|
|
207
|
+
const newSignatures = await this.#inMemoryKeyAgent.signTransaction(
|
|
208
|
+
{
|
|
209
|
+
body: multiSigTx.getTransaction().body,
|
|
210
|
+
hash: multiSigTx.getTransaction().id
|
|
211
|
+
},
|
|
212
|
+
{ additionalKeyPaths: [DERIVATION_PATH] }
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
for (const signature of newSignatures.entries()) {
|
|
216
|
+
currentSignatures.set(signature[0], signature[1]);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
multiSigTx.getTransaction().witness.signatures = currentSignatures;
|
|
220
|
+
|
|
221
|
+
return multiSigTx;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Submits a signed multi-signature transaction to the network.
|
|
226
|
+
*
|
|
227
|
+
* @param {MultiSigTx} multiSigTx - The signed multi-signature transaction to submit.
|
|
228
|
+
* @returns {Promise<Cardano.TransactionId>} The transaction ID of the submitted transaction.
|
|
229
|
+
*/
|
|
230
|
+
async submit(multiSigTx: MultiSigTx): Promise<Cardano.TransactionId> {
|
|
231
|
+
const tx = Serialization.Transaction.fromCore(multiSigTx.getTransaction());
|
|
232
|
+
|
|
233
|
+
await this.#txSubmitProvider.submitTx({
|
|
234
|
+
signedTransaction: tx.toCbor()
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
return tx.getId();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Retrieves the set of unspent transaction outputs (UTXOs) associated with the wallet.
|
|
242
|
+
*
|
|
243
|
+
* @returns {Observable<Cardano.Utxo[]>} A hot observable with the list of current UTXOs.
|
|
244
|
+
*/
|
|
245
|
+
getUtxoSet(): Observable<Cardano.Utxo[]> {
|
|
246
|
+
return interval(this.#pollingInterval).pipe(
|
|
247
|
+
switchMap(
|
|
248
|
+
() =>
|
|
249
|
+
new Observable<Cardano.Utxo[]>((subscriber) => {
|
|
250
|
+
this.#utxoProvider
|
|
251
|
+
.utxoByAddresses({ addresses: [this.#address.address] })
|
|
252
|
+
// eslint-disable-next-line promise/always-return
|
|
253
|
+
.then((utxos) => {
|
|
254
|
+
subscriber.next(utxos);
|
|
255
|
+
})
|
|
256
|
+
.catch((error) => subscriber.error(error));
|
|
257
|
+
})
|
|
258
|
+
)
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Calculates and returns the total balance of the wallet.
|
|
264
|
+
*
|
|
265
|
+
* @returns {Observable<Cardano.Value>} An observable that emits the wallet's balance.
|
|
266
|
+
*/
|
|
267
|
+
getBalance(): Observable<Cardano.Value> {
|
|
268
|
+
return this.getUtxoSet().pipe(map((utxoSet) => coalesceValueQuantities(utxoSet.map((utxo) => utxo[1].value))));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Retrieves and emits the transaction history of the wallet at specified polling intervals.
|
|
273
|
+
*
|
|
274
|
+
* @returns {Observable<Cardano.HydratedTx[]>} An observable that emits the list of historical transactions.
|
|
275
|
+
*/
|
|
276
|
+
getTransactionHistory(): Observable<Cardano.HydratedTx[]> {
|
|
277
|
+
return interval(this.#pollingInterval).pipe(
|
|
278
|
+
switchMap(
|
|
279
|
+
() =>
|
|
280
|
+
new Observable<Cardano.HydratedTx[]>((subscriber) => {
|
|
281
|
+
this.#chainHistoryProvider
|
|
282
|
+
.transactionsByAddresses({
|
|
283
|
+
addresses: [this.#address.address],
|
|
284
|
+
pagination: {
|
|
285
|
+
limit: 25, // Gets only the first 25 transaction. This is probably good enough for the POC.
|
|
286
|
+
startAt: 0
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
// eslint-disable-next-line promise/always-return
|
|
290
|
+
.then((paginatedTxs) => {
|
|
291
|
+
subscriber.next(paginatedTxs.pageResults);
|
|
292
|
+
subscriber.complete();
|
|
293
|
+
})
|
|
294
|
+
.catch((error) => subscriber.error(error));
|
|
295
|
+
})
|
|
296
|
+
)
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Retrieves and emits the rewards history of the wallet's reward account at specified polling intervals.
|
|
302
|
+
*
|
|
303
|
+
* @returns {Observable<Map<Cardano.RewardAccount, Reward[]>>} An observable that emits the rewards history.
|
|
304
|
+
*/
|
|
305
|
+
getRewardsHistory(): Observable<Map<Cardano.RewardAccount, Reward[]>> {
|
|
306
|
+
return interval(this.#pollingInterval).pipe(
|
|
307
|
+
switchMap(
|
|
308
|
+
() =>
|
|
309
|
+
new Observable<Map<Cardano.RewardAccount, Reward[]>>((subscriber) => {
|
|
310
|
+
this.#rewardsProvider
|
|
311
|
+
.rewardsHistory({
|
|
312
|
+
rewardAccounts: [this.#address.rewardAccount]
|
|
313
|
+
})
|
|
314
|
+
// eslint-disable-next-line promise/always-return
|
|
315
|
+
.then((rewardsHistory) => {
|
|
316
|
+
subscriber.next(rewardsHistory);
|
|
317
|
+
subscriber.complete();
|
|
318
|
+
})
|
|
319
|
+
.catch((error) => subscriber.error(error));
|
|
320
|
+
})
|
|
321
|
+
)
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Retrieves and emits the current balance of the reward account at specified polling intervals.
|
|
327
|
+
*
|
|
328
|
+
* @returns {Observable<Cardano.Lovelace>} An observable that emits the balance of the reward account.
|
|
329
|
+
*/
|
|
330
|
+
getRewardAccountBalance(): Observable<Cardano.Lovelace> {
|
|
331
|
+
return interval(this.#pollingInterval).pipe(
|
|
332
|
+
switchMap(
|
|
333
|
+
() =>
|
|
334
|
+
new Observable<Cardano.Lovelace>((subscriber) => {
|
|
335
|
+
this.#rewardsProvider
|
|
336
|
+
.rewardAccountBalance({
|
|
337
|
+
rewardAccount: this.#address.rewardAccount
|
|
338
|
+
})
|
|
339
|
+
// eslint-disable-next-line promise/always-return
|
|
340
|
+
.then((rewardAccountBalance) => {
|
|
341
|
+
subscriber.next(rewardAccountBalance);
|
|
342
|
+
subscriber.complete();
|
|
343
|
+
})
|
|
344
|
+
.catch((error) => subscriber.error(error));
|
|
345
|
+
})
|
|
346
|
+
)
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Internally used method to build the multi-signature script.
|
|
352
|
+
*
|
|
353
|
+
* @param {Array<Crypto.Ed25519PublicKeyHex>} expectedSigners - The public keys expected to sign transactions.
|
|
354
|
+
* @param {InMemoryKeyAgent} keyAgent - The in-memory key agent.
|
|
355
|
+
* @returns {Promise<Cardano.NativeScript>} The constructed native script.
|
|
356
|
+
*/
|
|
357
|
+
static async #buildScript(expectedSigners: Array<Crypto.Ed25519PublicKeyHex>, keyAgent: InMemoryKeyAgent) {
|
|
358
|
+
const signers = [...expectedSigners];
|
|
359
|
+
|
|
360
|
+
// Sorting guarantees that we will always get the same script if the same keys are used.
|
|
361
|
+
signers.sort();
|
|
362
|
+
|
|
363
|
+
// We are going to use RequireAllOf for this POC to keep it simple, but RequireNOf makes more sense.
|
|
364
|
+
const script: Cardano.NativeScript = {
|
|
365
|
+
__type: Cardano.ScriptType.Native,
|
|
366
|
+
kind: Cardano.NativeScriptKind.RequireAllOf,
|
|
367
|
+
scripts: []
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
for (const signer of signers) {
|
|
371
|
+
script.scripts.push({
|
|
372
|
+
__type: Cardano.ScriptType.Native,
|
|
373
|
+
keyHash: await keyAgent.bip32Ed25519.getPubKeyHash(signer),
|
|
374
|
+
kind: Cardano.NativeScriptKind.RequireSignature
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return script;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Internally used method to derive the wallet's grouped address from the script and network ID.
|
|
383
|
+
*
|
|
384
|
+
* @param {Cardano.NativeScript} script - The native script for multi-signature.
|
|
385
|
+
* @param {Cardano.NetworkId} networkId - The network identifier.
|
|
386
|
+
* @returns {Promise<GroupedAddress>} The derived grouped address.
|
|
387
|
+
*/
|
|
388
|
+
static async #getAddress(script: Cardano.NativeScript, networkId: Cardano.NetworkId): Promise<GroupedAddress> {
|
|
389
|
+
const scriptHash = nativeScriptPolicyId(script) as unknown as Crypto.Hash28ByteBase16;
|
|
390
|
+
|
|
391
|
+
const scriptCredential = {
|
|
392
|
+
hash: scriptHash,
|
|
393
|
+
type: Cardano.CredentialType.ScriptHash
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
const baseAddress = Cardano.BaseAddress.fromCredentials(
|
|
397
|
+
Cardano.NetworkId.Testnet,
|
|
398
|
+
scriptCredential,
|
|
399
|
+
scriptCredential
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
return {
|
|
403
|
+
accountIndex: 0,
|
|
404
|
+
address: baseAddress.toAddress().toBech32() as Cardano.PaymentAddress,
|
|
405
|
+
index: 0,
|
|
406
|
+
networkId,
|
|
407
|
+
rewardAccount: Cardano.RewardAddress.fromCredentials(networkId, scriptCredential)
|
|
408
|
+
.toAddress()
|
|
409
|
+
.toBech32() as Cardano.RewardAccount,
|
|
410
|
+
type: AddressType.External
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Internally used method to create a transaction with the given outputs and certificates.
|
|
416
|
+
*
|
|
417
|
+
* @param {Set<Cardano.TxOut>?} txOuts - The set of transaction outputs.
|
|
418
|
+
* @param {Cardano.Certificate[]?} certificates - The list of certificates to include in the transaction.
|
|
419
|
+
* @returns {Promise<{ body: Cardano.TxBody, id: Cardano.TransactionId }>} The transaction body and ID.
|
|
420
|
+
*/
|
|
421
|
+
async #createTransaction(txOuts?: Set<Cardano.TxOut>, certificates?: Cardano.Certificate[]) {
|
|
422
|
+
const [protocolParameters, utxo] = await Promise.all([
|
|
423
|
+
this.#networkInfoProvider.protocolParameters(),
|
|
424
|
+
firstValueFrom(this.getUtxoSet())
|
|
425
|
+
]);
|
|
426
|
+
|
|
427
|
+
const withdrawals: Cardano.Withdrawal[] = [];
|
|
428
|
+
const rewardsBalance = await firstValueFrom(this.getRewardAccountBalance());
|
|
429
|
+
|
|
430
|
+
if (rewardsBalance > 0) {
|
|
431
|
+
withdrawals.push({ quantity: rewardsBalance, stakeAddress: this.getRewardAccount() });
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const constraints = defaultSelectionConstraints({
|
|
435
|
+
buildTx: async (inputSelection) => {
|
|
436
|
+
const body: Cardano.TxBody = {
|
|
437
|
+
certificates,
|
|
438
|
+
fee: inputSelection.fee,
|
|
439
|
+
inputs: [...inputSelection.inputs].map(([txIn]) => txIn),
|
|
440
|
+
outputs: txOuts ? [...txOuts.values()] : [],
|
|
441
|
+
...(withdrawals.length > 0 ? { withdrawals } : {})
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
const signatureMap = new Map();
|
|
445
|
+
|
|
446
|
+
// TODO: There is a small bug here and the fee is off by a few lovelace, this *2 will offset
|
|
447
|
+
// the error in the meantime.
|
|
448
|
+
for (let i = 0; i < this.#expectedSigners.length * 2; ++i)
|
|
449
|
+
signatureMap.set(randomPublicKey(), DUMMY_HEX_BYTES as Crypto.Ed25519SignatureHex);
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
body,
|
|
453
|
+
id: '' as Cardano.TransactionId,
|
|
454
|
+
witness: {
|
|
455
|
+
scripts: [this.#multisigScript],
|
|
456
|
+
signatures: signatureMap
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
},
|
|
460
|
+
protocolParameters
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const implicitCoin = Cardano.util.computeImplicitCoin(protocolParameters, {
|
|
464
|
+
certificates,
|
|
465
|
+
withdrawals
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
const { selection: inputSelection } = await this.#inputSelector.select({
|
|
469
|
+
constraints,
|
|
470
|
+
implicitValue: { coin: implicitCoin },
|
|
471
|
+
outputs: txOuts || new Set(),
|
|
472
|
+
utxo: new Set(utxo)
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
const body = {
|
|
476
|
+
certificates,
|
|
477
|
+
fee: inputSelection.fee,
|
|
478
|
+
inputs: [...inputSelection.inputs].map(([txIn]) => txIn),
|
|
479
|
+
outputs: txOuts ? [...inputSelection.outputs, ...inputSelection.change] : [],
|
|
480
|
+
withdrawals
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const serializableBody = Serialization.TransactionBody.fromCore(body);
|
|
484
|
+
|
|
485
|
+
const id = Cardano.TransactionId.fromHexBlob(
|
|
486
|
+
util.bytesToHex(Crypto.blake2b(Crypto.blake2b.BYTES).update(util.hexToBytes(serializableBody.toCbor())).digest())
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
return { body, id };
|
|
490
|
+
}
|
|
491
|
+
}
|