Skip to content

Commit

Permalink
add pub key for game joining so game master always can use it for pkdf
Browse files Browse the repository at this point in the history
  • Loading branch information
peersky committed Feb 12, 2025
1 parent 2285b43 commit bba9f8c
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 113 deletions.
49 changes: 34 additions & 15 deletions scripts/EnvironmentSimulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ interface ProposalParams {
commitment: BigNumberish;
proposer: string;
gmSignature: BytesLike;
voterSignature: BytesLike;
proposerSignature: BytesLike;
}

export interface ProposalSubmission {
Expand Down Expand Up @@ -811,7 +811,7 @@ class EnvironmentSimulator {
},
);

const voterSignature = await this.signProposal(
const proposerSignature = await this.signProposal(
verifier.address,
proposer.wallet.address,
gameId,
Expand All @@ -831,7 +831,7 @@ class EnvironmentSimulator {
commitment: poseidonCommitment,
proposer: proposer.wallet.address,
gmSignature,
voterSignature,
proposerSignature,
};

log(`Generated proposal secrets with commitment ${poseidonCommitment}`);
Expand Down Expand Up @@ -907,11 +907,11 @@ class EnvironmentSimulator {

joinTypes = {
AttestJoiningGame: [
{ type: 'address', name: 'instance' },
{ type: 'address', name: 'participant' },
{ type: 'uint256', name: 'gameId' },
{ type: 'bytes32', name: 'gmCommitment' },
{ type: 'uint256', name: 'deadline' },
{ type: 'bytes32', name: 'participantPubKeyHash' },
],
};
/**
Expand All @@ -923,7 +923,15 @@ class EnvironmentSimulator {
* @param signer - The signer's identity
* @returns Object containing signature and hidden salt
*/
signJoiningGame = async (gameId: BigNumberish, participant: string, signer: Wallet) => {
signJoiningGame = async ({
gameId,
participant,
signer,
}: {
gameId: BigNumberish;
participant: Wallet | SignerWithAddress;
signer: Wallet;
}) => {
const { ethers } = this.hre;
const eip712 = await this.rankifyInstance.inspectEIP712Hashes();
let { chainId } = await ethers.provider.getNetwork();
Expand All @@ -935,14 +943,19 @@ class EnvironmentSimulator {
};
const gmCommitment = ethers.utils.formatBytes32String('0x123131231311'); // Pad to 32 bytes
const deadline = BigInt(Math.floor(Date.now() / 1000) + 100000);
const participantPubKey = utils.recoverPublicKey(
utils.hashMessage(participant.address),
await participant.signMessage(participant.address),
);

const signature = await signer._signTypedData(domain, this.joinTypes, {
instance: this.rankifyInstance.address,
participant,
participant: participant.address,
gameId,
gmCommitment, // Hash the padded value
deadline,
participantPubKeyHash: utils.solidityKeccak256(['string'], [participantPubKey]),
});
return { signature, gmCommitment, deadline };
return { signature, gmCommitment, deadline, participant, participantPubKey: participantPubKey };
};

public async createGame(
Expand Down Expand Up @@ -1121,7 +1134,7 @@ class EnvironmentSimulator {
commitment: 0,
proposer: constants.AddressZero,
gmSignature: '0x',
voterSignature: '0x',
proposerSignature: '0x',
},
proposal: '',
proposalValue: 0n,
Expand Down Expand Up @@ -1152,7 +1165,7 @@ class EnvironmentSimulator {
proposals[i].params.proposer = alreadyExistingProposal.args.proposer;
proposals[i].params.gameId = alreadyExistingProposal.args.gameId;
proposals[i].params.gmSignature = alreadyExistingProposal.args.gmSignature;
proposals[i].params.voterSignature = alreadyExistingProposal.args.voterSignature;
proposals[i].params.proposerSignature = alreadyExistingProposal.args.proposerSignature;

try {
const sharedKey = sharedSigner({
Expand Down Expand Up @@ -1393,17 +1406,23 @@ class EnvironmentSimulator {
}
} else {
if (!this.rankToken.address) throw new Error('Rank token undefined or unemployed');
const pubKey = utils.recoverPublicKey(
utils.hashMessage(players[i].wallet.address),
await players[i].wallet.signMessage(players[i].wallet.address),
);
await this.rankToken
.connect(players[i].wallet)
.setApprovalForAll(this.rankifyInstance.address, true)
.then(tx => tx.wait(1));
const { signature, gmCommitment, deadline } = await this.signJoiningGame(
const { signature, gmCommitment, deadline } = await this.signJoiningGame({
gameId,
players[i].wallet.address,
gameMaster,
);
participant: players[i].wallet,
signer: gameMaster,
});
promises.push(
await this.rankifyInstance.connect(players[i].wallet).joinGame(gameId, signature, gmCommitment, deadline),
await this.rankifyInstance
.connect(players[i].wallet)
.joinGame(gameId, signature, gmCommitment, deadline, pubKey),
);
}
}
Expand Down
33 changes: 17 additions & 16 deletions src/facets/RankifyInstanceGameMastersFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,24 @@ contract RankifyInstanceGameMastersFacet is DiamondReentrancyGuard, EIP712 {
IErrors.invalidECDSARecoverSigner(proposalDigest, "Invalid GM signature")
);
}

bytes32 voterDigest = _hashTypedDataV4(
keccak256(
abi.encode(
keccak256(
"AuthorizeProposalSubmission(uint256 gameId,string encryptedProposal,uint256 commitment)"
),
params.gameId,
keccak256(bytes(params.encryptedProposal)),
params.commitment
if (msg.sender != params.proposer) {
bytes32 voterDigest = _hashTypedDataV4(
keccak256(
abi.encode(
keccak256(
"AuthorizeProposalSubmission(uint256 gameId,string encryptedProposal,uint256 commitment)"
),
params.gameId,
keccak256(bytes(params.encryptedProposal)),
params.commitment
)
)
)
);
require(
SignatureChecker.isValidSignatureNow(params.proposer, voterDigest, params.proposerSignature),
"invalid proposer signature"
);
);
require(
SignatureChecker.isValidSignatureNow(params.proposer, voterDigest, params.proposerSignature),
"invalid proposer signature"
);
}
LibRankify.GameState storage game = params.gameId.getGameState();
require(LibTBG.getPlayersGame(params.proposer) == params.gameId, "not a player");
require(bytes(params.encryptedProposal).length != 0, "Cannot propose empty");
Expand Down
11 changes: 6 additions & 5 deletions src/facets/RankifyInstanceMainFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,26 +165,27 @@ contract RankifyInstanceMainFacet is
uint256 gameId,
bytes memory gameMasterSignature,
bytes32 gmCommitment,
uint256 deadline
uint256 deadline,
string memory voterPubKey
) public payable nonReentrant {
require(block.timestamp < deadline, "Signature deadline has passed");
bytes32 digest = _hashTypedDataV4(
keccak256(
abi.encode(
keccak256(
"AttestJoiningGame(address instance,address participant,uint256 gameId,bytes32 gmCommitment,uint256 deadline)"
"AttestJoiningGame(address participant,uint256 gameId,bytes32 gmCommitment,uint256 deadline,bytes32 participantPubKeyHash)"
),
address(this),
msg.sender,
gameId,
gmCommitment,
deadline
deadline,
keccak256(abi.encodePacked(voterPubKey))
)
)
);
gameId.joinGame(msg.sender, gameMasterSignature, digest);
LibCoinVending.fund(bytes32(gameId));
emit PlayerJoined(gameId, msg.sender, gmCommitment);
emit PlayerJoined(gameId, msg.sender, gmCommitment, voterPubKey);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IRankifyInstance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IRankifyInstance {
error RankNotSpecified();

event RegistrationOpen(uint256 indexed gameId);
event PlayerJoined(uint256 indexed gameId, address indexed participant, bytes32 gmCommitment);
event PlayerJoined(uint256 indexed gameId, address indexed participant, bytes32 gmCommitment, string voterPubKey);
event GameStarted(uint256 indexed gameId);
event gameCreated(uint256 gameId, address indexed gm, address indexed creator, uint256 indexed rank);
event GameClosed(uint256 indexed gameId);
Expand Down
1 change: 1 addition & 0 deletions src/libraries/LibRankify.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ library LibRankify {
uint128 minGameTime;
uint128 timePerTurn;
uint128 timeToJoin;
// ToDo: It must list gameKey for Game master and game master signature, committing to serve the game
}

function getGamePrice(uint128 minGameTime, CommonParams memory commonParams) internal pure returns (uint256) {
Expand Down
Loading

0 comments on commit bba9f8c

Please sign in to comment.