@0xsequence/connect
Version:
Connect package for Sequence Web SDK
129 lines • 4.88 kB
JavaScript
;
'use client';
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWaasLinkWallet = void 0;
const hooks_1 = require("@0xsequence/hooks");
const react_1 = require("react");
const walletLinking_js_1 = require("../constants/walletLinking.js");
/**
* Hook to manage wallet linking operations for WaaS (Wallet-as-a-Service).
*
* This hook provides functionality to link and unlink child wallets to/from a parent WaaS wallet.
* It handles the signature generation and API calls required for the linking process.
*
* The linking process involves:
* 1. Getting the parent WaaS wallet address
* 2. Signing a message with the parent wallet
* 3. Submitting both parent and child signatures to the API
*
* The unlinking process involves:
* 1. Getting the parent WaaS wallet address
* 2. Signing a removal message
* 3. Submitting the request to the API
*
* @param connector - The WaaS connector instance. Optional because the user might not have
* a WaaS wallet connected yet.
*
* @returns An object containing:
* - `linkWallet` - Function to link a child wallet
* - `removeLinkedWallet` - Function to remove a linked wallet
* - `loading` - Whether an operation is in progress
* - `error` - Any error that occurred
*
* @example
* ```tsx
* const { linkWallet, removeLinkedWallet, loading, error } = useWaasLinkWallet(waasConnector)
*
* // Link a wallet
* await linkWallet({
* signatureChainId: CHAIN_ID_FOR_SIGNATURE,
* connectorName: 'MetaMask',
* childWalletAddress: '0x...',
* childMessage: 'Link to parent wallet...',
* childSignature: '0x...'
* })
*
* // Remove a linked wallet
* await removeLinkedWallet('0x...')
* ```
*/
const useWaasLinkWallet = (connector) => {
const [loading, setLoading] = (0, react_1.useState)(false);
const [error, setError] = (0, react_1.useState)(null);
const apiClient = (0, hooks_1.useAPIClient)();
const linkWallet = async ({ signatureChainId, connectorName, childWalletAddress, childMessage, childSignature }) => {
const sequenceWaas = connector?.sequenceWaas;
try {
setLoading(true);
setError(null);
const parentWalletAddress = await sequenceWaas?.getAddress();
if (!parentWalletAddress) {
throw new Error('Failed to fetch WaaS address');
}
const parentWalletMessage = 'Link child wallet with address: ' + childWalletAddress;
const parentWalletSignature = await sequenceWaas?.signMessage({
message: parentWalletMessage,
network: walletLinking_js_1.CHAIN_ID_FOR_SIGNATURE
});
if (!parentWalletSignature) {
throw new Error('Failed to sign message');
}
await apiClient.linkWallet({
signatureChainId: String(signatureChainId),
linkedWalletType: connectorName,
parentWalletAddress,
parentWalletMessage,
parentWalletSignature: parentWalletSignature.data.signature,
linkedWalletAddress: childWalletAddress,
linkedWalletMessage: childMessage,
linkedWalletSignature: childSignature
});
}
catch (err) {
setError(err);
}
finally {
setLoading(false);
}
};
const removeLinkedWallet = async (linkedWalletAddress) => {
const sequenceWaas = connector?.sequenceWaas;
try {
setLoading(true);
setError(null);
const parentWalletAddress = await sequenceWaas?.getAddress();
if (!parentWalletAddress) {
throw new Error('Failed to fetch WaaS address');
}
const parentWalletMessage = 'Remove linked wallet with address: ' + linkedWalletAddress;
const parentWalletSignature = await sequenceWaas?.signMessage({
message: parentWalletMessage,
network: walletLinking_js_1.CHAIN_ID_FOR_SIGNATURE
});
if (!parentWalletSignature) {
throw new Error('Failed to sign message');
}
await apiClient.removeLinkedWallet({
parentWalletAddress,
parentWalletMessage,
parentWalletSignature: parentWalletSignature.data.signature,
linkedWalletAddress,
signatureChainId: String(walletLinking_js_1.CHAIN_ID_FOR_SIGNATURE)
});
}
catch (err) {
setError(err);
}
finally {
setLoading(false);
}
};
return {
linkWallet,
removeLinkedWallet,
loading,
error
};
};
exports.useWaasLinkWallet = useWaasLinkWallet;
//# sourceMappingURL=useWaasLinkWallet.js.map