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 3 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
24 changes: 24 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,24 @@
import calculateSaltFromAddress from '../calc-salt';

describe('calculate salt', () => {
it('calculates a random salt', () => {
const SEED_CONSTANT = 'test';
const address = '0xe2E9b9B5d0757c26aB477A754788B19b60f2ed83';

const results = [
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
calculateSaltFromAddress(SEED_CONSTANT, address),
];

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

export default function calculateSaltFromAddress(seedConstant: string, address: string) {
const hash = ethers.keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(['string'], [seedConstant + address]),
);
const addressBigInt = ethers.toBigInt('0x' + hash.slice(26));

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!

const randomBigInt = BigInt(ethers.hexlify(randomBytes));

return addressBigInt & randomBigInt & BigInt('0xFFFFFFFFFFFFFFFF');
}
10 changes: 3 additions & 7 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 Down Expand Up @@ -28,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 calculateSaltFromAddress from '../calc-salt';

type AccountId = string;

Expand Down Expand Up @@ -348,11 +349,6 @@ export default class DripListService {

// Create random salt from address
Copy link
Collaborator

@jtourkos jtourkos Dec 31, 2024

Choose a reason for hiding this comment

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

Are we sure this generates a random number?
I think for the same address it will produce the same output, no?

(Even the previous code, I believe wasn't truly random.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We're not sure and it looks like it doesn't based on the error I'm receiving. Will look into it!

private _calcSaltFromAddress = (address: string): bigint /* 64bit */ => {
const hash = ethers.keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(['string'], [this.SEED_CONSTANT + address]),
);
const randomBigInt = ethers.toBigInt('0x' + hash.slice(26));

return BigInt(randomBigInt.toString()) & BigInt('0xFFFFFFFFFFFFFFFF');
return calculateSaltFromAddress(this.SEED_CONSTANT, address);
};
}
Loading