@meshsdk/wallet 1.6.0-alpha.21 → 1.6.2
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/dist/index.cjs +6 -0
- package/dist/{index.d.mts → index.d.cts} +30 -1
- package/dist/index.d.ts +30 -1
- package/dist/index.js +1 -1208
- package/package.json +33 -15
- package/.turbo/turbo-build$colon$docs.log +0 -11
- package/.turbo/turbo-build$colon$mesh.log +0 -19
- package/.turbo/turbo-build.log +0 -19
- package/dist/index.mjs +0 -1234
- package/jest.config.js +0 -5
- package/src/app/index.ts +0 -239
- package/src/browser/index.ts +0 -440
- package/src/embedded/index.ts +0 -258
- package/src/index.ts +0 -4
- package/src/mesh/index.ts +0 -413
- package/src/types/index.ts +0 -39
- package/test/app.test.ts +0 -98
- package/test/browser.test.ts +0 -21
- package/test/embedded.test.ts +0 -137
- package/test/mesh.test.ts +0 -95
- package/tsconfig.json +0 -5
package/dist/index.js
CHANGED
|
@@ -1,1211 +1,4 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
AppWallet: () => AppWallet,
|
|
24
|
-
BrowserWallet: () => BrowserWallet,
|
|
25
|
-
EmbeddedWallet: () => EmbeddedWallet,
|
|
26
|
-
MeshWallet: () => MeshWallet,
|
|
27
|
-
WalletStaticMethods: () => WalletStaticMethods
|
|
28
|
-
});
|
|
29
|
-
module.exports = __toCommonJS(src_exports);
|
|
30
|
-
|
|
31
|
-
// src/app/index.ts
|
|
32
|
-
var import_core_cst2 = require("@meshsdk/core-cst");
|
|
33
|
-
|
|
34
|
-
// ../../node_modules/@scure/base/lib/esm/index.js
|
|
35
|
-
// @__NO_SIDE_EFFECTS__
|
|
36
|
-
function assertNumber(n) {
|
|
37
|
-
if (!Number.isSafeInteger(n))
|
|
38
|
-
throw new Error(`Wrong integer: ${n}`);
|
|
39
|
-
}
|
|
40
|
-
function isBytes(a) {
|
|
41
|
-
return a instanceof Uint8Array || a != null && typeof a === "object" && a.constructor.name === "Uint8Array";
|
|
42
|
-
}
|
|
43
|
-
// @__NO_SIDE_EFFECTS__
|
|
44
|
-
function chain(...args) {
|
|
45
|
-
const id = (a) => a;
|
|
46
|
-
const wrap = (a, b) => (c) => a(b(c));
|
|
47
|
-
const encode = args.map((x) => x.encode).reduceRight(wrap, id);
|
|
48
|
-
const decode = args.map((x) => x.decode).reduce(wrap, id);
|
|
49
|
-
return { encode, decode };
|
|
50
|
-
}
|
|
51
|
-
// @__NO_SIDE_EFFECTS__
|
|
52
|
-
function alphabet(alphabet2) {
|
|
53
|
-
return {
|
|
54
|
-
encode: (digits) => {
|
|
55
|
-
if (!Array.isArray(digits) || digits.length && typeof digits[0] !== "number")
|
|
56
|
-
throw new Error("alphabet.encode input should be an array of numbers");
|
|
57
|
-
return digits.map((i) => {
|
|
58
|
-
/* @__PURE__ */ assertNumber(i);
|
|
59
|
-
if (i < 0 || i >= alphabet2.length)
|
|
60
|
-
throw new Error(`Digit index outside alphabet: ${i} (alphabet: ${alphabet2.length})`);
|
|
61
|
-
return alphabet2[i];
|
|
62
|
-
});
|
|
63
|
-
},
|
|
64
|
-
decode: (input) => {
|
|
65
|
-
if (!Array.isArray(input) || input.length && typeof input[0] !== "string")
|
|
66
|
-
throw new Error("alphabet.decode input should be array of strings");
|
|
67
|
-
return input.map((letter) => {
|
|
68
|
-
if (typeof letter !== "string")
|
|
69
|
-
throw new Error(`alphabet.decode: not string element=${letter}`);
|
|
70
|
-
const index = alphabet2.indexOf(letter);
|
|
71
|
-
if (index === -1)
|
|
72
|
-
throw new Error(`Unknown letter: "${letter}". Allowed: ${alphabet2}`);
|
|
73
|
-
return index;
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
// @__NO_SIDE_EFFECTS__
|
|
79
|
-
function join(separator = "") {
|
|
80
|
-
if (typeof separator !== "string")
|
|
81
|
-
throw new Error("join separator should be string");
|
|
82
|
-
return {
|
|
83
|
-
encode: (from) => {
|
|
84
|
-
if (!Array.isArray(from) || from.length && typeof from[0] !== "string")
|
|
85
|
-
throw new Error("join.encode input should be array of strings");
|
|
86
|
-
for (let i of from)
|
|
87
|
-
if (typeof i !== "string")
|
|
88
|
-
throw new Error(`join.encode: non-string input=${i}`);
|
|
89
|
-
return from.join(separator);
|
|
90
|
-
},
|
|
91
|
-
decode: (to) => {
|
|
92
|
-
if (typeof to !== "string")
|
|
93
|
-
throw new Error("join.decode input should be string");
|
|
94
|
-
return to.split(separator);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
var gcd = /* @__NO_SIDE_EFFECTS__ */ (a, b) => !b ? a : /* @__PURE__ */ gcd(b, a % b);
|
|
99
|
-
var radix2carry = /* @__NO_SIDE_EFFECTS__ */ (from, to) => from + (to - /* @__PURE__ */ gcd(from, to));
|
|
100
|
-
// @__NO_SIDE_EFFECTS__
|
|
101
|
-
function convertRadix2(data, from, to, padding) {
|
|
102
|
-
if (!Array.isArray(data))
|
|
103
|
-
throw new Error("convertRadix2: data should be array");
|
|
104
|
-
if (from <= 0 || from > 32)
|
|
105
|
-
throw new Error(`convertRadix2: wrong from=${from}`);
|
|
106
|
-
if (to <= 0 || to > 32)
|
|
107
|
-
throw new Error(`convertRadix2: wrong to=${to}`);
|
|
108
|
-
if (/* @__PURE__ */ radix2carry(from, to) > 32) {
|
|
109
|
-
throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${/* @__PURE__ */ radix2carry(from, to)}`);
|
|
110
|
-
}
|
|
111
|
-
let carry = 0;
|
|
112
|
-
let pos = 0;
|
|
113
|
-
const mask = 2 ** to - 1;
|
|
114
|
-
const res = [];
|
|
115
|
-
for (const n of data) {
|
|
116
|
-
/* @__PURE__ */ assertNumber(n);
|
|
117
|
-
if (n >= 2 ** from)
|
|
118
|
-
throw new Error(`convertRadix2: invalid data word=${n} from=${from}`);
|
|
119
|
-
carry = carry << from | n;
|
|
120
|
-
if (pos + from > 32)
|
|
121
|
-
throw new Error(`convertRadix2: carry overflow pos=${pos} from=${from}`);
|
|
122
|
-
pos += from;
|
|
123
|
-
for (; pos >= to; pos -= to)
|
|
124
|
-
res.push((carry >> pos - to & mask) >>> 0);
|
|
125
|
-
carry &= 2 ** pos - 1;
|
|
126
|
-
}
|
|
127
|
-
carry = carry << to - pos & mask;
|
|
128
|
-
if (!padding && pos >= from)
|
|
129
|
-
throw new Error("Excess padding");
|
|
130
|
-
if (!padding && carry)
|
|
131
|
-
throw new Error(`Non-zero padding: ${carry}`);
|
|
132
|
-
if (padding && pos > 0)
|
|
133
|
-
res.push(carry >>> 0);
|
|
134
|
-
return res;
|
|
135
|
-
}
|
|
136
|
-
// @__NO_SIDE_EFFECTS__
|
|
137
|
-
function radix2(bits, revPadding = false) {
|
|
138
|
-
/* @__PURE__ */ assertNumber(bits);
|
|
139
|
-
if (bits <= 0 || bits > 32)
|
|
140
|
-
throw new Error("radix2: bits should be in (0..32]");
|
|
141
|
-
if (/* @__PURE__ */ radix2carry(8, bits) > 32 || /* @__PURE__ */ radix2carry(bits, 8) > 32)
|
|
142
|
-
throw new Error("radix2: carry overflow");
|
|
143
|
-
return {
|
|
144
|
-
encode: (bytes) => {
|
|
145
|
-
if (!isBytes(bytes))
|
|
146
|
-
throw new Error("radix2.encode input should be Uint8Array");
|
|
147
|
-
return /* @__PURE__ */ convertRadix2(Array.from(bytes), 8, bits, !revPadding);
|
|
148
|
-
},
|
|
149
|
-
decode: (digits) => {
|
|
150
|
-
if (!Array.isArray(digits) || digits.length && typeof digits[0] !== "number")
|
|
151
|
-
throw new Error("radix2.decode input should be array of numbers");
|
|
152
|
-
return Uint8Array.from(/* @__PURE__ */ convertRadix2(digits, bits, 8, revPadding));
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
// @__NO_SIDE_EFFECTS__
|
|
157
|
-
function unsafeWrapper(fn) {
|
|
158
|
-
if (typeof fn !== "function")
|
|
159
|
-
throw new Error("unsafeWrapper fn should be function");
|
|
160
|
-
return function(...args) {
|
|
161
|
-
try {
|
|
162
|
-
return fn.apply(null, args);
|
|
163
|
-
} catch (e) {
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
var BECH_ALPHABET = /* @__PURE__ */ chain(/* @__PURE__ */ alphabet("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), /* @__PURE__ */ join(""));
|
|
168
|
-
var POLYMOD_GENERATORS = [996825010, 642813549, 513874426, 1027748829, 705979059];
|
|
169
|
-
// @__NO_SIDE_EFFECTS__
|
|
170
|
-
function bech32Polymod(pre) {
|
|
171
|
-
const b = pre >> 25;
|
|
172
|
-
let chk = (pre & 33554431) << 5;
|
|
173
|
-
for (let i = 0; i < POLYMOD_GENERATORS.length; i++) {
|
|
174
|
-
if ((b >> i & 1) === 1)
|
|
175
|
-
chk ^= POLYMOD_GENERATORS[i];
|
|
176
|
-
}
|
|
177
|
-
return chk;
|
|
178
|
-
}
|
|
179
|
-
// @__NO_SIDE_EFFECTS__
|
|
180
|
-
function bechChecksum(prefix, words, encodingConst = 1) {
|
|
181
|
-
const len = prefix.length;
|
|
182
|
-
let chk = 1;
|
|
183
|
-
for (let i = 0; i < len; i++) {
|
|
184
|
-
const c = prefix.charCodeAt(i);
|
|
185
|
-
if (c < 33 || c > 126)
|
|
186
|
-
throw new Error(`Invalid prefix (${prefix})`);
|
|
187
|
-
chk = /* @__PURE__ */ bech32Polymod(chk) ^ c >> 5;
|
|
188
|
-
}
|
|
189
|
-
chk = /* @__PURE__ */ bech32Polymod(chk);
|
|
190
|
-
for (let i = 0; i < len; i++)
|
|
191
|
-
chk = /* @__PURE__ */ bech32Polymod(chk) ^ prefix.charCodeAt(i) & 31;
|
|
192
|
-
for (let v of words)
|
|
193
|
-
chk = /* @__PURE__ */ bech32Polymod(chk) ^ v;
|
|
194
|
-
for (let i = 0; i < 6; i++)
|
|
195
|
-
chk = /* @__PURE__ */ bech32Polymod(chk);
|
|
196
|
-
chk ^= encodingConst;
|
|
197
|
-
return BECH_ALPHABET.encode(/* @__PURE__ */ convertRadix2([chk % 2 ** 30], 30, 5, false));
|
|
198
|
-
}
|
|
199
|
-
// @__NO_SIDE_EFFECTS__
|
|
200
|
-
function genBech32(encoding) {
|
|
201
|
-
const ENCODING_CONST = encoding === "bech32" ? 1 : 734539939;
|
|
202
|
-
const _words = /* @__PURE__ */ radix2(5);
|
|
203
|
-
const fromWords = _words.decode;
|
|
204
|
-
const toWords = _words.encode;
|
|
205
|
-
const fromWordsUnsafe = /* @__PURE__ */ unsafeWrapper(fromWords);
|
|
206
|
-
function encode(prefix, words, limit = 90) {
|
|
207
|
-
if (typeof prefix !== "string")
|
|
208
|
-
throw new Error(`bech32.encode prefix should be string, not ${typeof prefix}`);
|
|
209
|
-
if (!Array.isArray(words) || words.length && typeof words[0] !== "number")
|
|
210
|
-
throw new Error(`bech32.encode words should be array of numbers, not ${typeof words}`);
|
|
211
|
-
if (prefix.length === 0)
|
|
212
|
-
throw new TypeError(`Invalid prefix length ${prefix.length}`);
|
|
213
|
-
const actualLength = prefix.length + 7 + words.length;
|
|
214
|
-
if (limit !== false && actualLength > limit)
|
|
215
|
-
throw new TypeError(`Length ${actualLength} exceeds limit ${limit}`);
|
|
216
|
-
const lowered = prefix.toLowerCase();
|
|
217
|
-
const sum = /* @__PURE__ */ bechChecksum(lowered, words, ENCODING_CONST);
|
|
218
|
-
return `${lowered}1${BECH_ALPHABET.encode(words)}${sum}`;
|
|
219
|
-
}
|
|
220
|
-
function decode(str, limit = 90) {
|
|
221
|
-
if (typeof str !== "string")
|
|
222
|
-
throw new Error(`bech32.decode input should be string, not ${typeof str}`);
|
|
223
|
-
if (str.length < 8 || limit !== false && str.length > limit)
|
|
224
|
-
throw new TypeError(`Wrong string length: ${str.length} (${str}). Expected (8..${limit})`);
|
|
225
|
-
const lowered = str.toLowerCase();
|
|
226
|
-
if (str !== lowered && str !== str.toUpperCase())
|
|
227
|
-
throw new Error(`String must be lowercase or uppercase`);
|
|
228
|
-
const sepIndex = lowered.lastIndexOf("1");
|
|
229
|
-
if (sepIndex === 0 || sepIndex === -1)
|
|
230
|
-
throw new Error(`Letter "1" must be present between prefix and data only`);
|
|
231
|
-
const prefix = lowered.slice(0, sepIndex);
|
|
232
|
-
const data = lowered.slice(sepIndex + 1);
|
|
233
|
-
if (data.length < 6)
|
|
234
|
-
throw new Error("Data must be at least 6 characters long");
|
|
235
|
-
const words = BECH_ALPHABET.decode(data).slice(0, -6);
|
|
236
|
-
const sum = /* @__PURE__ */ bechChecksum(prefix, words, ENCODING_CONST);
|
|
237
|
-
if (!data.endsWith(sum))
|
|
238
|
-
throw new Error(`Invalid checksum in ${str}: expected "${sum}"`);
|
|
239
|
-
return { prefix, words };
|
|
240
|
-
}
|
|
241
|
-
const decodeUnsafe = /* @__PURE__ */ unsafeWrapper(decode);
|
|
242
|
-
function decodeToBytes(str) {
|
|
243
|
-
const { prefix, words } = decode(str, false);
|
|
244
|
-
return { prefix, words, bytes: fromWords(words) };
|
|
245
|
-
}
|
|
246
|
-
return { encode, decode, decodeToBytes, decodeUnsafe, fromWords, fromWordsUnsafe, toWords };
|
|
247
|
-
}
|
|
248
|
-
var bech32 = /* @__PURE__ */ genBech32("bech32");
|
|
249
|
-
|
|
250
|
-
// src/embedded/index.ts
|
|
251
|
-
var import_common = require("@meshsdk/common");
|
|
252
|
-
var import_core_cst = require("@meshsdk/core-cst");
|
|
253
|
-
var WalletStaticMethods = class {
|
|
254
|
-
static privateKeyToEntropy(bech322) {
|
|
255
|
-
const bech32DecodedBytes = bech32.decodeToBytes(bech322).bytes;
|
|
256
|
-
const bip32PrivateKey = import_core_cst.Bip32PrivateKey.fromBytes(bech32DecodedBytes);
|
|
257
|
-
return (0, import_common.bytesToHex)(bip32PrivateKey.bytes());
|
|
258
|
-
}
|
|
259
|
-
static mnemonicToEntropy(words) {
|
|
260
|
-
const entropy = (0, import_common.mnemonicToEntropy)(words.join(" "));
|
|
261
|
-
const bip32PrivateKey = (0, import_core_cst.buildBip32PrivateKey)(entropy);
|
|
262
|
-
return (0, import_common.bytesToHex)(bip32PrivateKey.bytes());
|
|
263
|
-
}
|
|
264
|
-
static signingKeyToEntropy(paymentKey, stakeKey) {
|
|
265
|
-
return [
|
|
266
|
-
paymentKey.startsWith("5820") ? paymentKey.slice(4) : paymentKey,
|
|
267
|
-
stakeKey.startsWith("5820") ? stakeKey.slice(4) : stakeKey
|
|
268
|
-
];
|
|
269
|
-
}
|
|
270
|
-
static getAddresses(paymentKey, stakingKey, networkId = 0) {
|
|
271
|
-
const baseAddress = (0, import_core_cst.buildBaseAddress)(
|
|
272
|
-
networkId,
|
|
273
|
-
import_core_cst.Hash28ByteBase16.fromEd25519KeyHashHex(
|
|
274
|
-
(0, import_core_cst.Ed25519KeyHashHex)(paymentKey.toPublicKey().hash().toString("hex"))
|
|
275
|
-
),
|
|
276
|
-
import_core_cst.Hash28ByteBase16.fromEd25519KeyHashHex(
|
|
277
|
-
(0, import_core_cst.Ed25519KeyHashHex)(stakingKey.toPublicKey().hash().toString("hex"))
|
|
278
|
-
)
|
|
279
|
-
);
|
|
280
|
-
const enterpriseAddress = (0, import_core_cst.buildEnterpriseAddress)(
|
|
281
|
-
networkId,
|
|
282
|
-
import_core_cst.Hash28ByteBase16.fromEd25519KeyHashHex(
|
|
283
|
-
(0, import_core_cst.Ed25519KeyHashHex)(paymentKey.toPublicKey().hash().toString("hex"))
|
|
284
|
-
)
|
|
285
|
-
);
|
|
286
|
-
const rewardAddress = (0, import_core_cst.buildRewardAddress)(
|
|
287
|
-
networkId,
|
|
288
|
-
import_core_cst.Hash28ByteBase16.fromEd25519KeyHashHex(
|
|
289
|
-
(0, import_core_cst.Ed25519KeyHashHex)(stakingKey.toPublicKey().hash().toString("hex"))
|
|
290
|
-
)
|
|
291
|
-
);
|
|
292
|
-
return {
|
|
293
|
-
baseAddress: baseAddress.toAddress(),
|
|
294
|
-
enterpriseAddress: enterpriseAddress.toAddress(),
|
|
295
|
-
rewardAddress: rewardAddress.toAddress()
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
static generateMnemonic(strength = 256) {
|
|
299
|
-
const mnemonic = (0, import_common.generateMnemonic)(strength);
|
|
300
|
-
return mnemonic.split(" ");
|
|
301
|
-
}
|
|
302
|
-
static addWitnessSets(txHex, witnesses) {
|
|
303
|
-
let tx = (0, import_core_cst.deserializeTx)(txHex);
|
|
304
|
-
let witnessSet = tx.witnessSet();
|
|
305
|
-
let witnessSetVkeys = witnessSet.vkeys();
|
|
306
|
-
let witnessSetVkeysValues = witnessSetVkeys ? [...witnessSetVkeys.values(), ...witnesses] : witnesses;
|
|
307
|
-
witnessSet.setVkeys(
|
|
308
|
-
import_core_cst.Serialization.CborSet.fromCore(
|
|
309
|
-
witnessSetVkeysValues.map((vkw) => vkw.toCore()),
|
|
310
|
-
import_core_cst.VkeyWitness.fromCore
|
|
311
|
-
)
|
|
312
|
-
);
|
|
313
|
-
return new import_core_cst.Transaction(tx.body(), witnessSet, tx.auxiliaryData()).toCbor();
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
var EmbeddedWallet = class extends WalletStaticMethods {
|
|
317
|
-
constructor(options) {
|
|
318
|
-
super();
|
|
319
|
-
this._networkId = options.networkId;
|
|
320
|
-
switch (options.key.type) {
|
|
321
|
-
case "mnemonic":
|
|
322
|
-
this._entropy = WalletStaticMethods.mnemonicToEntropy(
|
|
323
|
-
options.key.words
|
|
324
|
-
);
|
|
325
|
-
break;
|
|
326
|
-
case "root":
|
|
327
|
-
this._entropy = WalletStaticMethods.privateKeyToEntropy(
|
|
328
|
-
options.key.bech32
|
|
329
|
-
);
|
|
330
|
-
break;
|
|
331
|
-
case "cli":
|
|
332
|
-
this._entropy = WalletStaticMethods.signingKeyToEntropy(
|
|
333
|
-
options.key.payment,
|
|
334
|
-
options.key.stake ?? "f0".repeat(32)
|
|
335
|
-
);
|
|
336
|
-
break;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
getAccount(accountIndex = 0, keyIndex = 0) {
|
|
340
|
-
if (this._entropy == void 0)
|
|
341
|
-
throw new Error("[EmbeddedWallet] No keys initialized");
|
|
342
|
-
const { paymentKey, stakeKey } = (0, import_core_cst.buildKeys)(
|
|
343
|
-
this._entropy,
|
|
344
|
-
accountIndex,
|
|
345
|
-
keyIndex
|
|
346
|
-
);
|
|
347
|
-
const { baseAddress, enterpriseAddress, rewardAddress } = WalletStaticMethods.getAddresses(paymentKey, stakeKey, this._networkId);
|
|
348
|
-
return {
|
|
349
|
-
baseAddress,
|
|
350
|
-
enterpriseAddress,
|
|
351
|
-
rewardAddress,
|
|
352
|
-
baseAddressBech32: baseAddress.toBech32(),
|
|
353
|
-
enterpriseAddressBech32: enterpriseAddress.toBech32(),
|
|
354
|
-
rewardAddressBech32: rewardAddress.toBech32(),
|
|
355
|
-
paymentKey,
|
|
356
|
-
stakeKey,
|
|
357
|
-
paymentKeyHex: paymentKey.toBytes().toString("hex"),
|
|
358
|
-
stakeKeyHex: stakeKey.toBytes().toString("hex")
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
getNetworkId() {
|
|
362
|
-
return this._networkId;
|
|
363
|
-
}
|
|
364
|
-
signData(address, payload, accountIndex = 0, keyIndex = 0) {
|
|
365
|
-
try {
|
|
366
|
-
const account = this.getAccount(accountIndex, keyIndex);
|
|
367
|
-
const foundAddress = [
|
|
368
|
-
account.baseAddress,
|
|
369
|
-
account.enterpriseAddress,
|
|
370
|
-
account.rewardAddress
|
|
371
|
-
].find((a) => a.toBech32() === address);
|
|
372
|
-
if (foundAddress === void 0)
|
|
373
|
-
throw new Error(
|
|
374
|
-
`[EmbeddedWallet] Address: ${address} doesn't belong to this account.`
|
|
375
|
-
);
|
|
376
|
-
return (0, import_core_cst.signData)(payload, account.paymentKey);
|
|
377
|
-
} catch (error) {
|
|
378
|
-
throw new Error(
|
|
379
|
-
`[EmbeddedWallet] An error occurred during signData: ${error}.`
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
signTx(unsignedTx, accountIndex = 0, keyIndex = 0) {
|
|
384
|
-
try {
|
|
385
|
-
const txHash = (0, import_core_cst.deserializeTxHash)((0, import_core_cst.resolveTxHash)(unsignedTx));
|
|
386
|
-
const account = this.getAccount(accountIndex, keyIndex);
|
|
387
|
-
const vKeyWitness = new import_core_cst.VkeyWitness(
|
|
388
|
-
(0, import_core_cst.Ed25519PublicKeyHex)(
|
|
389
|
-
account.paymentKey.toPublicKey().toBytes().toString("hex")
|
|
390
|
-
),
|
|
391
|
-
(0, import_core_cst.Ed25519SignatureHex)(
|
|
392
|
-
account.paymentKey.sign(Buffer.from(txHash, "hex")).toString("hex")
|
|
393
|
-
)
|
|
394
|
-
);
|
|
395
|
-
return vKeyWitness;
|
|
396
|
-
} catch (error) {
|
|
397
|
-
throw new Error(
|
|
398
|
-
`[EmbeddedWallet] An error occurred during signTx: ${error}.`
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
// src/app/index.ts
|
|
405
|
-
var AppWallet = class {
|
|
406
|
-
constructor(options) {
|
|
407
|
-
this._fetcher = options.fetcher;
|
|
408
|
-
this._submitter = options.submitter;
|
|
409
|
-
switch (options.key.type) {
|
|
410
|
-
case "mnemonic":
|
|
411
|
-
this._wallet = new EmbeddedWallet({
|
|
412
|
-
networkId: options.networkId,
|
|
413
|
-
key: {
|
|
414
|
-
type: "mnemonic",
|
|
415
|
-
words: options.key.words
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
break;
|
|
419
|
-
case "root":
|
|
420
|
-
this._wallet = new EmbeddedWallet({
|
|
421
|
-
networkId: options.networkId,
|
|
422
|
-
key: {
|
|
423
|
-
type: "root",
|
|
424
|
-
bech32: options.key.bech32
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
break;
|
|
428
|
-
case "cli":
|
|
429
|
-
this._wallet = new EmbeddedWallet({
|
|
430
|
-
networkId: options.networkId,
|
|
431
|
-
key: {
|
|
432
|
-
type: "cli",
|
|
433
|
-
payment: options.key.payment,
|
|
434
|
-
stake: options.key.stake
|
|
435
|
-
}
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
/**
|
|
440
|
-
* Get a list of UTXOs to be used as collateral inputs for transactions with plutus script inputs.
|
|
441
|
-
*
|
|
442
|
-
* This is used in transaction building.
|
|
443
|
-
*
|
|
444
|
-
* @returns a list of UTXOs
|
|
445
|
-
*/
|
|
446
|
-
async getCollateralUnspentOutput(accountIndex = 0, addressType = "payment") {
|
|
447
|
-
const utxos = await this.getUnspentOutputs(accountIndex, addressType);
|
|
448
|
-
const pureAdaUtxos = utxos.filter((utxo) => {
|
|
449
|
-
return utxo.output().amount().multiasset() === void 0;
|
|
450
|
-
});
|
|
451
|
-
pureAdaUtxos.sort((a, b) => {
|
|
452
|
-
return Number(a.output().amount().coin()) - Number(b.output().amount().coin());
|
|
453
|
-
});
|
|
454
|
-
for (const utxo of pureAdaUtxos) {
|
|
455
|
-
if (Number(utxo.output().amount().coin()) >= 5e6) {
|
|
456
|
-
return [utxo];
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
return [];
|
|
460
|
-
}
|
|
461
|
-
getEnterpriseAddress(accountIndex = 0, keyIndex = 0) {
|
|
462
|
-
const account = this._wallet.getAccount(accountIndex, keyIndex);
|
|
463
|
-
return account.enterpriseAddressBech32;
|
|
464
|
-
}
|
|
465
|
-
getPaymentAddress(accountIndex = 0, keyIndex = 0) {
|
|
466
|
-
const account = this._wallet.getAccount(accountIndex, keyIndex);
|
|
467
|
-
return account.baseAddressBech32;
|
|
468
|
-
}
|
|
469
|
-
getRewardAddress(accountIndex = 0, keyIndex = 0) {
|
|
470
|
-
const account = this._wallet.getAccount(accountIndex, keyIndex);
|
|
471
|
-
return account.rewardAddressBech32;
|
|
472
|
-
}
|
|
473
|
-
getNetworkId() {
|
|
474
|
-
return this._wallet.getNetworkId();
|
|
475
|
-
}
|
|
476
|
-
getUsedAddress(accountIndex = 0, keyIndex = 0, addressType = "payment") {
|
|
477
|
-
if (addressType === "enterprise") {
|
|
478
|
-
return (0, import_core_cst2.toAddress)(this.getEnterpriseAddress(accountIndex, keyIndex));
|
|
479
|
-
} else {
|
|
480
|
-
return (0, import_core_cst2.toAddress)(this.getPaymentAddress(accountIndex, keyIndex));
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
async getUnspentOutputs(accountIndex = 0, addressType = "payment") {
|
|
484
|
-
if (!this._fetcher) {
|
|
485
|
-
throw new Error(
|
|
486
|
-
"[AppWallet] Fetcher is required to fetch UTxOs. Please provide a fetcher."
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
const account = this._wallet.getAccount(accountIndex);
|
|
490
|
-
const utxos = await this._fetcher.fetchAddressUTxOs(
|
|
491
|
-
addressType == "enterprise" ? account.enterpriseAddressBech32 : account.baseAddressBech32
|
|
492
|
-
);
|
|
493
|
-
return utxos.map((utxo) => (0, import_core_cst2.toTxUnspentOutput)(utxo));
|
|
494
|
-
}
|
|
495
|
-
signData(address, payload, accountIndex = 0, keyIndex = 0) {
|
|
496
|
-
try {
|
|
497
|
-
return this._wallet.signData(address, payload, accountIndex, keyIndex);
|
|
498
|
-
} catch (error) {
|
|
499
|
-
throw new Error(
|
|
500
|
-
`[AppWallet] An error occurred during signData: ${error}.`
|
|
501
|
-
);
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
signTx(unsignedTx, partialSign = false, accountIndex = 0, keyIndex = 0) {
|
|
505
|
-
try {
|
|
506
|
-
const tx = (0, import_core_cst2.deserializeTx)(unsignedTx);
|
|
507
|
-
if (!partialSign && tx.witnessSet().vkeys() !== void 0 && tx.witnessSet().vkeys().size() !== 0)
|
|
508
|
-
throw new Error(
|
|
509
|
-
"Signatures already exist in the transaction in a non partial sign call"
|
|
510
|
-
);
|
|
511
|
-
const newSignatures = this._wallet.signTx(
|
|
512
|
-
unsignedTx,
|
|
513
|
-
accountIndex,
|
|
514
|
-
keyIndex
|
|
515
|
-
);
|
|
516
|
-
let signedTx = EmbeddedWallet.addWitnessSets(unsignedTx, [newSignatures]);
|
|
517
|
-
return signedTx;
|
|
518
|
-
} catch (error) {
|
|
519
|
-
throw new Error(`[AppWallet] An error occurred during signTx: ${error}.`);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
signTxSync(unsignedTx, partialSign = false, accountIndex = 0, keyIndex = 0) {
|
|
523
|
-
try {
|
|
524
|
-
return "signedTx";
|
|
525
|
-
} catch (error) {
|
|
526
|
-
throw new Error(`[AppWallet] An error occurred during signTx: ${error}.`);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
async signTxs(unsignedTxs, partialSign) {
|
|
530
|
-
throw new Error(`[AppWallet] signTxs() is not implemented.`);
|
|
531
|
-
}
|
|
532
|
-
submitTx(tx) {
|
|
533
|
-
if (!this._submitter) {
|
|
534
|
-
throw new Error(
|
|
535
|
-
"[AppWallet] Submitter is required to submit transactions. Please provide a submitter."
|
|
536
|
-
);
|
|
537
|
-
}
|
|
538
|
-
return this._submitter.submitTx(tx);
|
|
539
|
-
}
|
|
540
|
-
static brew(strength = 256) {
|
|
541
|
-
return EmbeddedWallet.generateMnemonic(strength);
|
|
542
|
-
}
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
// src/browser/index.ts
|
|
546
|
-
var import_common2 = require("@meshsdk/common");
|
|
547
|
-
var import_core_cst3 = require("@meshsdk/core-cst");
|
|
548
|
-
var BrowserWallet = class _BrowserWallet {
|
|
549
|
-
constructor(_walletInstance, _walletName) {
|
|
550
|
-
this._walletInstance = _walletInstance;
|
|
551
|
-
this._walletName = _walletName;
|
|
552
|
-
this.walletInstance = { ..._walletInstance };
|
|
553
|
-
}
|
|
554
|
-
/**
|
|
555
|
-
* Returns a list of wallets installed on user's device. Each wallet is an object with the following properties:
|
|
556
|
-
* - A name is provided to display wallet's name on the user interface.
|
|
557
|
-
* - A version is provided to display wallet's version on the user interface.
|
|
558
|
-
* - An icon is provided to display wallet's icon on the user interface.
|
|
559
|
-
*
|
|
560
|
-
* @returns a list of wallet names
|
|
561
|
-
*/
|
|
562
|
-
static getInstalledWallets() {
|
|
563
|
-
if (window === void 0) return [];
|
|
564
|
-
if (window.cardano === void 0) return [];
|
|
565
|
-
let wallets = [];
|
|
566
|
-
for (const key in window.cardano) {
|
|
567
|
-
try {
|
|
568
|
-
const _wallet = window.cardano[key];
|
|
569
|
-
if (_wallet === void 0) continue;
|
|
570
|
-
if (_wallet.name === void 0) continue;
|
|
571
|
-
if (_wallet.icon === void 0) continue;
|
|
572
|
-
if (_wallet.apiVersion === void 0) continue;
|
|
573
|
-
wallets.push({
|
|
574
|
-
id: key,
|
|
575
|
-
name: _wallet.name,
|
|
576
|
-
icon: _wallet.icon,
|
|
577
|
-
version: _wallet.apiVersion
|
|
578
|
-
});
|
|
579
|
-
} catch (e) {
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
return wallets;
|
|
583
|
-
}
|
|
584
|
-
/**
|
|
585
|
-
* This is the entrypoint to start communication with the user's wallet. The wallet should request the user's permission to connect the web page to the user's wallet, and if permission has been granted, the wallet will be returned and exposing the full API for the dApp to use.
|
|
586
|
-
*
|
|
587
|
-
* Query BrowserWallet.getInstalledWallets() to get a list of available wallets, then provide the wallet name for which wallet the user would like to connect with.
|
|
588
|
-
*
|
|
589
|
-
* @param walletName
|
|
590
|
-
* @returns WalletInstance
|
|
591
|
-
*/
|
|
592
|
-
static async enable(walletName) {
|
|
593
|
-
try {
|
|
594
|
-
const walletInstance = await _BrowserWallet.resolveInstance(walletName);
|
|
595
|
-
if (walletInstance !== void 0)
|
|
596
|
-
return new _BrowserWallet(walletInstance, walletName);
|
|
597
|
-
throw new Error(`Couldn't create an instance of wallet: ${walletName}`);
|
|
598
|
-
} catch (error) {
|
|
599
|
-
throw new Error(
|
|
600
|
-
`[BrowserWallet] An error occurred during enable: ${JSON.stringify(
|
|
601
|
-
error
|
|
602
|
-
)}.`
|
|
603
|
-
);
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
/**
|
|
607
|
-
* Retrieves the total available balance of the wallet, encoded in CBOR.
|
|
608
|
-
* @returns {Promise<Value>} - The balance of the wallet.
|
|
609
|
-
*/
|
|
610
|
-
// async _getBalance(): Promise<Value> {
|
|
611
|
-
// const balance = await this._walletInstance.getBalance();
|
|
612
|
-
// return Value.fromCbor(HexBlob(balance));
|
|
613
|
-
// }
|
|
614
|
-
/**
|
|
615
|
-
* Returns a list of assets in the wallet. This API will return every assets in the wallet. Each asset is an object with the following properties:
|
|
616
|
-
* - A unit is provided to display asset's name on the user interface.
|
|
617
|
-
* - A quantity is provided to display asset's quantity on the user interface.
|
|
618
|
-
*
|
|
619
|
-
* @returns a list of assets and their quantities
|
|
620
|
-
*/
|
|
621
|
-
async getBalance() {
|
|
622
|
-
const balance = await this._walletInstance.getBalance();
|
|
623
|
-
return (0, import_core_cst3.fromValue)((0, import_core_cst3.deserializeValue)(balance));
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Returns an address owned by the wallet that should be used as a change address to return leftover assets during transaction creation back to the connected wallet.
|
|
627
|
-
*
|
|
628
|
-
* @returns an address
|
|
629
|
-
*/
|
|
630
|
-
async getChangeAddress() {
|
|
631
|
-
const changeAddress = await this._walletInstance.getChangeAddress();
|
|
632
|
-
return (0, import_core_cst3.addressToBech32)((0, import_core_cst3.deserializeAddress)(changeAddress));
|
|
633
|
-
}
|
|
634
|
-
/**
|
|
635
|
-
* This function shall return a list of one or more UTXOs (unspent transaction outputs) controlled by the wallet that are required to reach AT LEAST the combined ADA value target specified in amount AND the best suitable to be used as collateral inputs for transactions with plutus script inputs (pure ADA-only UTXOs).
|
|
636
|
-
*
|
|
637
|
-
* If this cannot be attained, an error message with an explanation of the blocking problem shall be returned. NOTE: wallets are free to return UTXOs that add up to a greater total ADA value than requested in the amount parameter, but wallets must never return any result where UTXOs would sum up to a smaller total ADA value, instead in a case like that an error message must be returned.
|
|
638
|
-
*
|
|
639
|
-
* @param limit
|
|
640
|
-
* @returns a list of UTXOs
|
|
641
|
-
*/
|
|
642
|
-
async getCollateral() {
|
|
643
|
-
const deserializedCollateral = await this.getCollateralUnspentOutput();
|
|
644
|
-
return deserializedCollateral.map((dc) => (0, import_core_cst3.fromTxUnspentOutput)(dc));
|
|
645
|
-
}
|
|
646
|
-
/**
|
|
647
|
-
* Returns the network ID of the currently connected account. 0 is testnet and 1 is mainnet but other networks can possibly be returned by wallets. Those other network ID values are not governed by CIP-30. This result will stay the same unless the connected account has changed.
|
|
648
|
-
*
|
|
649
|
-
* @returns network ID
|
|
650
|
-
*/
|
|
651
|
-
getNetworkId() {
|
|
652
|
-
return this._walletInstance.getNetworkId();
|
|
653
|
-
}
|
|
654
|
-
/**
|
|
655
|
-
* Returns a list of reward addresses owned by the wallet. A reward address is a stake address that is used to receive rewards from staking, generally starts from `stake` prefix.
|
|
656
|
-
*
|
|
657
|
-
* @returns a list of reward addresses
|
|
658
|
-
*/
|
|
659
|
-
async getRewardAddresses() {
|
|
660
|
-
const rewardAddresses = await this._walletInstance.getRewardAddresses();
|
|
661
|
-
return rewardAddresses.map((ra) => (0, import_core_cst3.addressToBech32)((0, import_core_cst3.deserializeAddress)(ra)));
|
|
662
|
-
}
|
|
663
|
-
/**
|
|
664
|
-
* Returns a list of unused addresses controlled by the wallet.
|
|
665
|
-
*
|
|
666
|
-
* @returns a list of unused addresses
|
|
667
|
-
*/
|
|
668
|
-
async getUnusedAddresses() {
|
|
669
|
-
const unusedAddresses = await this._walletInstance.getUnusedAddresses();
|
|
670
|
-
return unusedAddresses.map(
|
|
671
|
-
(una) => (0, import_core_cst3.addressToBech32)((0, import_core_cst3.deserializeAddress)(una))
|
|
672
|
-
);
|
|
673
|
-
}
|
|
674
|
-
/**
|
|
675
|
-
* Returns a list of used addresses controlled by the wallet.
|
|
676
|
-
*
|
|
677
|
-
* @returns a list of used addresses
|
|
678
|
-
*/
|
|
679
|
-
async getUsedAddresses() {
|
|
680
|
-
const usedAddresses = await this._walletInstance.getUsedAddresses();
|
|
681
|
-
return usedAddresses.map((usa) => (0, import_core_cst3.addressToBech32)((0, import_core_cst3.deserializeAddress)(usa)));
|
|
682
|
-
}
|
|
683
|
-
/**
|
|
684
|
-
* Get a list of UTXOs to be used as collateral inputs for transactions with plutus script inputs.
|
|
685
|
-
*
|
|
686
|
-
* This is used in transaction building.
|
|
687
|
-
*
|
|
688
|
-
* @returns a list of UTXOs
|
|
689
|
-
*/
|
|
690
|
-
async getUsedCollateral(limit = import_common2.DEFAULT_PROTOCOL_PARAMETERS.maxCollateralInputs) {
|
|
691
|
-
const collateral = await this._walletInstance.experimental.getCollateral() ?? [];
|
|
692
|
-
return collateral.map((c) => (0, import_core_cst3.deserializeTxUnspentOutput)(c)).slice(0, limit);
|
|
693
|
-
}
|
|
694
|
-
/**
|
|
695
|
-
* Return a list of all UTXOs (unspent transaction outputs) controlled by the wallet.
|
|
696
|
-
*
|
|
697
|
-
* @returns a list of UTXOs
|
|
698
|
-
*/
|
|
699
|
-
async getUtxos() {
|
|
700
|
-
const deserializedUTxOs = await this.getUsedUTxOs();
|
|
701
|
-
return deserializedUTxOs.map((du) => (0, import_core_cst3.fromTxUnspentOutput)(du));
|
|
702
|
-
}
|
|
703
|
-
/**
|
|
704
|
-
* This endpoint utilizes the [CIP-8 - Message Signing](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0030) to sign arbitrary data, to verify the data was signed by the owner of the private key.
|
|
705
|
-
*
|
|
706
|
-
* Here, we get the first wallet's address with wallet.getUsedAddresses(), alternativelly you can use reward addresses (getRewardAddresses()) too. It's really up to you as the developer which address you want to use in your application.
|
|
707
|
-
*
|
|
708
|
-
* @param address
|
|
709
|
-
* @param payload
|
|
710
|
-
* @returns a signature
|
|
711
|
-
*/
|
|
712
|
-
signData(address, payload) {
|
|
713
|
-
const signerAddress = (0, import_core_cst3.toAddress)(address).toBytes().toString();
|
|
714
|
-
return this._walletInstance.signData(signerAddress, (0, import_common2.fromUTF8)(payload));
|
|
715
|
-
}
|
|
716
|
-
/**
|
|
717
|
-
* Requests user to sign the provided transaction (tx). The wallet should ask the user for permission, and if given, try to sign the supplied body and return a signed transaction. partialSign should be true if the transaction provided requires multiple signatures.
|
|
718
|
-
*
|
|
719
|
-
* @param unsignedTx
|
|
720
|
-
* @param partialSign
|
|
721
|
-
* @returns a signed transaction in CBOR
|
|
722
|
-
*/
|
|
723
|
-
async signTx(unsignedTx, partialSign = false) {
|
|
724
|
-
const witness = await this._walletInstance.signTx(unsignedTx, partialSign);
|
|
725
|
-
console.log("witness", witness);
|
|
726
|
-
return _BrowserWallet.addBrowserWitnesses(unsignedTx, witness);
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* Experimental feature - sign multiple transactions at once (Supported wallet(s): Typhon)
|
|
730
|
-
*
|
|
731
|
-
* @param unsignedTxs - array of unsigned transactions in CborHex string
|
|
732
|
-
* @param partialSign - if the transactions are signed partially
|
|
733
|
-
* @returns array of signed transactions CborHex string
|
|
734
|
-
*/
|
|
735
|
-
async signTxs(unsignedTxs, partialSign = false) {
|
|
736
|
-
let witnessSets = void 0;
|
|
737
|
-
switch (this._walletName) {
|
|
738
|
-
case "Typhon Wallet":
|
|
739
|
-
if (this._walletInstance.signTxs) {
|
|
740
|
-
witnessSets = await this._walletInstance.signTxs(
|
|
741
|
-
unsignedTxs,
|
|
742
|
-
partialSign
|
|
743
|
-
);
|
|
744
|
-
}
|
|
745
|
-
break;
|
|
746
|
-
default:
|
|
747
|
-
if (this._walletInstance.signTxs) {
|
|
748
|
-
witnessSets = await this._walletInstance.signTxs(
|
|
749
|
-
unsignedTxs.map((cbor) => ({
|
|
750
|
-
cbor,
|
|
751
|
-
partialSign
|
|
752
|
-
}))
|
|
753
|
-
);
|
|
754
|
-
} else if (this._walletInstance.experimental.signTxs) {
|
|
755
|
-
witnessSets = await this._walletInstance.experimental.signTxs(
|
|
756
|
-
unsignedTxs.map((cbor) => ({
|
|
757
|
-
cbor,
|
|
758
|
-
partialSign
|
|
759
|
-
}))
|
|
760
|
-
);
|
|
761
|
-
}
|
|
762
|
-
break;
|
|
763
|
-
}
|
|
764
|
-
if (!witnessSets) throw new Error("Wallet does not support signTxs");
|
|
765
|
-
const signedTxs = [];
|
|
766
|
-
for (let i = 0; i < witnessSets.length; i++) {
|
|
767
|
-
const unsignedTx = unsignedTxs[i];
|
|
768
|
-
const cWitness = witnessSets[i];
|
|
769
|
-
const signedTx = _BrowserWallet.addBrowserWitnesses(unsignedTx, cWitness);
|
|
770
|
-
signedTxs.push(signedTx);
|
|
771
|
-
}
|
|
772
|
-
return signedTxs;
|
|
773
|
-
}
|
|
774
|
-
/**
|
|
775
|
-
* Submits the signed transaction to the blockchain network.
|
|
776
|
-
*
|
|
777
|
-
* As wallets should already have this ability to submit transaction, we allow dApps to request that a transaction be sent through it. If the wallet accepts the transaction and tries to send it, it shall return the transaction ID for the dApp to track. The wallet can return error messages or failure if there was an error in sending it.
|
|
778
|
-
*
|
|
779
|
-
* @param tx
|
|
780
|
-
* @returns a transaction hash
|
|
781
|
-
*/
|
|
782
|
-
submitTx(tx) {
|
|
783
|
-
return this._walletInstance.submitTx(tx);
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Get a used address of type Address from the wallet.
|
|
787
|
-
*
|
|
788
|
-
* This is used in transaction building.
|
|
789
|
-
*
|
|
790
|
-
* @returns an Address object
|
|
791
|
-
*/
|
|
792
|
-
async getUsedAddress() {
|
|
793
|
-
const usedAddresses = await this._walletInstance.getUsedAddresses();
|
|
794
|
-
if (usedAddresses.length === 0) throw new Error("No used addresses found");
|
|
795
|
-
return (0, import_core_cst3.deserializeAddress)(usedAddresses[0]);
|
|
796
|
-
}
|
|
797
|
-
/**
|
|
798
|
-
* Get a list of UTXOs to be used as collateral inputs for transactions with plutus script inputs.
|
|
799
|
-
*
|
|
800
|
-
* This is used in transaction building.
|
|
801
|
-
*
|
|
802
|
-
* @returns a list of UTXOs
|
|
803
|
-
*/
|
|
804
|
-
async getCollateralUnspentOutput() {
|
|
805
|
-
const collateral = await this._walletInstance.experimental.getCollateral() ?? [];
|
|
806
|
-
return collateral.map((c) => (0, import_core_cst3.deserializeTxUnspentOutput)(c));
|
|
807
|
-
}
|
|
808
|
-
/**
|
|
809
|
-
* Get a list of UTXOs to be used for transaction building.
|
|
810
|
-
*
|
|
811
|
-
* This is used in transaction building.
|
|
812
|
-
*
|
|
813
|
-
* @returns a list of UTXOs
|
|
814
|
-
*/
|
|
815
|
-
async getUsedUTxOs() {
|
|
816
|
-
const utxos = await this._walletInstance.getUtxos() ?? [];
|
|
817
|
-
return utxos.map((u) => (0, import_core_cst3.deserializeTxUnspentOutput)(u));
|
|
818
|
-
}
|
|
819
|
-
/**
|
|
820
|
-
* A helper function to get the assets in the wallet.
|
|
821
|
-
*
|
|
822
|
-
* @returns a list of assets
|
|
823
|
-
*/
|
|
824
|
-
async getAssets() {
|
|
825
|
-
const balance = await this.getBalance();
|
|
826
|
-
return balance.filter((v) => v.unit !== "lovelace").map((v) => {
|
|
827
|
-
const policyId = v.unit.slice(0, import_common2.POLICY_ID_LENGTH);
|
|
828
|
-
const assetName = v.unit.slice(import_common2.POLICY_ID_LENGTH);
|
|
829
|
-
const fingerprint = (0, import_common2.resolveFingerprint)(policyId, assetName);
|
|
830
|
-
return {
|
|
831
|
-
unit: v.unit,
|
|
832
|
-
policyId,
|
|
833
|
-
assetName,
|
|
834
|
-
fingerprint,
|
|
835
|
-
quantity: v.quantity
|
|
836
|
-
};
|
|
837
|
-
});
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* A helper function to get the lovelace balance in the wallet.
|
|
841
|
-
*
|
|
842
|
-
* @returns lovelace balance
|
|
843
|
-
*/
|
|
844
|
-
async getLovelace() {
|
|
845
|
-
const balance = await this.getBalance();
|
|
846
|
-
const nativeAsset = balance.find((v) => v.unit === "lovelace");
|
|
847
|
-
return nativeAsset !== void 0 ? nativeAsset.quantity : "0";
|
|
848
|
-
}
|
|
849
|
-
/**
|
|
850
|
-
* A helper function to get the assets of a specific policy ID in the wallet.
|
|
851
|
-
*
|
|
852
|
-
* @param policyId
|
|
853
|
-
* @returns a list of assets
|
|
854
|
-
*/
|
|
855
|
-
async getPolicyIdAssets(policyId) {
|
|
856
|
-
const assets = await this.getAssets();
|
|
857
|
-
return assets.filter((v) => v.policyId === policyId);
|
|
858
|
-
}
|
|
859
|
-
/**
|
|
860
|
-
* A helper function to get the policy IDs of all the assets in the wallet.
|
|
861
|
-
*
|
|
862
|
-
* @returns a list of policy IDs
|
|
863
|
-
*/
|
|
864
|
-
async getPolicyIds() {
|
|
865
|
-
const balance = await this.getBalance();
|
|
866
|
-
return Array.from(
|
|
867
|
-
new Set(balance.map((v) => v.unit.slice(0, import_common2.POLICY_ID_LENGTH)))
|
|
868
|
-
).filter((p) => p !== "lovelace");
|
|
869
|
-
}
|
|
870
|
-
static resolveInstance(walletName) {
|
|
871
|
-
if (window.cardano === void 0) return void 0;
|
|
872
|
-
if (window.cardano[walletName] === void 0) return void 0;
|
|
873
|
-
const wallet = window.cardano[walletName];
|
|
874
|
-
return wallet?.enable();
|
|
875
|
-
}
|
|
876
|
-
static addBrowserWitnesses(unsignedTx, witnesses) {
|
|
877
|
-
const cWitness = import_core_cst3.Serialization.TransactionWitnessSet.fromCbor(
|
|
878
|
-
import_core_cst3.CardanoSDKUtil.HexBlob(witnesses)
|
|
879
|
-
).vkeys()?.values();
|
|
880
|
-
if (cWitness === void 0) {
|
|
881
|
-
return unsignedTx;
|
|
882
|
-
}
|
|
883
|
-
let tx = (0, import_core_cst3.deserializeTx)(unsignedTx);
|
|
884
|
-
let witnessSet = tx.witnessSet();
|
|
885
|
-
let witnessSetVkeys = witnessSet.vkeys();
|
|
886
|
-
let witnessSetVkeysValues = witnessSetVkeys ? [...witnessSetVkeys.values(), ...cWitness] : [...cWitness];
|
|
887
|
-
witnessSet.setVkeys(
|
|
888
|
-
import_core_cst3.Serialization.CborSet.fromCore(
|
|
889
|
-
witnessSetVkeysValues.map((vkw) => vkw.toCore()),
|
|
890
|
-
import_core_cst3.VkeyWitness.fromCore
|
|
891
|
-
)
|
|
892
|
-
);
|
|
893
|
-
return new import_core_cst3.Transaction(tx.body(), witnessSet, tx.auxiliaryData()).toCbor();
|
|
894
|
-
}
|
|
895
|
-
};
|
|
896
|
-
|
|
897
|
-
// src/mesh/index.ts
|
|
898
|
-
var import_common3 = require("@meshsdk/common");
|
|
899
|
-
var import_core_cst4 = require("@meshsdk/core-cst");
|
|
900
|
-
var import_transaction = require("@meshsdk/transaction");
|
|
901
|
-
var MeshWallet = class {
|
|
902
|
-
constructor(options) {
|
|
903
|
-
switch (options.key.type) {
|
|
904
|
-
case "root":
|
|
905
|
-
this._wallet = new AppWallet({
|
|
906
|
-
networkId: options.networkId,
|
|
907
|
-
fetcher: options.fetcher,
|
|
908
|
-
submitter: options.submitter,
|
|
909
|
-
key: {
|
|
910
|
-
type: "root",
|
|
911
|
-
bech32: options.key.bech32
|
|
912
|
-
}
|
|
913
|
-
});
|
|
914
|
-
break;
|
|
915
|
-
case "cli":
|
|
916
|
-
this._wallet = new AppWallet({
|
|
917
|
-
networkId: options.networkId,
|
|
918
|
-
fetcher: options.fetcher,
|
|
919
|
-
submitter: options.submitter,
|
|
920
|
-
key: {
|
|
921
|
-
type: "cli",
|
|
922
|
-
payment: options.key.payment,
|
|
923
|
-
stake: options.key.stake
|
|
924
|
-
}
|
|
925
|
-
});
|
|
926
|
-
break;
|
|
927
|
-
case "mnemonic":
|
|
928
|
-
this._wallet = new AppWallet({
|
|
929
|
-
networkId: options.networkId,
|
|
930
|
-
fetcher: options.fetcher,
|
|
931
|
-
submitter: options.submitter,
|
|
932
|
-
key: {
|
|
933
|
-
type: "mnemonic",
|
|
934
|
-
words: options.key.words
|
|
935
|
-
}
|
|
936
|
-
});
|
|
937
|
-
break;
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
/**
|
|
941
|
-
* Returns a list of assets in the wallet. This API will return every assets in the wallet. Each asset is an object with the following properties:
|
|
942
|
-
* - A unit is provided to display asset's name on the user interface.
|
|
943
|
-
* - A quantity is provided to display asset's quantity on the user interface.
|
|
944
|
-
*
|
|
945
|
-
* @returns a list of assets and their quantities
|
|
946
|
-
*/
|
|
947
|
-
async getBalance() {
|
|
948
|
-
const utxos = await this.getUnspentOutputs();
|
|
949
|
-
const assets = /* @__PURE__ */ new Map();
|
|
950
|
-
utxos.map((utxo) => {
|
|
951
|
-
const _utxo = (0, import_core_cst4.fromTxUnspentOutput)(utxo);
|
|
952
|
-
_utxo.output.amount.map((asset) => {
|
|
953
|
-
const assetId = asset.unit;
|
|
954
|
-
const amount = Number(asset.quantity);
|
|
955
|
-
if (assets.has(assetId)) {
|
|
956
|
-
const quantity = assets.get(assetId);
|
|
957
|
-
assets.set(assetId, quantity + amount);
|
|
958
|
-
} else {
|
|
959
|
-
assets.set(assetId, amount);
|
|
960
|
-
}
|
|
961
|
-
});
|
|
962
|
-
});
|
|
963
|
-
const arrayAssets = Array.from(assets, ([unit, quantity]) => ({
|
|
964
|
-
unit,
|
|
965
|
-
quantity: quantity.toString()
|
|
966
|
-
}));
|
|
967
|
-
return arrayAssets;
|
|
968
|
-
}
|
|
969
|
-
/**
|
|
970
|
-
* Returns an address owned by the wallet that should be used as a change address to return leftover assets during transaction creation back to the connected wallet.
|
|
971
|
-
*
|
|
972
|
-
* @returns an address
|
|
973
|
-
*/
|
|
974
|
-
getChangeAddress() {
|
|
975
|
-
return this._wallet.getPaymentAddress();
|
|
976
|
-
}
|
|
977
|
-
/**
|
|
978
|
-
* This function shall return a list of one or more UTXOs (unspent transaction outputs) controlled by the wallet that are required to reach AT LEAST the combined ADA value target specified in amount AND the best suitable to be used as collateral inputs for transactions with plutus script inputs (pure ADA-only UTXOs).
|
|
979
|
-
*
|
|
980
|
-
* If this cannot be attained, an error message with an explanation of the blocking problem shall be returned. NOTE: wallets are free to return UTXOs that add up to a greater total ADA value than requested in the amount parameter, but wallets must never return any result where UTXOs would sum up to a smaller total ADA value, instead in a case like that an error message must be returned.
|
|
981
|
-
*
|
|
982
|
-
* @returns a list of UTXOs
|
|
983
|
-
*/
|
|
984
|
-
async getCollateral(addressType = "payment") {
|
|
985
|
-
const utxos = await this._wallet.getCollateralUnspentOutput(0, addressType);
|
|
986
|
-
return utxos.map((utxo, i) => {
|
|
987
|
-
return (0, import_core_cst4.fromTxUnspentOutput)(utxo);
|
|
988
|
-
});
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* Returns the network ID of the currently connected account. 0 is testnet and 1 is mainnet but other networks can possibly be returned by wallets. Those other network ID values are not governed by CIP-30. This result will stay the same unless the connected account has changed.
|
|
992
|
-
*
|
|
993
|
-
* @returns network ID
|
|
994
|
-
*/
|
|
995
|
-
getNetworkId() {
|
|
996
|
-
return this._wallet.getNetworkId();
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
* Returns a list of reward addresses owned by the wallet. A reward address is a stake address that is used to receive rewards from staking, generally starts from `stake` prefix.
|
|
1000
|
-
*
|
|
1001
|
-
* @returns a list of reward addresses
|
|
1002
|
-
*/
|
|
1003
|
-
getRewardAddresses() {
|
|
1004
|
-
return [this._wallet.getRewardAddress()];
|
|
1005
|
-
}
|
|
1006
|
-
/**
|
|
1007
|
-
* Returns a list of unused addresses controlled by the wallet.
|
|
1008
|
-
*
|
|
1009
|
-
* @returns a list of unused addresses
|
|
1010
|
-
*/
|
|
1011
|
-
getUnusedAddresses() {
|
|
1012
|
-
return [this.getChangeAddress()];
|
|
1013
|
-
}
|
|
1014
|
-
/**
|
|
1015
|
-
* Returns a list of used addresses controlled by the wallet.
|
|
1016
|
-
*
|
|
1017
|
-
* @returns a list of used addresses
|
|
1018
|
-
*/
|
|
1019
|
-
getUsedAddresses() {
|
|
1020
|
-
return [this.getChangeAddress()];
|
|
1021
|
-
}
|
|
1022
|
-
/**
|
|
1023
|
-
* Get a list of UTXOs to be used as collateral inputs for transactions with plutus script inputs.
|
|
1024
|
-
*
|
|
1025
|
-
* This is used in transaction building.
|
|
1026
|
-
*
|
|
1027
|
-
* @returns a list of UTXOs
|
|
1028
|
-
*/
|
|
1029
|
-
async getUsedCollateral() {
|
|
1030
|
-
const collateralUtxo = await this.getCollateral();
|
|
1031
|
-
const unspentOutput = collateralUtxo.map((utxo) => {
|
|
1032
|
-
return (0, import_core_cst4.toTxUnspentOutput)(utxo);
|
|
1033
|
-
});
|
|
1034
|
-
return unspentOutput;
|
|
1035
|
-
}
|
|
1036
|
-
/**
|
|
1037
|
-
* Get a list of UTXOs to be used for transaction building.
|
|
1038
|
-
*
|
|
1039
|
-
* This is used in transaction building.
|
|
1040
|
-
*
|
|
1041
|
-
* @returns a list of UTXOs
|
|
1042
|
-
*/
|
|
1043
|
-
async getUsedUTxOs(addressType) {
|
|
1044
|
-
return await this.getUnspentOutputs(addressType);
|
|
1045
|
-
}
|
|
1046
|
-
/**
|
|
1047
|
-
* Return a list of all UTXOs (unspent transaction outputs) controlled by the wallet.
|
|
1048
|
-
*
|
|
1049
|
-
* @returns a list of UTXOs
|
|
1050
|
-
*/
|
|
1051
|
-
async getUtxos(addressType) {
|
|
1052
|
-
const utxos = await this.getUsedUTxOs(addressType);
|
|
1053
|
-
return utxos.map((c) => (0, import_core_cst4.fromTxUnspentOutput)(c));
|
|
1054
|
-
}
|
|
1055
|
-
/**
|
|
1056
|
-
* This endpoint utilizes the [CIP-8 - Message Signing](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0030) to sign arbitrary data, to verify the data was signed by the owner of the private key.
|
|
1057
|
-
*
|
|
1058
|
-
* Here, we get the first wallet's address with wallet.getUsedAddresses(), alternativelly you can use reward addresses (getRewardAddresses()) too. It's really up to you as the developer which address you want to use in your application.
|
|
1059
|
-
*
|
|
1060
|
-
* @param address
|
|
1061
|
-
* @param payload
|
|
1062
|
-
* @returns a signature
|
|
1063
|
-
*/
|
|
1064
|
-
signData(payload) {
|
|
1065
|
-
return this._wallet.signData(this.getChangeAddress(), payload);
|
|
1066
|
-
}
|
|
1067
|
-
/**
|
|
1068
|
-
* Requests user to sign the provided transaction (tx). The wallet should ask the user for permission, and if given, try to sign the supplied body and return a signed transaction. partialSign should be true if the transaction provided requires multiple signatures.
|
|
1069
|
-
*
|
|
1070
|
-
* @param unsignedTx
|
|
1071
|
-
* @param partialSign
|
|
1072
|
-
* @returns a signed transaction in CBOR
|
|
1073
|
-
*/
|
|
1074
|
-
signTx(unsignedTx, partialSign = false) {
|
|
1075
|
-
return this._wallet.signTx(unsignedTx, partialSign);
|
|
1076
|
-
}
|
|
1077
|
-
/**
|
|
1078
|
-
* Experimental feature - sign multiple transactions at once.
|
|
1079
|
-
*
|
|
1080
|
-
* @param unsignedTxs - array of unsigned transactions in CborHex string
|
|
1081
|
-
* @param partialSign - if the transactions are signed partially
|
|
1082
|
-
* @returns array of signed transactions CborHex string
|
|
1083
|
-
*/
|
|
1084
|
-
signTxs(unsignedTxs, partialSign = false) {
|
|
1085
|
-
const signedTxs = [];
|
|
1086
|
-
for (const unsignedTx of unsignedTxs) {
|
|
1087
|
-
const signedTx = this.signTx(unsignedTx, partialSign);
|
|
1088
|
-
signedTxs.push(signedTx);
|
|
1089
|
-
}
|
|
1090
|
-
return signedTxs;
|
|
1091
|
-
}
|
|
1092
|
-
/**
|
|
1093
|
-
* Submits the signed transaction to the blockchain network.
|
|
1094
|
-
*
|
|
1095
|
-
* As wallets should already have this ability to submit transaction, we allow dApps to request that a transaction be sent through it. If the wallet accepts the transaction and tries to send it, it shall return the transaction ID for the dApp to track. The wallet can return error messages or failure if there was an error in sending it.
|
|
1096
|
-
*
|
|
1097
|
-
* @param tx
|
|
1098
|
-
* @returns a transaction hash
|
|
1099
|
-
*/
|
|
1100
|
-
async submitTx(tx) {
|
|
1101
|
-
return await this._wallet.submitTx(tx);
|
|
1102
|
-
}
|
|
1103
|
-
/**
|
|
1104
|
-
* Get a used address of type Address from the wallet.
|
|
1105
|
-
*
|
|
1106
|
-
* This is used in transaction building.
|
|
1107
|
-
*
|
|
1108
|
-
* @returns an Address object
|
|
1109
|
-
*/
|
|
1110
|
-
getUsedAddress(addressType) {
|
|
1111
|
-
return this._wallet.getUsedAddress(0, 0, addressType);
|
|
1112
|
-
}
|
|
1113
|
-
/**
|
|
1114
|
-
* Get a list of UTXOs to be used for transaction building.
|
|
1115
|
-
*
|
|
1116
|
-
* This is used in transaction building.
|
|
1117
|
-
*
|
|
1118
|
-
* @returns a list of UTXOs
|
|
1119
|
-
*/
|
|
1120
|
-
async getUnspentOutputs(addressType) {
|
|
1121
|
-
return await this._wallet.getUnspentOutputs(0, addressType);
|
|
1122
|
-
}
|
|
1123
|
-
/**
|
|
1124
|
-
* A helper function to get the assets in the wallet.
|
|
1125
|
-
*
|
|
1126
|
-
* @returns a list of assets
|
|
1127
|
-
*/
|
|
1128
|
-
async getAssets() {
|
|
1129
|
-
const balance = await this.getBalance();
|
|
1130
|
-
return balance.filter((v) => v.unit !== "lovelace").map((v) => {
|
|
1131
|
-
const policyId = v.unit.slice(0, import_common3.POLICY_ID_LENGTH);
|
|
1132
|
-
const assetName = v.unit.slice(import_common3.POLICY_ID_LENGTH);
|
|
1133
|
-
const fingerprint = (0, import_common3.resolveFingerprint)(policyId, assetName);
|
|
1134
|
-
return {
|
|
1135
|
-
unit: v.unit,
|
|
1136
|
-
policyId,
|
|
1137
|
-
assetName: (0, import_common3.toUTF8)(assetName),
|
|
1138
|
-
fingerprint,
|
|
1139
|
-
quantity: v.quantity
|
|
1140
|
-
};
|
|
1141
|
-
});
|
|
1142
|
-
}
|
|
1143
|
-
/**
|
|
1144
|
-
* A helper function to get the lovelace balance in the wallet.
|
|
1145
|
-
*
|
|
1146
|
-
* @returns lovelace balance
|
|
1147
|
-
*/
|
|
1148
|
-
async getLovelace() {
|
|
1149
|
-
const balance = await this.getBalance();
|
|
1150
|
-
const nativeAsset = balance.find((v) => v.unit === "lovelace");
|
|
1151
|
-
return nativeAsset !== void 0 ? nativeAsset.quantity : "0";
|
|
1152
|
-
}
|
|
1153
|
-
/**
|
|
1154
|
-
* A helper function to get the assets of a specific policy ID in the wallet.
|
|
1155
|
-
*
|
|
1156
|
-
* @param policyId
|
|
1157
|
-
* @returns a list of assets
|
|
1158
|
-
*/
|
|
1159
|
-
async getPolicyIdAssets(policyId) {
|
|
1160
|
-
const assets = await this.getAssets();
|
|
1161
|
-
return assets.filter((v) => v.policyId === policyId);
|
|
1162
|
-
}
|
|
1163
|
-
/**
|
|
1164
|
-
* A helper function to get the policy IDs of all the assets in the wallet.
|
|
1165
|
-
*
|
|
1166
|
-
* @returns a list of policy IDs
|
|
1167
|
-
*/
|
|
1168
|
-
async getPolicyIds() {
|
|
1169
|
-
const balance = await this.getBalance();
|
|
1170
|
-
return Array.from(
|
|
1171
|
-
new Set(balance.map((v) => v.unit.slice(0, import_common3.POLICY_ID_LENGTH)))
|
|
1172
|
-
).filter((p) => p !== "lovelace");
|
|
1173
|
-
}
|
|
1174
|
-
/**
|
|
1175
|
-
* A helper function to create a collateral input for a transaction.
|
|
1176
|
-
*
|
|
1177
|
-
* @returns a transaction hash
|
|
1178
|
-
*/
|
|
1179
|
-
async createCollateral() {
|
|
1180
|
-
const tx = new import_transaction.Transaction({ initiator: this });
|
|
1181
|
-
tx.sendLovelace(this.getChangeAddress(), "5000000");
|
|
1182
|
-
const unsignedTx = await tx.build();
|
|
1183
|
-
const signedTx = await this.signTx(unsignedTx);
|
|
1184
|
-
const txHash = await this.submitTx(signedTx);
|
|
1185
|
-
return txHash;
|
|
1186
|
-
}
|
|
1187
|
-
/**
|
|
1188
|
-
* Generate mnemonic or private key
|
|
1189
|
-
*
|
|
1190
|
-
* @param privateKey return private key if true
|
|
1191
|
-
* @returns a transaction hash
|
|
1192
|
-
*/
|
|
1193
|
-
static brew(privateKey = false, strength = 256) {
|
|
1194
|
-
const mnemonic = EmbeddedWallet.generateMnemonic(strength);
|
|
1195
|
-
if (privateKey) {
|
|
1196
|
-
return (0, import_core_cst4.resolvePrivateKey)(mnemonic);
|
|
1197
|
-
}
|
|
1198
|
-
return mnemonic;
|
|
1199
|
-
}
|
|
1200
|
-
};
|
|
1201
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1202
|
-
0 && (module.exports = {
|
|
1203
|
-
AppWallet,
|
|
1204
|
-
BrowserWallet,
|
|
1205
|
-
EmbeddedWallet,
|
|
1206
|
-
MeshWallet,
|
|
1207
|
-
WalletStaticMethods
|
|
1208
|
-
});
|
|
1
|
+
import{deserializeTx as je,toAddress as H,toTxUnspentOutput as Ne}from"@meshsdk/core-cst";function m(i){if(!Number.isSafeInteger(i))throw new Error(`Wrong integer: ${i}`)}function X(i){return i instanceof Uint8Array||i!=null&&typeof i=="object"&&i.constructor.name==="Uint8Array"}function q(...i){let e=s=>s,t=(s,o)=>a=>s(o(a)),r=i.map(s=>s.encode).reduceRight(t,e),n=i.map(s=>s.decode).reduce(t,e);return{encode:r,decode:n}}function ee(i){return{encode:e=>{if(!Array.isArray(e)||e.length&&typeof e[0]!="number")throw new Error("alphabet.encode input should be an array of numbers");return e.map(t=>{if(t<0||t>=i.length)throw new Error(`Digit index outside alphabet: ${t} (alphabet: ${i.length})`);return i[t]})},decode:e=>{if(!Array.isArray(e)||e.length&&typeof e[0]!="string")throw new Error("alphabet.decode input should be array of strings");return e.map(t=>{if(typeof t!="string")throw new Error(`alphabet.decode: not string element=${t}`);let r=i.indexOf(t);if(r===-1)throw new Error(`Unknown letter: "${t}". Allowed: ${i}`);return r})}}}function te(i=""){if(typeof i!="string")throw new Error("join separator should be string");return{encode:e=>{if(!Array.isArray(e)||e.length&&typeof e[0]!="string")throw new Error("join.encode input should be array of strings");for(let t of e)if(typeof t!="string")throw new Error(`join.encode: non-string input=${t}`);return e.join(i)},decode:e=>{if(typeof e!="string")throw new Error("join.decode input should be string");return e.split(i)}}}var v=(i,e)=>e?v(e,i%e):i,h=(i,e)=>i+(e-v(i,e));function T(i,e,t,r){if(!Array.isArray(i))throw new Error("convertRadix2: data should be array");if(e<=0||e>32)throw new Error(`convertRadix2: wrong from=${e}`);if(t<=0||t>32)throw new Error(`convertRadix2: wrong to=${t}`);if(h(e,t)>32)throw new Error(`convertRadix2: carry overflow from=${e} to=${t} carryBits=${h(e,t)}`);let n=0,s=0,o=2**t-1,a=[];for(let c of i){if(c>=2**e)throw new Error(`convertRadix2: invalid data word=${c} from=${e}`);if(n=n<<e|c,s+e>32)throw new Error(`convertRadix2: carry overflow pos=${s} from=${e}`);for(s+=e;s>=t;s-=t)a.push((n>>s-t&o)>>>0);n&=2**s-1}if(n=n<<t-s&o,!r&&s>=e)throw new Error("Excess padding");if(!r&&n)throw new Error(`Non-zero padding: ${n}`);return r&&s>0&&a.push(n>>>0),a}function re(i,e=!1){if(i<=0||i>32)throw new Error("radix2: bits should be in (0..32]");if(h(8,i)>32||h(i,8)>32)throw new Error("radix2: carry overflow");return{encode:t=>{if(!X(t))throw new Error("radix2.encode input should be Uint8Array");return T(Array.from(t),8,i,!e)},decode:t=>{if(!Array.isArray(t)||t.length&&typeof t[0]!="number")throw new Error("radix2.decode input should be array of numbers");return Uint8Array.from(T(t,i,8,e))}}}function U(i){if(typeof i!="function")throw new Error("unsafeWrapper fn should be function");return function(...e){try{return i.apply(null,e)}catch{}}}var E=q(ee("qpzry9x8gf2tvdw0s3jn54khce6mua7l"),te("")),Y=[996825010,642813549,513874426,1027748829,705979059];function L(i){let e=i>>25,t=(i&33554431)<<5;for(let r=0;r<Y.length;r++)(e>>r&1)===1&&(t^=Y[r]);return t}function B(i,e,t=1){let r=i.length,n=1;for(let s=0;s<r;s++){let o=i.charCodeAt(s);if(o<33||o>126)throw new Error(`Invalid prefix (${i})`);n=L(n)^o>>5}n=L(n);for(let s=0;s<r;s++)n=L(n)^i.charCodeAt(s)&31;for(let s of e)n=L(n)^s;for(let s=0;s<6;s++)n=L(n);return n^=t,E.encode(T([n%2**30],30,5,!1))}function se(i){let e=i==="bech32"?1:734539939,t=re(5),r=t.decode,n=t.encode,s=U(r);function o(u,g,M=90){if(typeof u!="string")throw new Error(`bech32.encode prefix should be string, not ${typeof u}`);if(!Array.isArray(g)||g.length&&typeof g[0]!="number")throw new Error(`bech32.encode words should be array of numbers, not ${typeof g}`);if(u.length===0)throw new TypeError(`Invalid prefix length ${u.length}`);let y=u.length+7+g.length;if(M!==!1&&y>M)throw new TypeError(`Length ${y} exceeds limit ${M}`);let l=u.toLowerCase(),w=B(l,g,e);return`${l}1${E.encode(g)}${w}`}function a(u,g=90){if(typeof u!="string")throw new Error(`bech32.decode input should be string, not ${typeof u}`);if(u.length<8||g!==!1&&u.length>g)throw new TypeError(`Wrong string length: ${u.length} (${u}). Expected (8..${g})`);let M=u.toLowerCase();if(u!==M&&u!==u.toUpperCase())throw new Error("String must be lowercase or uppercase");let y=M.lastIndexOf("1");if(y===0||y===-1)throw new Error('Letter "1" must be present between prefix and data only');let l=M.slice(0,y),w=M.slice(y+1);if(w.length<6)throw new Error("Data must be at least 6 characters long");let k=E.decode(w).slice(0,-6),O=B(l,k,e);if(!w.endsWith(O))throw new Error(`Invalid checksum in ${u}: expected "${O}"`);return{prefix:l,words:k}}let c=U(a);function D(u){let{prefix:g,words:M}=a(u,!1);return{prefix:g,words:M,bytes:r(M)}}return{encode:o,decode:a,decodeToBytes:D,decodeUnsafe:c,fromWords:r,fromWordsUnsafe:s,toWords:n}}var P=se("bech32");import{bytesToHex as W,generateMnemonic as ie,mnemonicToEntropy as oe}from"@meshsdk/common";import{Bip32PrivateKey as ae,buildBaseAddress as ue,buildBip32PrivateKey as de,buildEnterpriseAddress as ce,buildKeys as ge,buildRewardAddress as Me,deserializeTx as ye,deserializeTxHash as Ie,Ed25519KeyHashHex as j,Ed25519PublicKeyHex as le,Ed25519SignatureHex as we,Hash28ByteBase16 as N,resolveTxHash as Le,Serialization as Ae,signData as xe,Transaction as he,VkeyWitness as Q}from"@meshsdk/core-cst";var I=class{static privateKeyToEntropy(e){let t=P.decodeToBytes(e).bytes,r=ae.fromBytes(t);return W(r.bytes())}static mnemonicToEntropy(e){let t=oe(e.join(" ")),r=de(t);return W(r.bytes())}static signingKeyToEntropy(e,t){return[e.startsWith("5820")?e.slice(4):e,t.startsWith("5820")?t.slice(4):t]}static getAddresses(e,t,r=0){let n=ue(r,N.fromEd25519KeyHashHex(j(e.toPublicKey().hash().toString("hex"))),N.fromEd25519KeyHashHex(j(t.toPublicKey().hash().toString("hex")))),s=ce(r,N.fromEd25519KeyHashHex(j(e.toPublicKey().hash().toString("hex")))),o=Me(r,N.fromEd25519KeyHashHex(j(t.toPublicKey().hash().toString("hex"))));return{baseAddress:n.toAddress(),enterpriseAddress:s.toAddress(),rewardAddress:o.toAddress()}}static generateMnemonic(e=256){return ie(e).split(" ")}static addWitnessSets(e,t){let r=ye(e),n=r.witnessSet(),s=n.vkeys(),o=s?[...s.values(),...t]:t;return n.setVkeys(Ae.CborSet.fromCore(o.map(a=>a.toCore()),Q.fromCore)),new he(r.body(),n,r.auxiliaryData()).toCbor()}},d=class extends I{_entropy;_networkId;constructor(e){switch(super(),this._networkId=e.networkId,e.key.type){case"mnemonic":this._entropy=I.mnemonicToEntropy(e.key.words);break;case"root":this._entropy=I.privateKeyToEntropy(e.key.bech32);break;case"cli":this._entropy=I.signingKeyToEntropy(e.key.payment,e.key.stake??"f0".repeat(32));break}}getAccount(e=0,t=0){if(this._entropy==null)throw new Error("[EmbeddedWallet] No keys initialized");let{paymentKey:r,stakeKey:n}=ge(this._entropy,e,t),{baseAddress:s,enterpriseAddress:o,rewardAddress:a}=I.getAddresses(r,n,this._networkId);return{baseAddress:s,enterpriseAddress:o,rewardAddress:a,baseAddressBech32:s.toBech32(),enterpriseAddressBech32:o.toBech32(),rewardAddressBech32:a.toBech32(),paymentKey:r,stakeKey:n,paymentKeyHex:r.toBytes().toString("hex"),stakeKeyHex:n.toBytes().toString("hex")}}getNetworkId(){return this._networkId}signData(e,t,r=0,n=0){try{let s=this.getAccount(r,n);if([s.baseAddress,s.enterpriseAddress,s.rewardAddress].find(a=>a.toBech32()===e)===void 0)throw new Error(`[EmbeddedWallet] Address: ${e} doesn't belong to this account.`);return xe(t,s.paymentKey)}catch(s){throw new Error(`[EmbeddedWallet] An error occurred during signData: ${s}.`)}}signTx(e,t=0,r=0){try{let n=Ie(Le(e)),s=this.getAccount(t,r);return new Q(le(s.paymentKey.toPublicKey().toBytes().toString("hex")),we(s.paymentKey.sign(Buffer.from(n,"hex")).toString("hex")))}catch(n){throw new Error(`[EmbeddedWallet] An error occurred during signTx: ${n}.`)}}};var G=class{_fetcher;_submitter;_wallet;constructor(e){switch(this._fetcher=e.fetcher,this._submitter=e.submitter,e.key.type){case"mnemonic":this._wallet=new d({networkId:e.networkId,key:{type:"mnemonic",words:e.key.words}});break;case"root":this._wallet=new d({networkId:e.networkId,key:{type:"root",bech32:e.key.bech32}});break;case"cli":this._wallet=new d({networkId:e.networkId,key:{type:"cli",payment:e.key.payment,stake:e.key.stake}})}}async getCollateralUnspentOutput(e=0,t="payment"){let n=(await this.getUnspentOutputs(e,t)).filter(s=>s.output().amount().multiasset()===void 0);n.sort((s,o)=>Number(s.output().amount().coin())-Number(o.output().amount().coin()));for(let s of n)if(Number(s.output().amount().coin())>=5e6)return[s];return[]}getEnterpriseAddress(e=0,t=0){return this._wallet.getAccount(e,t).enterpriseAddressBech32}getPaymentAddress(e=0,t=0){return this._wallet.getAccount(e,t).baseAddressBech32}getRewardAddress(e=0,t=0){return this._wallet.getAccount(e,t).rewardAddressBech32}getNetworkId(){return this._wallet.getNetworkId()}getUsedAddress(e=0,t=0,r="payment"){return r==="enterprise"?H(this.getEnterpriseAddress(e,t)):H(this.getPaymentAddress(e,t))}async getUnspentOutputs(e=0,t="payment"){if(!this._fetcher)throw new Error("[AppWallet] Fetcher is required to fetch UTxOs. Please provide a fetcher.");let r=this._wallet.getAccount(e);return(await this._fetcher.fetchAddressUTxOs(t=="enterprise"?r.enterpriseAddressBech32:r.baseAddressBech32)).map(s=>Ne(s))}signData(e,t,r=0,n=0){try{return this._wallet.signData(e,t,r,n)}catch(s){throw new Error(`[AppWallet] An error occurred during signData: ${s}.`)}}signTx(e,t=!1,r=0,n=0){try{let s=je(e);if(!t&&s.witnessSet().vkeys()!==void 0&&s.witnessSet().vkeys().size()!==0)throw new Error("Signatures already exist in the transaction in a non partial sign call");let o=this._wallet.signTx(e,r,n);return d.addWitnessSets(e,[o])}catch(s){throw new Error(`[AppWallet] An error occurred during signTx: ${s}.`)}}signTxSync(e,t=!1,r=0,n=0){try{return"signedTx"}catch(s){throw new Error(`[AppWallet] An error occurred during signTx: ${s}.`)}}async signTxs(e,t){throw new Error("[AppWallet] signTxs() is not implemented.")}submitTx(e){if(!this._submitter)throw new Error("[AppWallet] Submitter is required to submit transactions. Please provide a submitter.");return this._submitter.submitTx(e)}static brew(e=256){return d.generateMnemonic(e)}};import{DEFAULT_PROTOCOL_PARAMETERS as De,fromUTF8 as Te,POLICY_ID_LENGTH as f,resolveFingerprint as Ee}from"@meshsdk/common";import{addressToBech32 as p,CardanoSDKUtil as me,deserializeAddress as A,deserializeTx as fe,deserializeTxUnspentOutput as S,deserializeValue as Se,fromTxUnspentOutput as F,fromValue as be,Serialization as Z,toAddress as ze,Transaction as ke,VkeyWitness as Oe}from"@meshsdk/core-cst";import{initNufiDappCardanoSdk as pe}from"@nufi/dapp-client-cardano";import Ce from"@nufi/dapp-client-core";var _={production:"https://wallet.nu.fi",mainnet:"https://wallet-staging.nu.fi",preprod:"https://wallet-testnet-staging.nu.fi",preview:"https://wallet-preview-staging.nu.fi"};function K(i="preprod"){try{let e=Ce.default;return Object.keys(_).includes(i)?e.init(_[i]):e.init(i),new Promise(t=>{e.getApi().isMetamaskInstalled().then(r=>{r?(pe(e,"snap"),t({id:"nufiSnap",name:"MetaMask",icon:"data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyBpZD0iTGF5ZXJfMiIgZGF0YS1uYW1lPSJMYXllciAyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNjkuMiAxNjMuNzEiPgogIDxkZWZzPgogICAgPHN0eWxlPgogICAgICAuY2xzLTEgewogICAgICAgIGZpbGw6ICNlMjc2MjU7CiAgICAgIH0KCiAgICAgIC5jbHMtMSwgLmNscy0yLCAuY2xzLTMsIC5jbHMtNCwgLmNscy01LCAuY2xzLTYsIC5jbHMtNywgLmNscy04LCAuY2xzLTkgewogICAgICAgIHN0cm9rZS13aWR0aDogMHB4OwogICAgICB9CgogICAgICAuY2xzLTIgewogICAgICAgIGZpbGw6ICM3NjNlMWE7CiAgICAgIH0KCiAgICAgIC5jbHMtMyB7CiAgICAgICAgZmlsbDogI2MwYWQ5ZTsKICAgICAgfQoK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0xNTcuODMsMTIxLjE2Yy43NywwLDEuNC42MywxLjQsMS40LDAsLjc3LS42MywxLjQtMS40LDEuNHMtMS40LS42My0xLjQtMS40aDBjMC0uNzguNjMtMS40LDEuNC0xLjRoMFpNOTguOTEsMTIxLjE2Yy43NywwLDEuNC42MywxLjQsMS40cy0uNjMsMS40LTEuNCwxLjQtMS40LS42My0xLjQtMS40aDBjMC0uNzguNjMtMS40LDEuNC0xLjRoMFoiLz4KICAgICAgPC9nPgogICAgPC9nPgogIDwvZz4KPC9zdmc+",version:"version"})):t(void 0)})})}catch{return Promise.resolve(void 0)}}var R=class i{constructor(e,t){this._walletInstance=e;this._walletName=t;this.walletInstance={...e}}walletInstance;static getInstalledWallets(){if(window===void 0)return[];if(window.cardano===void 0)return[];let e=[];for(let t in window.cardano)try{let r=window.cardano[t];if(r===void 0||r.name===void 0||r.icon===void 0||r.apiVersion===void 0)continue;e.push({id:t,name:t=="nufiSnap"?"MetaMask":r.name,icon:r.icon,version:r.apiVersion})}catch{}return e}static async getAvailableWallets({nufiNetwork:e="preprod"}={}){return window===void 0?[]:(await K(e),i.getInstalledWallets())}static async enable(e){try{let t=await i.resolveInstance(e);if(t!==void 0)return new i(t,e);throw new Error(`Couldn't create an instance of wallet: ${e}`)}catch(t){throw new Error(`[BrowserWallet] An error occurred during enable: ${JSON.stringify(t)}.`)}}async getBalance(){let e=await this._walletInstance.getBalance();return be(Se(e))}async getChangeAddress(){let e=await this._walletInstance.getChangeAddress();return p(A(e))}async getCollateral(){return(await this.getCollateralUnspentOutput()).map(t=>F(t))}getNetworkId(){return this._walletInstance.getNetworkId()}async getRewardAddresses(){return(await this._walletInstance.getRewardAddresses()).map(t=>p(A(t)))}async getUnusedAddresses(){return(await this._walletInstance.getUnusedAddresses()).map(t=>p(A(t)))}async getUsedAddresses(){return(await this._walletInstance.getUsedAddresses()).map(t=>p(A(t)))}async getUsedCollateral(e=De.maxCollateralInputs){return(await this._walletInstance.experimental.getCollateral()??[]).map(r=>S(r)).slice(0,e)}async getUtxos(){return(await this.getUsedUTxOs()).map(t=>F(t))}signData(e,t){let r=ze(e).toBytes().toString();return this._walletInstance.signData(r,Te(t))}async signTx(e,t=!1){let r=await this._walletInstance.signTx(e,t);return i.addBrowserWitnesses(e,r)}async signTxs(e,t=!1){let r;switch(this._walletName){case"Typhon Wallet":this._walletInstance.signTxs&&(r=await this._walletInstance.signTxs(e,t));break;default:this._walletInstance.signTxs?r=await this._walletInstance.signTxs(e.map(s=>({cbor:s,partialSign:t}))):this._walletInstance.experimental.signTxs&&(r=await this._walletInstance.experimental.signTxs(e.map(s=>({cbor:s,partialSign:t}))));break}if(!r)throw new Error("Wallet does not support signTxs");let n=[];for(let s=0;s<r.length;s++){let o=e[s],a=r[s],c=i.addBrowserWitnesses(o,a);n.push(c)}return n}submitTx(e){return this._walletInstance.submitTx(e)}async getUsedAddress(){let e=await this._walletInstance.getUsedAddresses();if(e.length===0)throw new Error("No used addresses found");return A(e[0])}async getCollateralUnspentOutput(){return(await this._walletInstance.experimental.getCollateral()??[]).map(t=>S(t))}async getUsedUTxOs(){return(await this._walletInstance.getUtxos()??[]).map(t=>S(t))}async getAssets(){return(await this.getBalance()).filter(t=>t.unit!=="lovelace").map(t=>{let r=t.unit.slice(0,f),n=t.unit.slice(f),s=Ee(r,n);return{unit:t.unit,policyId:r,assetName:n,fingerprint:s,quantity:t.quantity}})}async getLovelace(){let t=(await this.getBalance()).find(r=>r.unit==="lovelace");return t!==void 0?t.quantity:"0"}async getPolicyIdAssets(e){return(await this.getAssets()).filter(r=>r.policyId===e)}async getPolicyIds(){let e=await this.getBalance();return Array.from(new Set(e.map(t=>t.unit.slice(0,f)))).filter(t=>t!=="lovelace")}static resolveInstance(e){return window.cardano===void 0||window.cardano[e]===void 0?void 0:window.cardano[e]?.enable()}static addBrowserWitnesses(e,t){let r=Z.TransactionWitnessSet.fromCbor(me.HexBlob(t)).vkeys()?.values();if(r===void 0)return e;let n=fe(e),s=n.witnessSet(),o=s.vkeys(),a=o?[...o.values(),...r]:[...r];return s.setVkeys(Z.CborSet.fromCore(a.map(c=>c.toCore()),Oe.fromCore)),new ke(n.body(),s,n.auxiliaryData()).toCbor()}};import{POLICY_ID_LENGTH as b,resolveFingerprint as Ue,toUTF8 as Ye}from"@meshsdk/common";import{buildBaseAddress as Be,buildEnterpriseAddress as ve,buildRewardAddress as Pe,CardanoSDKSerializer as We,deserializeTx as Qe,Ed25519KeyHashHex as x,fromTxUnspentOutput as z,Hash28ByteBase16 as C,resolvePrivateKey as He,toAddress as $,toTxUnspentOutput as J}from"@meshsdk/core-cst";import{Transaction as Ge}from"@meshsdk/transaction";var V=class{_wallet;_accountIndex=0;_keyIndex=0;_fetcher;_submitter;_networkId;_addresses={};constructor(e){switch(e.key.type){case"root":this._wallet=new d({networkId:e.networkId,key:{type:"root",bech32:e.key.bech32}}),this.getAddressesFromWallet(this._wallet);break;case"cli":this._wallet=new d({networkId:e.networkId,key:{type:"cli",payment:e.key.payment,stake:e.key.stake}}),this.getAddressesFromWallet(this._wallet);break;case"mnemonic":this._wallet=new d({networkId:e.networkId,key:{type:"mnemonic",words:e.key.words}}),this.getAddressesFromWallet(this._wallet);break}this._networkId=e.networkId,e.fetcher&&(this._fetcher=e.fetcher),e.submitter&&(this._submitter=e.submitter),e.accountIndex&&(this._accountIndex=e.accountIndex),e.keyIndex&&(this._keyIndex=e.keyIndex)}async getBalance(){let e=await this.getUnspentOutputs(),t=new Map;return e.map(n=>{z(n).output.amount.map(o=>{let a=o.unit,c=Number(o.quantity);if(t.has(a)){let D=t.get(a);t.set(a,D+c)}else t.set(a,c)})}),Array.from(t,([n,s])=>({unit:n,quantity:s.toString()}))}getChangeAddress(){return this._addresses.baseAddressBech32}async getCollateral(e="payment"){return(await this.getCollateralUnspentOutput(e)).map((r,n)=>z(r))}async getCollateralUnspentOutput(e="payment"){let r=(await this.getUnspentOutputs(e)).filter(n=>n.output().amount().multiasset()===void 0);r.sort((n,s)=>Number(n.output().amount().coin())-Number(s.output().amount().coin()));for(let n of r)if(Number(n.output().amount().coin())>=5e6)return[n];return[]}getNetworkId(){return this._networkId}getRewardAddresses(){return[this._addresses.rewardAddressBech32]}getUnusedAddresses(){return[this.getChangeAddress()]}getUsedAddresses(){return[this.getChangeAddress()]}async getUsedCollateral(){return(await this.getCollateral()).map(r=>J(r))}async getUsedUTxOs(e){return await this.getUnspentOutputs(e)}async getUtxos(e){return(await this.getUsedUTxOs(e)).map(r=>z(r))}signData(e){if(!this._wallet)throw new Error("[MeshWallet] Read only wallet does not support signing data.");return this._wallet.signData(this.getChangeAddress(),e)}signTx(e,t=!1){if(!this._wallet)throw new Error("[MeshWallet] Read only wallet does not support signing data.");let r=Qe(e);if(!t&&r.witnessSet().vkeys()!==void 0&&r.witnessSet().vkeys().size()!==0)throw new Error("Signatures already exist in the transaction in a non partial sign call");let n=this._wallet.signTx(e,this._accountIndex,this._keyIndex);return d.addWitnessSets(e,[n])}signTxs(e,t=!1){let r=[];for(let n of e){let s=this.signTx(n,t);r.push(s)}return r}async submitTx(e){if(!this._submitter)throw new Error("[AppWallet] Submitter is required to submit transactions. Please provide a submitter.");return this._submitter.submitTx(e)}getUsedAddress(e){return e==="enterprise"?$(this._addresses.enterpriseAddressBech32):$(this._addresses.baseAddressBech32)}async getUnspentOutputs(e){if(!this._fetcher)throw new Error("[AppWallet] Fetcher is required to fetch UTxOs. Please provide a fetcher.");return(await this._fetcher.fetchAddressUTxOs(e=="enterprise"?this._addresses.enterpriseAddressBech32:this._addresses.baseAddressBech32)).map(r=>J(r))}async getAssets(){return(await this.getBalance()).filter(t=>t.unit!=="lovelace").map(t=>{let r=t.unit.slice(0,b),n=t.unit.slice(b),s=Ue(r,n);return{unit:t.unit,policyId:r,assetName:Ye(n),fingerprint:s,quantity:t.quantity}})}async getLovelace(){let t=(await this.getBalance()).find(r=>r.unit==="lovelace");return t!==void 0?t.quantity:"0"}async getPolicyIdAssets(e){return(await this.getAssets()).filter(r=>r.policyId===e)}async getPolicyIds(){let e=await this.getBalance();return Array.from(new Set(e.map(t=>t.unit.slice(0,b)))).filter(t=>t!=="lovelace")}async createCollateral(){let e=new Ge({initiator:this});e.sendLovelace(this.getChangeAddress(),"5000000");let t=await e.build(),r=await this.signTx(t);return await this.submitTx(r)}static brew(e=!1,t=256){let r=d.generateMnemonic(t);return e?He(r):r}getAddressesFromWallet(e){let t=e.getAccount(this._accountIndex,this._keyIndex);this._addresses={baseAddress:t.baseAddress,enterpriseAddress:t.enterpriseAddress,rewardAddress:t.rewardAddress,baseAddressBech32:t.baseAddressBech32,enterpriseAddressBech32:t.enterpriseAddressBech32,rewardAddressBech32:t.rewardAddressBech32}}buildAddressFromBech32Address(e){let r=new We().deserializer.key.deserializeAddress(e);r.pubKeyHash&&r.stakeCredentialHash&&(this._addresses.baseAddress=Be(this._networkId,C.fromEd25519KeyHashHex(x(r.pubKeyHash)),C.fromEd25519KeyHashHex(x(x(r.stakeCredentialHash)))).toAddress(),this._addresses.baseAddressBech32=this._addresses.baseAddress.toBech32()),r.pubKeyHash&&(this._addresses.enterpriseAddress=ve(this._networkId,C.fromEd25519KeyHashHex(x(r.pubKeyHash))).toAddress(),this._addresses.enterpriseAddressBech32=this._addresses.enterpriseAddress.toBech32()),r.stakeCredentialHash&&(this._addresses.rewardAddress=Pe(this._networkId,C.fromEd25519KeyHashHex(x(r.stakeCredentialHash))).toAddress(),this._addresses.rewardAddressBech32=this._addresses.rewardAddress.toBech32())}};export{G as AppWallet,R as BrowserWallet,d as EmbeddedWallet,V as MeshWallet,I as WalletStaticMethods};
|
|
1209
2
|
/*! Bundled license information:
|
|
1210
3
|
|
|
1211
4
|
@scure/base/lib/esm/index.js:
|