Skip to content

Commit 0838420

Browse files
authored
Merge pull request #54 from trader-xyz/feat/better-nonces
Use BigInt to generate 128bit nonces
2 parents df267e2 + 67927f5 commit 0838420

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/sdk/v4/pure.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Signer, TypedDataSigner } from '@ethersproject/abstract-signer';
22
import { BigNumber } from '@ethersproject/bignumber';
33
import { hexDataLength, hexDataSlice } from '@ethersproject/bytes';
4-
import { BaseProvider } from '@ethersproject/providers';
4+
import type { BaseProvider } from '@ethersproject/providers';
5+
import type { ContractTransaction } from '@ethersproject/contracts';
56
import getUnixTime from 'date-fns/getUnixTime';
6-
import { ContractTransaction } from 'ethers';
77
import { v4 } from 'uuid';
8+
import warning from 'tiny-warning';
89
import {
910
ERC1155__factory,
1011
ERC20__factory,
@@ -39,7 +40,6 @@ import {
3940
PROPERTY_ABI,
4041
ETH_ADDRESS_AS_ERC20,
4142
} from './constants';
42-
import warning from 'tiny-warning';
4343

4444
export const signOrderWithEoaWallet = async (
4545
order: NftOrderV4,
@@ -314,7 +314,7 @@ export const generateErc721Order = (
314314
expiry: orderData.expiry
315315
? getUnixTime(orderData.expiry).toString()
316316
: INFINITE_EXPIRATION_TIMESTAMP_SEC.toString(),
317-
nonce: orderData.nonce?.toString() ?? generateRandomNonce(),
317+
nonce: orderData.nonce?.toString() ?? generateRandomV4OrderNonce(),
318318
taker: orderData.taker?.toLowerCase() ?? NULL_ADDRESS,
319319
};
320320

@@ -351,15 +351,21 @@ export const generateErc1155Order = (
351351
expiry: orderData.expiry
352352
? getUnixTime(orderData.expiry).toString()
353353
: INFINITE_EXPIRATION_TIMESTAMP_SEC.toString(),
354-
nonce: orderData.nonce?.toString() ?? generateRandomNonce(),
354+
nonce: orderData.nonce?.toString() ?? generateRandomV4OrderNonce(),
355355
taker: orderData.taker?.toLowerCase() ?? NULL_ADDRESS,
356356
};
357357

358358
return erc1155Order;
359359
};
360360

361-
const generateRandomNonce = () => {
362-
return `0x${v4().toString().split('-').join('')}`;
361+
/**
362+
* @returns 128bit nonce as string (0x orders can handle up to 256 bit nonce)
363+
*/
364+
export const generateRandomV4OrderNonce = (): string => {
365+
const hex = '0x' + v4().replace(/-/g, '');
366+
const value = BigInt(hex);
367+
const decimal = value.toString(); // don't convert this to a number, will lose precision
368+
return decimal;
363369
};
364370

365371
export const serializeNftOrder = (

test/v4/utils.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { BigNumber, ethers } from 'ethers';
22
import { NftSwapV4 } from '../../src/sdk/v4/NftSwapV4';
33

4-
import {
5-
SignedERC721OrderStruct,
6-
SwappableAssetV4,
7-
} from '../../src/sdk/v4/types';
4+
import { SwappableAssetV4 } from '../../src/sdk/v4/types';
85

96
jest.setTimeout(120 * 1000);
107

0 commit comments

Comments
 (0)