Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate a random Drip List ID when creating a new Drip List #1406

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/lib/utils/__test__/calc-salt.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import calculateRandomSalt from '../calc-salt';

describe('calculate salt', () => {
it('calculates a random salt', () => {
const results = [
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
calculateRandomSalt(),
];

const set = new Set(results);
expect(set.size).toBe(results.length);
});
});
6 changes: 6 additions & 0 deletions src/lib/utils/calc-salt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ethers } from 'ethers';

export default function calculateRandomSalt() {
const randomBytes = ethers.randomBytes(32);
Copy link
Collaborator

@jtourkos jtourkos Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely introduces randomness now 👍
But, why not simplifying to something simple like:

export default function generateRandomNumber() {
  const randomBytes = ethers.randomBytes(32);
  return BigInt(ethers.hexlify(randomBytes)) & BigInt('0xFFFFFFFFFFFFFFFF');
}

This is purely random compared to what we had before where the final result is partially influenced by the (deterministic) seed and address.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason at all. Just not trying to shake the tree too much because I don't know what constraints the seed should have. Will update!

return BigInt(ethers.hexlify(randomBytes)) & BigInt('0xFFFFFFFFFFFFFFFF');
}
49 changes: 3 additions & 46 deletions src/lib/utils/driplist/DripListService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NftDriverMetadataManager from '../metadata/NftDriverMetadataManager';
import MetadataManagerBase from '../metadata/MetadataManagerBase';
import { ethers, MaxUint256, type Signer, toBigInt } from 'ethers';
import { MaxUint256, type Signer, toBigInt } from 'ethers';
import GitProjectService from '../project/GitProjectService';
import assert from '$lib/utils/assert';
import type { Address, IpfsHash } from '../common-types';
Expand All @@ -9,12 +9,6 @@ import { get } from 'svelte/store';
import Emoji from '$lib/components/emoji/emoji.svelte';
import type { nftDriverAccountMetadataParser } from '../metadata/schemas';
import type { LatestVersion } from '@efstajas/versioned-parser';
import { gql } from 'graphql-request';
import query from '$lib/graphql/dripsQL';
import type {
MintedNftAccountsCountQuery,
MintedNftAccountsCountQueryVariables,
} from './__generated__/gql.generated';
import type { Items, Weights } from '$lib/components/list-editor/types';
import { buildStreamCreateBatchTx } from '../streams/streams';
import {
Expand All @@ -34,6 +28,7 @@ import keyValueToMetatada from '../sdk/utils/key-value-to-metadata';
import { populateCallerWriteTx } from '../sdk/caller/caller';
import txToCallerCall from '../sdk/utils/tx-to-caller-call';
import network from '$lib/stores/wallet/network';
import calculateRandomSalt from '../calc-salt';

type AccountId = string;

Expand Down Expand Up @@ -113,23 +108,7 @@ export default class DripListService {
items,
);

const mintedNftAccountsCountQuery = gql`
query MintedNftAccountsCount($ownerAddress: String!, $chain: SupportedChain!) {
mintedTokensCountByOwnerAddress(ownerAddress: $ownerAddress, chain: $chain) {
total
}
}
`;

const mintedNftAccountsCountRes = await query<
MintedNftAccountsCountQuery,
MintedNftAccountsCountQueryVariables
>(mintedNftAccountsCountQuery, { ownerAddress: this._ownerAddress, chain: network.gqlName });

const salt = this._calcSaltFromAddress(
this._ownerAddress,
mintedNftAccountsCountRes.mintedTokensCountByOwnerAddress.total ?? 0,
);
const salt = calculateRandomSalt();

const listId = (
await executeNftDriverReadMethod({
Expand Down Expand Up @@ -367,26 +346,4 @@ export default class DripListService {

return ipfsHash;
}

// We use the count of *all* NFT sub-accounts to generate the salt for the Drip List ID.
// This is because we want to avoid making HTTP requests to the subgraph for each NFT sub-account to check if it's a Drip List.
private _calcSaltFromAddress = (address: string, listCount: number): bigint /* 64bit */ => {
const hash = ethers.keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(['string'], [this.SEED_CONSTANT + address]),
);
const randomBigInt = ethers.toBigInt('0x' + hash.slice(26));

let random64BitBigInt = BigInt(randomBigInt.toString()) & BigInt('0xFFFFFFFFFFFFFFFF');

const listCountBigInt = BigInt(listCount);
random64BitBigInt = random64BitBigInt ^ listCountBigInt;

return random64BitBigInt;
};

private _generateDripIdFromSalt = (salt: bigint): number /* 32bit */ => {
const random32BitNumber = Number(salt & BigInt(0xffffffff));

return random32BitNumber;
};
}
Loading