@txnlab/use-wallet 4.2.0 → 4.3.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/dist/index.cjs +152 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +27 -2
- package/dist/index.d.ts +27 -2
- package/dist/index.js +149 -5
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -3258,6 +3258,7 @@ __export(index_exports, {
|
|
|
3258
3258
|
SignDataError: () => SignDataError,
|
|
3259
3259
|
SignTxnsError: () => SignTxnsError,
|
|
3260
3260
|
StorageAdapter: () => StorageAdapter,
|
|
3261
|
+
W3Wallet: () => W3Wallet,
|
|
3261
3262
|
WalletConnect: () => WalletConnect,
|
|
3262
3263
|
WalletId: () => WalletId,
|
|
3263
3264
|
WalletManager: () => WalletManager,
|
|
@@ -3335,8 +3336,8 @@ var Logger = class _Logger {
|
|
|
3335
3336
|
var logger = Logger.getInstance();
|
|
3336
3337
|
|
|
3337
3338
|
// src/manager.ts
|
|
3338
|
-
var
|
|
3339
|
-
var
|
|
3339
|
+
var import_store14 = require("@tanstack/store");
|
|
3340
|
+
var import_algosdk13 = __toESM(require("algosdk"), 1);
|
|
3340
3341
|
|
|
3341
3342
|
// src/network.ts
|
|
3342
3343
|
var DEFAULT_NETWORK_CONFIG = {
|
|
@@ -3544,6 +3545,7 @@ var WalletId = /* @__PURE__ */ ((WalletId2) => {
|
|
|
3544
3545
|
WalletId2["MNEMONIC"] = "mnemonic";
|
|
3545
3546
|
WalletId2["PERA"] = "pera";
|
|
3546
3547
|
WalletId2["WALLETCONNECT"] = "walletconnect";
|
|
3548
|
+
WalletId2["W3_WALLET"] = "w3-wallet";
|
|
3547
3549
|
return WalletId2;
|
|
3548
3550
|
})(WalletId || {});
|
|
3549
3551
|
var SignTxnsError = class extends Error {
|
|
@@ -3682,7 +3684,7 @@ function isValidPersistedState(state) {
|
|
|
3682
3684
|
}
|
|
3683
3685
|
|
|
3684
3686
|
// src/utils.ts
|
|
3685
|
-
var
|
|
3687
|
+
var import_algosdk12 = __toESM(require("algosdk"), 1);
|
|
3686
3688
|
|
|
3687
3689
|
// src/wallets/walletconnect.ts
|
|
3688
3690
|
var import_algosdk2 = __toESM(require("algosdk"), 1);
|
|
@@ -6412,6 +6414,147 @@ var PeraWallet = class extends BaseWallet {
|
|
|
6412
6414
|
};
|
|
6413
6415
|
};
|
|
6414
6416
|
|
|
6417
|
+
// src/wallets/w3wallet.ts
|
|
6418
|
+
var import_algosdk11 = __toESM(require("algosdk"), 1);
|
|
6419
|
+
var ICON14 = `data:image/svg+xml;base64,${btoa(`
|
|
6420
|
+
<svg width="860" height="860" viewBox="0 0 860 860" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
6421
|
+
<rect width="860" height="860" rx="30" fill="#151923"/>
|
|
6422
|
+
<path d="M766 207L496.627 623.406C463.521 675.336 382.014 652.248 382.014 590.941V432.568L260.638 623.28C227.559 675.255 146 652.186 146 590.854V274.844H234.646V499.761L356.022 309.049C389.101 257.074 470.66 280.143 470.66 341.475V499.978L660.146 207L766 207Z" fill="#4BB7D1"/>
|
|
6423
|
+
</svg>
|
|
6424
|
+
`)}`;
|
|
6425
|
+
var W3Wallet = class extends BaseWallet {
|
|
6426
|
+
client = null;
|
|
6427
|
+
store;
|
|
6428
|
+
constructor({
|
|
6429
|
+
id,
|
|
6430
|
+
store,
|
|
6431
|
+
subscribe,
|
|
6432
|
+
getAlgodClient,
|
|
6433
|
+
metadata = {}
|
|
6434
|
+
}) {
|
|
6435
|
+
super({ id, metadata, getAlgodClient, store, subscribe });
|
|
6436
|
+
this.store = store;
|
|
6437
|
+
}
|
|
6438
|
+
static defaultMetadata = {
|
|
6439
|
+
name: "W3 Wallet",
|
|
6440
|
+
icon: ICON14
|
|
6441
|
+
};
|
|
6442
|
+
async initializeClient() {
|
|
6443
|
+
this.logger.info("Initializing client...");
|
|
6444
|
+
if (typeof window === "undefined" || window.w3walletAlgorand === void 0) {
|
|
6445
|
+
this.logger.error("W3 Wallet is not available.");
|
|
6446
|
+
throw new Error("W3 Wallet is not available.");
|
|
6447
|
+
}
|
|
6448
|
+
const client = window.w3walletAlgorand;
|
|
6449
|
+
this.client = client;
|
|
6450
|
+
this.logger.info("Client initialized");
|
|
6451
|
+
return client;
|
|
6452
|
+
}
|
|
6453
|
+
connect = async () => {
|
|
6454
|
+
this.logger.info("Connecting...");
|
|
6455
|
+
const client = this.client || await this.initializeClient();
|
|
6456
|
+
const activeAccount = await client.account();
|
|
6457
|
+
const walletState = {
|
|
6458
|
+
accounts: [activeAccount],
|
|
6459
|
+
activeAccount
|
|
6460
|
+
};
|
|
6461
|
+
addWallet(this.store, {
|
|
6462
|
+
walletId: this.id,
|
|
6463
|
+
wallet: walletState
|
|
6464
|
+
});
|
|
6465
|
+
this.logger.info("\u2705 Connected.", walletState);
|
|
6466
|
+
return [activeAccount];
|
|
6467
|
+
};
|
|
6468
|
+
disconnect = async () => {
|
|
6469
|
+
this.logger.info("Disconnecting...");
|
|
6470
|
+
this.onDisconnect();
|
|
6471
|
+
this.logger.info("Disconnected.");
|
|
6472
|
+
};
|
|
6473
|
+
resumeSession = async () => {
|
|
6474
|
+
try {
|
|
6475
|
+
const state = this.store.state;
|
|
6476
|
+
const walletState = state.wallets[this.id];
|
|
6477
|
+
if (!walletState) {
|
|
6478
|
+
this.logger.info("No session to resume");
|
|
6479
|
+
return;
|
|
6480
|
+
}
|
|
6481
|
+
this.logger.info("Resuming session...");
|
|
6482
|
+
const client = await this.initializeClient();
|
|
6483
|
+
const isConnected = await client.isConnected();
|
|
6484
|
+
if (!isConnected) {
|
|
6485
|
+
this.logger.error("W3 Wallet is not connected.");
|
|
6486
|
+
throw new Error("W3 Wallet is not connected.");
|
|
6487
|
+
}
|
|
6488
|
+
this.logger.info("Session resumed");
|
|
6489
|
+
} catch (error) {
|
|
6490
|
+
this.logger.error("Error resuming session:", error.message);
|
|
6491
|
+
this.onDisconnect();
|
|
6492
|
+
throw error;
|
|
6493
|
+
}
|
|
6494
|
+
};
|
|
6495
|
+
processTxns(txnGroup, indexesToSign) {
|
|
6496
|
+
const txnsToSign = [];
|
|
6497
|
+
txnGroup.forEach((txn, index) => {
|
|
6498
|
+
const isIndexMatch = !indexesToSign || indexesToSign.includes(index);
|
|
6499
|
+
const signer = txn.sender.toString();
|
|
6500
|
+
const canSignTxn = this.addresses.includes(signer);
|
|
6501
|
+
const txnString = byteArrayToBase64(txn.toByte());
|
|
6502
|
+
if (isIndexMatch && canSignTxn) {
|
|
6503
|
+
txnsToSign.push({ txn: txnString });
|
|
6504
|
+
} else {
|
|
6505
|
+
txnsToSign.push({ txn: txnString, signers: [] });
|
|
6506
|
+
}
|
|
6507
|
+
});
|
|
6508
|
+
return txnsToSign;
|
|
6509
|
+
}
|
|
6510
|
+
processEncodedTxns(txnGroup, indexesToSign) {
|
|
6511
|
+
const txnsToSign = [];
|
|
6512
|
+
txnGroup.forEach((txnBuffer, index) => {
|
|
6513
|
+
const decodedObj = import_algosdk11.default.msgpackRawDecode(txnBuffer);
|
|
6514
|
+
const isSigned = isSignedTxn(decodedObj);
|
|
6515
|
+
const txn = isSigned ? import_algosdk11.default.decodeSignedTransaction(txnBuffer).txn : import_algosdk11.default.decodeUnsignedTransaction(txnBuffer);
|
|
6516
|
+
const isIndexMatch = !indexesToSign || indexesToSign.includes(index);
|
|
6517
|
+
const signer = txn.sender.toString();
|
|
6518
|
+
const canSignTxn = !isSigned && this.addresses.includes(signer);
|
|
6519
|
+
const txnString = byteArrayToBase64(txn.toByte());
|
|
6520
|
+
if (isIndexMatch && canSignTxn) {
|
|
6521
|
+
txnsToSign.push({ txn: txnString });
|
|
6522
|
+
} else {
|
|
6523
|
+
txnsToSign.push({ txn: txnString, signers: [] });
|
|
6524
|
+
}
|
|
6525
|
+
});
|
|
6526
|
+
return txnsToSign;
|
|
6527
|
+
}
|
|
6528
|
+
signTransactions = async (txnGroup, indexesToSign) => {
|
|
6529
|
+
try {
|
|
6530
|
+
this.logger.debug("Signing transactions...", { txnGroup, indexesToSign });
|
|
6531
|
+
let txnsToSign = [];
|
|
6532
|
+
if (isTransactionArray(txnGroup)) {
|
|
6533
|
+
const flatTxns = flattenTxnGroup(txnGroup);
|
|
6534
|
+
txnsToSign = this.processTxns(flatTxns, indexesToSign);
|
|
6535
|
+
} else {
|
|
6536
|
+
const flatTxns = flattenTxnGroup(txnGroup);
|
|
6537
|
+
txnsToSign = this.processEncodedTxns(flatTxns, indexesToSign);
|
|
6538
|
+
}
|
|
6539
|
+
const client = this.client || await this.initializeClient();
|
|
6540
|
+
this.logger.debug("Sending processed transactions to wallet...", txnsToSign);
|
|
6541
|
+
const signTxnsResult = await client.signTxns(txnsToSign);
|
|
6542
|
+
this.logger.debug("Received signed transactions from wallet", signTxnsResult);
|
|
6543
|
+
const result = signTxnsResult.map((value) => {
|
|
6544
|
+
if (value === null) {
|
|
6545
|
+
return null;
|
|
6546
|
+
}
|
|
6547
|
+
return base64ToByteArray(value);
|
|
6548
|
+
});
|
|
6549
|
+
this.logger.debug("Transactions signed successfully", result);
|
|
6550
|
+
return result;
|
|
6551
|
+
} catch (error) {
|
|
6552
|
+
this.logger.error("Error signing transactions:", error.message);
|
|
6553
|
+
throw error;
|
|
6554
|
+
}
|
|
6555
|
+
};
|
|
6556
|
+
};
|
|
6557
|
+
|
|
6415
6558
|
// src/utils.ts
|
|
6416
6559
|
function createWalletMap() {
|
|
6417
6560
|
return {
|
|
@@ -6427,7 +6570,8 @@ function createWalletMap() {
|
|
|
6427
6570
|
["magic" /* MAGIC */]: MagicAuth,
|
|
6428
6571
|
["mnemonic" /* MNEMONIC */]: MnemonicWallet,
|
|
6429
6572
|
["pera" /* PERA */]: PeraWallet,
|
|
6430
|
-
["walletconnect" /* WALLETCONNECT */]: WalletConnect
|
|
6573
|
+
["walletconnect" /* WALLETCONNECT */]: WalletConnect,
|
|
6574
|
+
["w3-wallet" /* W3_WALLET */]: W3Wallet
|
|
6431
6575
|
};
|
|
6432
6576
|
}
|
|
6433
6577
|
function compareAccounts(accounts, compareTo) {
|
|
@@ -6473,7 +6617,7 @@ function isSignedTxn(txnObj) {
|
|
|
6473
6617
|
return hasRequiredProps;
|
|
6474
6618
|
}
|
|
6475
6619
|
function isTransaction(item) {
|
|
6476
|
-
return item && typeof item === "object" && "sender" in item && (item.sender instanceof
|
|
6620
|
+
return item && typeof item === "object" && "sender" in item && (item.sender instanceof import_algosdk12.default.Address || typeof item.sender === "string");
|
|
6477
6621
|
}
|
|
6478
6622
|
function isTransactionArray(txnGroup) {
|
|
6479
6623
|
if (!Array.isArray(txnGroup) || txnGroup.length === 0) {
|
|
@@ -6544,7 +6688,7 @@ var WalletManager = class {
|
|
|
6544
6688
|
activeNetwork,
|
|
6545
6689
|
algodClient
|
|
6546
6690
|
};
|
|
6547
|
-
this.store = new
|
|
6691
|
+
this.store = new import_store14.Store(initialState, {
|
|
6548
6692
|
onUpdate: () => this.savePersistedState()
|
|
6549
6693
|
});
|
|
6550
6694
|
this.savePersistedState();
|
|
@@ -6727,7 +6871,7 @@ var WalletManager = class {
|
|
|
6727
6871
|
createAlgodClient(config) {
|
|
6728
6872
|
this.logger.info(`Creating new Algodv2 client...`);
|
|
6729
6873
|
const { token = "", baseServer, port = "", headers = {} } = config;
|
|
6730
|
-
return new
|
|
6874
|
+
return new import_algosdk13.default.Algodv2(token, baseServer, port, headers);
|
|
6731
6875
|
}
|
|
6732
6876
|
getAlgodClient = () => {
|
|
6733
6877
|
return this.algodClient;
|
|
@@ -6892,6 +7036,7 @@ var webpackFallback = {
|
|
|
6892
7036
|
SignDataError,
|
|
6893
7037
|
SignTxnsError,
|
|
6894
7038
|
StorageAdapter,
|
|
7039
|
+
W3Wallet,
|
|
6895
7040
|
WalletConnect,
|
|
6896
7041
|
WalletId,
|
|
6897
7042
|
WalletManager,
|