Skip to content

Commit 0e3e5fa

Browse files
committed
fix: use bigint in PDP auth, clientDataSetId now needs to be large
1 parent 2b5f8d6 commit 0e3e5fa

File tree

9 files changed

+64
-81
lines changed

9 files changed

+64
-81
lines changed

packages/synapse-sdk/src/pdp/auth.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export class PDPAuthHelper {
204204
* ```
205205
*/
206206
async signCreateDataSet(
207-
clientDataSetId: number | bigint,
207+
clientDataSetId: bigint,
208208
payee: string,
209209
metadata: MetadataEntry[] = []
210210
): Promise<AuthSignature> {
@@ -226,7 +226,7 @@ export class PDPAuthHelper {
226226
} else {
227227
// Use standard ethers.js signing (for private keys, etc)
228228
const value = {
229-
clientDataSetId: BigInt(clientDataSetId),
229+
clientDataSetId,
230230
metadata,
231231
payee,
232232
}
@@ -241,7 +241,7 @@ export class PDPAuthHelper {
241241

242242
// For EIP-712, signedData contains the actual message hash that was signed
243243
const signedData = ethers.TypedDataEncoder.hash(this.domain, types, {
244-
clientDataSetId: BigInt(clientDataSetId),
244+
clientDataSetId,
245245
metadata,
246246
payee,
247247
})
@@ -282,8 +282,8 @@ export class PDPAuthHelper {
282282
* ```
283283
*/
284284
async signAddPieces(
285-
clientDataSetId: number | bigint,
286-
firstPieceId: number | bigint,
285+
clientDataSetId: bigint,
286+
firstPieceId: bigint,
287287
pieceDataArray: PieceCID[] | string[],
288288
metadata: MetadataEntry[][] = []
289289
): Promise<AuthSignature> {
@@ -341,8 +341,8 @@ export class PDPAuthHelper {
341341
} else {
342342
// Use standard ethers.js signing with bigint values
343343
const value = {
344-
clientDataSetId: BigInt(clientDataSetId),
345-
firstAdded: BigInt(firstPieceId),
344+
clientDataSetId,
345+
firstAdded: firstPieceId,
346346
pieceData: formattedPieceData,
347347
pieceMetadata: pieceMetadata,
348348
}
@@ -357,8 +357,8 @@ export class PDPAuthHelper {
357357

358358
// For EIP-712, signedData contains the actual message hash that was signed
359359
const signedData = ethers.TypedDataEncoder.hash(this.domain, types, {
360-
clientDataSetId: BigInt(clientDataSetId),
361-
firstAdded: BigInt(firstPieceId),
360+
clientDataSetId,
361+
firstAdded: firstPieceId,
362362
pieceData: formattedPieceData,
363363
pieceMetadata: pieceMetadata,
364364
})
@@ -392,13 +392,7 @@ export class PDPAuthHelper {
392392
* )
393393
* ```
394394
*/
395-
async signSchedulePieceRemovals(
396-
clientDataSetId: number | bigint,
397-
pieceIds: Array<number | bigint>
398-
): Promise<AuthSignature> {
399-
// Convert pieceIds to BigInt array for proper encoding
400-
const pieceIdsBigInt = pieceIds.map((id) => BigInt(id))
401-
395+
async signSchedulePieceRemovals(clientDataSetId: bigint, pieceIds: Array<bigint>): Promise<AuthSignature> {
402396
let signature: string
403397

404398
// Check if we should use MetaMask-friendly signing
@@ -408,16 +402,13 @@ export class PDPAuthHelper {
408402
// Use MetaMask-friendly signing for better UX
409403
const value = {
410404
clientDataSetId: clientDataSetId.toString(), // Keep as string for MetaMask display
411-
pieceIds: pieceIdsBigInt.map((id) => id.toString()), // Convert to string array for display
405+
pieceIds: pieceIds.map((id) => id.toString()), // Convert to string array for display
412406
}
413407

414408
signature = await this.signWithMetaMask({ SchedulePieceRemovals: EIP712_TYPES.SchedulePieceRemovals }, value)
415409
} else {
416410
// Use standard ethers.js signing with BigInt values
417-
const value = {
418-
clientDataSetId: BigInt(clientDataSetId),
419-
pieceIds: pieceIdsBigInt,
420-
}
411+
const value = { clientDataSetId, pieceIds }
421412

422413
// Use underlying signer for typed data signing (handles NonceManager)
423414
const actualSigner = this.getUnderlyingSigner()
@@ -434,10 +425,7 @@ export class PDPAuthHelper {
434425
const signedData = ethers.TypedDataEncoder.hash(
435426
this.domain,
436427
{ SchedulePieceRemovals: EIP712_TYPES.SchedulePieceRemovals },
437-
{
438-
clientDataSetId: BigInt(clientDataSetId),
439-
pieceIds: pieceIdsBigInt,
440-
}
428+
{ clientDataSetId, pieceIds }
441429
)
442430

443431
return {
@@ -467,7 +455,7 @@ export class PDPAuthHelper {
467455
* )
468456
* ```
469457
*/
470-
async signDeleteDataSet(clientDataSetId: number | bigint): Promise<AuthSignature> {
458+
async signDeleteDataSet(clientDataSetId: bigint): Promise<AuthSignature> {
471459
let signature: string
472460

473461
// Check if we should use MetaMask-friendly signing
@@ -482,9 +470,7 @@ export class PDPAuthHelper {
482470
signature = await this.signWithMetaMask({ DeleteDataSet: EIP712_TYPES.DeleteDataSet }, value)
483471
} else {
484472
// Use standard ethers.js signing
485-
const value = {
486-
clientDataSetId: BigInt(clientDataSetId),
487-
}
473+
const value = { clientDataSetId }
488474

489475
// Use underlying signer for typed data signing (handles NonceManager)
490476
const actualSigner = this.getUnderlyingSigner()
@@ -497,9 +483,7 @@ export class PDPAuthHelper {
497483
const signedData = ethers.TypedDataEncoder.hash(
498484
this.domain,
499485
{ DeleteDataSet: EIP712_TYPES.DeleteDataSet },
500-
{
501-
clientDataSetId: BigInt(clientDataSetId),
502-
}
486+
{ clientDataSetId }
503487
)
504488

505489
return {

packages/synapse-sdk/src/pdp/server.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export class PDPServer {
182182
* @returns Promise that resolves with transaction hash and status URL
183183
*/
184184
async createDataSet(
185-
clientDataSetId: number | bigint,
185+
clientDataSetId: bigint,
186186
payee: string,
187187
payer: string,
188188
metadata: MetadataEntry[],
@@ -264,7 +264,7 @@ export class PDPServer {
264264
*/
265265
async addPieces(
266266
dataSetId: number,
267-
clientDataSetId: number,
267+
clientDataSetId: bigint,
268268
nextPieceId: number,
269269
pieceDataArray: PieceCID[] | string[],
270270
metadata?: MetadataEntry[][]
@@ -305,7 +305,7 @@ export class PDPServer {
305305
// Generate the EIP-712 signature for adding pieces
306306
const authData = await this.getAuthHelper().signAddPieces(
307307
clientDataSetId,
308-
nextPieceId,
308+
BigInt(nextPieceId),
309309
pieceDataArray, // Pass PieceData[] directly to auth helper
310310
finalMetadata
311311
)
@@ -644,8 +644,8 @@ export class PDPServer {
644644
* @param pieceID - The ID of the piece to delete
645645
* @returns Promise for transaction hash of the delete operation
646646
*/
647-
async deletePiece(dataSetId: number, clientDataSetId: number, pieceID: number): Promise<string> {
648-
const authData = await this.getAuthHelper().signSchedulePieceRemovals(clientDataSetId, [pieceID])
647+
async deletePiece(dataSetId: number, clientDataSetId: bigint, pieceID: number): Promise<string> {
648+
const authData = await this.getAuthHelper().signSchedulePieceRemovals(clientDataSetId, [BigInt(pieceID)])
649649
const payload = {
650650
extraData: `0x${authData.signature}`,
651651
}
@@ -672,7 +672,7 @@ export class PDPServer {
672672
*/
673673
private _encodeDataSetCreateData(data: {
674674
payer: string
675-
clientDataSetId: number | bigint
675+
clientDataSetId: bigint
676676
metadata: MetadataEntry[]
677677
signature: string
678678
}): string {

packages/synapse-sdk/src/test/metadata.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Metadata Support', () => {
6464
)
6565

6666
const result = await pdpServer.createDataSet(
67-
1,
67+
1n,
6868
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8', // payee
6969
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8', // payer
7070
dataSetMetadata,
@@ -103,7 +103,7 @@ describe('Metadata Support', () => {
103103
)
104104

105105
// Test with matching metadata
106-
const result = await pdpServer.addPieces(dataSetId, 1, 1, pieces, metadata)
106+
const result = await pdpServer.addPieces(dataSetId, 1n, 1, pieces, metadata)
107107
assert.equal(result.txHash, mockTxHash)
108108
assert.exists(capturedPieceMetadata)
109109
assert.isNotNull(capturedPieceMetadata)
@@ -117,15 +117,15 @@ describe('Metadata Support', () => {
117117
]
118118

119119
try {
120-
await pdpServer.addPieces(dataSetId, 1, 1, pieces, mismatchedMetadata)
120+
await pdpServer.addPieces(dataSetId, 1n, 1, pieces, mismatchedMetadata)
121121
assert.fail('Should have thrown an error')
122122
} catch (error: any) {
123123
assert.match(error.message, /Metadata length \(2\) must match pieces length \(1\)/)
124124
}
125125

126126
// Test without metadata (should create empty arrays)
127127
capturedPieceMetadata = null
128-
const resultNoMetadata = await pdpServer.addPieces(dataSetId, 1, 1, pieces)
128+
const resultNoMetadata = await pdpServer.addPieces(dataSetId, 1n, 1, pieces)
129129
assert.equal(resultNoMetadata.txHash, mockTxHash)
130130
assert.exists(capturedPieceMetadata)
131131
assert.isNotNull(capturedPieceMetadata)
@@ -156,7 +156,7 @@ describe('Metadata Support', () => {
156156
]
157157

158158
await pdpServer.createDataSet(
159-
1,
159+
1n,
160160
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8', // payee
161161
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8', // payer
162162
metadataWithCDN,
@@ -171,7 +171,7 @@ describe('Metadata Support', () => {
171171
const metadataWithoutCDN: MetadataEntry[] = [{ key: 'project', value: 'test' }]
172172

173173
await pdpServer.createDataSet(
174-
1,
174+
1n,
175175
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8', // payee
176176
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8', // payer
177177
metadataWithoutCDN,

packages/synapse-sdk/src/test/pdp-auth.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ const FIXTURES = {
3636
signature:
3737
'0xc77965e2b6efd594629c44eb61127bc3133b65d08c25f8aa33e3021e7f46435845ab67ffbac96afc4b4671ecbd32d4869ca7fe1c0eaa5affa942d0abbfd98d601b',
3838
digest: '0xd89be6a725302e66575d7a9c730191a84e2a624d0f0f3976194d0bd6f2927640',
39-
clientDataSetId: 12345,
39+
clientDataSetId: 12345n,
4040
payee: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8',
4141
metadata: [{ key: 'title', value: 'TestDataSet' }],
4242
},
4343
addPieces: {
4444
signature:
4545
'0x215d2d6ea06c7daad46e3e636b305885c7d09aa34420e8dbace032af03cae06224cf678da808c7f1026b08ccf51f3d5d53351b935f5eee9750b80e78caffaaa91c',
4646
digest: '0xa690b5f3c6400833822aa3fd63ad1f0e4c1f70e5cc132cfd898c2993169d23bf',
47-
clientDataSetId: 12345,
48-
firstAdded: 1,
47+
clientDataSetId: 12345n,
48+
firstAdded: 1n,
4949
pieceCidBytes: [
5050
'0x01559120220500de6815dcb348843215a94de532954b60be550a4bec6e74555665e9a5ec4e0f3c',
5151
'0x01559120227e03642a607ef886b004bf2c1978463ae1d4693ac0f410eb2d1b7a47fe205e5e750f',
@@ -56,14 +56,14 @@ const FIXTURES = {
5656
signature:
5757
'0xcb8e645f2894fde89de54d4a54eb1e0d9871901c6fa1c2ee8a0390dc3a29e6cb2244d0561e3eca6452fa59efaab3d4b18a0b5b59ab52e233b3469422556ae9c61c',
5858
digest: '0xef55929f8dd724ef4b43c5759db26878608f7e1277d168e3e621d3cd4ba682dd',
59-
clientDataSetId: 12345,
60-
pieceIds: [1, 3, 5],
59+
clientDataSetId: 12345n,
60+
pieceIds: [1n, 3n, 5n],
6161
},
6262
deleteDataSet: {
6363
signature:
6464
'0x94e366bd2f9bfc933a87575126715bccf128b77d9c6937e194023e13b54272eb7a74b7e6e26acf4341d9c56e141ff7ba154c37ea03e9c35b126fff1efe1a0c831c',
6565
digest: '0x79df79ba922d913eccb0f9a91564ba3a1a81a0ea81d99a7cecf23cc3f425cafb',
66-
clientDataSetId: 12345,
66+
clientDataSetId: 12345n,
6767
},
6868
},
6969
}

packages/synapse-sdk/src/test/pdp-server.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('PDPServer', () => {
8383
)
8484

8585
const result = await pdpServer.createDataSet(
86-
0, // clientDataSetId
86+
0n, // clientDataSetId
8787
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8', // payee
8888
await signer.getAddress(), // payer
8989
[], // metadata (empty for no CDN)
@@ -193,7 +193,7 @@ describe('PDPServer', () => {
193193
it('should validate input parameters', async () => {
194194
// Test empty piece entries
195195
try {
196-
await pdpServer.addPieces(1, 0, 0, [])
196+
await pdpServer.addPieces(1, 0n, 0, [])
197197
assert.fail('Should have thrown error for empty piece entries')
198198
} catch (error) {
199199
assert.include((error as Error).message, 'At least one piece must be provided')
@@ -203,7 +203,7 @@ describe('PDPServer', () => {
203203
const invalidPieceCid = 'invalid-piece-link-string'
204204

205205
try {
206-
await pdpServer.addPieces(1, 0, 0, [invalidPieceCid])
206+
await pdpServer.addPieces(1, 0n, 0, [invalidPieceCid])
207207
assert.fail('Should have thrown error for invalid PieceCID')
208208
} catch (error) {
209209
assert.include((error as Error).message, 'Invalid PieceCID')
@@ -238,7 +238,7 @@ describe('PDPServer', () => {
238238
)
239239

240240
// Should not throw
241-
const result = await pdpServer.addPieces(1, 0, 0, validPieceCid)
241+
const result = await pdpServer.addPieces(1, 0n, 0, validPieceCid)
242242
assert.isDefined(result)
243243
assert.isDefined(result.message)
244244
})
@@ -256,7 +256,7 @@ describe('PDPServer', () => {
256256
)
257257

258258
try {
259-
await pdpServer.addPieces(1, 0, 0, validPieceCid)
259+
await pdpServer.addPieces(1, 0n, 0, validPieceCid)
260260
assert.fail('Should have thrown error for server error')
261261
} catch (error) {
262262
assert.include(
@@ -302,7 +302,7 @@ describe('PDPServer', () => {
302302
}
303303
)
304304
)
305-
const result = await pdpServer.addPieces(1, 0, 0, multiplePieceCid)
305+
const result = await pdpServer.addPieces(1, 0n, 0, multiplePieceCid)
306306
assert.isDefined(result)
307307
assert.isDefined(result.message)
308308
})
@@ -322,7 +322,7 @@ describe('PDPServer', () => {
322322
})
323323
)
324324

325-
const result = await pdpServer.addPieces(1, 0, 0, validPieceCid)
325+
const result = await pdpServer.addPieces(1, 0n, 0, validPieceCid)
326326
assert.isDefined(result)
327327
assert.isDefined(result.message)
328328
assert.strictEqual(result.txHash, mockTxHash)
@@ -346,7 +346,7 @@ describe('PDPServer', () => {
346346
})
347347
)
348348

349-
const result = await pdpServer.addPieces(1, 0, 0, validPieceCid)
349+
const result = await pdpServer.addPieces(1, 0n, 0, validPieceCid)
350350
assert.isDefined(result)
351351
assert.strictEqual(result.txHash, mockTxHashWith0x) // Should have 0x prefix added
352352
})
@@ -365,7 +365,7 @@ describe('PDPServer', () => {
365365
})
366366
)
367367

368-
const result = await pdpServer.addPieces(1, 0, 0, validPieceCid)
368+
const result = await pdpServer.addPieces(1, 0n, 0, validPieceCid)
369369
assert.isDefined(result)
370370
assert.isDefined(result.message)
371371
assert.isUndefined(result.txHash) // No txHash for malformed Location
@@ -389,7 +389,7 @@ describe('PDPServer', () => {
389389
})
390390
})
391391
)
392-
const result = await pdpServer.deletePiece(1, 0, 2)
392+
const result = await pdpServer.deletePiece(1, 0n, 2)
393393
assert.strictEqual(result, mockTxHash)
394394
})
395395

@@ -402,7 +402,7 @@ describe('PDPServer', () => {
402402
})
403403
)
404404
try {
405-
await pdpServer.deletePiece(1, 0, 2)
405+
await pdpServer.deletePiece(1, 0n, 2)
406406
assert.fail('Should have thrown error for server error')
407407
} catch (error: any) {
408408
assert.include(error.message, 'Failed to delete piece')

0 commit comments

Comments
 (0)