Skip to content

Commit 1927fa3

Browse files
committed
Wallet build improvements
1 parent 3b1dde2 commit 1927fa3

File tree

4 files changed

+57
-57
lines changed

4 files changed

+57
-57
lines changed

src/pages/api/v1/aggregatedBalances/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,28 @@ All endpoints require authentication using the `SNAPSHOT_AUTH_TOKEN` environment
1212

1313
### `/api/v1/aggregatedBalances/wallets`
1414
- **Method**: GET
15-
- **Purpose**: Returns all wallet information without fetching balances
15+
- **Purpose**: Returns all wallet information without fetching balances or building wallet objects
1616
- **Authentication**: Required (Bearer token)
1717
- **Response**:
1818
```json
1919
{
2020
"wallets": [
2121
{
2222
"walletId": "string",
23-
"walletName": "string",
23+
"walletName": "string",
24+
"description": "string|null",
2425
"signersAddresses": ["string"],
26+
"signersStakeKeys": ["string"],
27+
"signersDRepKeys": ["string"],
28+
"signersDescriptions": ["string"],
2529
"numRequiredSigners": number,
26-
"type": "string",
30+
"verified": ["string"],
31+
"scriptCbor": "string",
2732
"stakeCredentialHash": "string|null",
33+
"type": "string",
2834
"isArchived": boolean,
29-
"network": number,
30-
"paymentAddress": "string",
31-
"stakeableAddress": "string"
35+
"clarityApiKey": "string|null",
36+
"network": number
3237
}
3338
],
3439
"walletCount": number,
@@ -39,7 +44,7 @@ All endpoints require authentication using the `SNAPSHOT_AUTH_TOKEN` environment
3944

4045
### `/api/v1/aggregatedBalances/balance`
4146
- **Method**: GET
42-
- **Purpose**: Fetches balance for a single wallet
47+
- **Purpose**: Fetches balance for a single wallet (builds wallet and generates addresses internally)
4348
- **Authentication**: Required (Bearer token)
4449
- **Query Parameters**:
4550
- `walletId` (required) - Wallet ID
@@ -50,8 +55,6 @@ All endpoints require authentication using the `SNAPSHOT_AUTH_TOKEN` environment
5055
- `stakeCredentialHash` (optional) - Stake credential hash
5156
- `isArchived` (required) - Whether wallet is archived
5257
- `network` (required) - Network ID (0=testnet, 1=mainnet)
53-
- `paymentAddress` (required) - Payment address
54-
- `stakeableAddress` (required) - Stakeable address
5558
- **Response**:
5659
```json
5760
{

src/pages/api/v1/aggregatedBalances/balance.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ export default async function handler(
5353
return res.status(401).json({ error: "Unauthorized" });
5454
}
5555

56-
const { walletId, walletName, signersAddresses, numRequiredSigners, type, stakeCredentialHash, isArchived, network, paymentAddress, stakeableAddress } = req.query;
56+
const { walletId, walletName, signersAddresses, numRequiredSigners, type, stakeCredentialHash, isArchived, network } = req.query;
5757

5858
// Validate required parameters
59-
if (!walletId || !walletName || !signersAddresses || !numRequiredSigners || !type || !network || !paymentAddress || !stakeableAddress) {
59+
if (!walletId || !walletName || !signersAddresses || !numRequiredSigners || !type || !network) {
6060
return res.status(400).json({ error: "Missing required parameters" });
6161
}
6262

@@ -69,8 +69,6 @@ export default async function handler(
6969
const stakeCredentialHashStr = stakeCredentialHash as string;
7070
const isArchivedBool = isArchived === 'true';
7171
const networkNum = parseInt(network as string);
72-
const paymentAddressStr = paymentAddress as string;
73-
const stakeableAddressStr = stakeableAddress as string;
7472

7573
// Build multisig wallet for address determination
7674
const walletData = {
@@ -97,29 +95,50 @@ export default async function handler(
9795
return res.status(400).json({ error: "Failed to build multisig wallet" });
9896
}
9997

98+
// Generate addresses from the built wallet
99+
const nativeScript = {
100+
type: typeStr || "atLeast",
101+
scripts: signersAddressesArray.map((addr: string) => ({
102+
type: "sig",
103+
keyHash: resolvePaymentKeyHash(addr),
104+
})),
105+
};
106+
if (nativeScript.type == "atLeast") {
107+
//@ts-ignore
108+
nativeScript.required = numRequiredSignersNum;
109+
}
110+
111+
const paymentAddress = serializeNativeScript(
112+
nativeScript as NativeScript,
113+
stakeCredentialHashStr as undefined | string,
114+
networkNum,
115+
).address;
116+
117+
const stakeableAddress = mWallet.getScript().address;
118+
100119
// Determine which address to use
101120
const blockchainProvider = getProvider(networkNum);
102121

103122
let paymentUtxos: UTxO[] = [];
104123
let stakeableUtxos: UTxO[] = [];
105124

106125
try {
107-
paymentUtxos = await blockchainProvider.fetchAddressUTxOs(paymentAddressStr);
108-
stakeableUtxos = await blockchainProvider.fetchAddressUTxOs(stakeableAddressStr);
126+
paymentUtxos = await blockchainProvider.fetchAddressUTxOs(paymentAddress);
127+
stakeableUtxos = await blockchainProvider.fetchAddressUTxOs(stakeableAddress);
109128
} catch (utxoError) {
110129
console.error(`Failed to fetch UTxOs for wallet ${walletIdStr}:`, utxoError);
111130
// Continue with empty UTxOs
112131
}
113132

114133
const paymentAddrEmpty = paymentUtxos.length === 0;
115-
let walletAddress = paymentAddressStr;
134+
let walletAddress = paymentAddress;
116135

117136
if (paymentAddrEmpty && mWallet.stakingEnabled()) {
118-
walletAddress = stakeableAddressStr;
137+
walletAddress = stakeableAddress;
119138
}
120139

121140
// Use the UTxOs from the selected address
122-
let utxos: UTxO[] = walletAddress === stakeableAddressStr ? stakeableUtxos : paymentUtxos;
141+
let utxos: UTxO[] = walletAddress === stakeableAddress ? stakeableUtxos : paymentUtxos;
123142

124143
// If we still have no UTxOs, try the other network as fallback
125144
if (utxos.length === 0) {

src/pages/api/v1/aggregatedBalances/test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ export default async function handler(
151151
stakeCredentialHash: wallet.stakeCredentialHash || '',
152152
isArchived: wallet.isArchived.toString(),
153153
network: wallet.network.toString(),
154-
paymentAddress: wallet.paymentAddress,
155-
stakeableAddress: wallet.stakeableAddress,
156154
})}`;
157155

158156
const balanceResponse = await fetch(balanceUrl, { headers });

src/pages/api/v1/aggregatedBalances/wallets.ts

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import { cors, addCorsCacheBustingHeaders } from "@/lib/cors";
22
import type { Wallet as DbWallet } from "@prisma/client";
33
import type { NextApiRequest, NextApiResponse } from "next";
4-
import { buildMultisigWallet } from "@/utils/common";
54
import { addressToNetwork } from "@/utils/multisigSDK";
6-
import { resolvePaymentKeyHash, serializeNativeScript } from "@meshsdk/core";
75
import { db } from "@/server/db";
8-
import type { NativeScript } from "@meshsdk/core";
96

107
interface WalletInfo {
118
walletId: string;
129
walletName: string;
10+
description: string | null;
1311
signersAddresses: string[];
12+
signersStakeKeys: string[];
13+
signersDRepKeys: string[];
14+
signersDescriptions: string[];
1415
numRequiredSigners: number;
15-
type: string;
16+
verified: string[];
17+
scriptCbor: string;
1618
stakeCredentialHash: string | null;
19+
type: string;
1720
isArchived: boolean;
21+
clarityApiKey: string | null;
1822
network: number;
19-
paymentAddress: string;
20-
stakeableAddress: string;
2123
}
2224

2325
interface WalletsResponse {
@@ -78,7 +80,7 @@ export default async function handler(
7880
let activeWalletCount = 0;
7981
let archivedWalletCount = 0;
8082

81-
// Process each wallet to extract wallet information
83+
// Process each wallet to extract basic wallet information
8284
for (const wallet of allWallets) {
8385
try {
8486
// Determine network from signer addresses
@@ -87,33 +89,6 @@ export default async function handler(
8789
const signerAddr = wallet.signersAddresses[0]!;
8890
network = addressToNetwork(signerAddr);
8991
}
90-
91-
const mWallet = buildMultisigWallet(wallet, network);
92-
if (!mWallet) {
93-
console.warn(`Failed to build multisig wallet for wallet ${wallet.id}`);
94-
continue;
95-
}
96-
97-
// Use the same address logic as the frontend buildWallet function
98-
const nativeScript = {
99-
type: wallet.type ? wallet.type : "atLeast",
100-
scripts: wallet.signersAddresses.map((addr) => ({
101-
type: "sig",
102-
keyHash: resolvePaymentKeyHash(addr),
103-
})),
104-
};
105-
if (nativeScript.type == "atLeast") {
106-
//@ts-ignore
107-
nativeScript.required = wallet.numRequiredSigners!;
108-
}
109-
110-
const paymentAddress = serializeNativeScript(
111-
nativeScript as NativeScript,
112-
wallet.stakeCredentialHash as undefined | string,
113-
network,
114-
).address;
115-
116-
const stakeableAddress = mWallet.getScript().address;
11792

11893
// Count wallet types
11994
if (wallet.isArchived) {
@@ -125,14 +100,19 @@ export default async function handler(
125100
wallets.push({
126101
walletId: wallet.id,
127102
walletName: wallet.name,
103+
description: wallet.description,
128104
signersAddresses: wallet.signersAddresses,
105+
signersStakeKeys: wallet.signersStakeKeys,
106+
signersDRepKeys: wallet.signersDRepKeys,
107+
signersDescriptions: wallet.signersDescriptions,
129108
numRequiredSigners: wallet.numRequiredSigners!,
130-
type: wallet.type || "atLeast",
109+
verified: wallet.verified,
110+
scriptCbor: wallet.scriptCbor,
131111
stakeCredentialHash: wallet.stakeCredentialHash,
112+
type: wallet.type || "atLeast",
132113
isArchived: wallet.isArchived,
114+
clarityApiKey: wallet.clarityApiKey,
133115
network,
134-
paymentAddress,
135-
stakeableAddress,
136116
});
137117

138118
} catch (error) {

0 commit comments

Comments
 (0)