Skip to content

Commit

Permalink
Fix card decrypt shares
Browse files Browse the repository at this point in the history
  • Loading branch information
arssly committed Oct 12, 2024
1 parent eddbc67 commit a646812
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
7 changes: 0 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/ts-sdk/src/Jeton/Jeton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class Jeton extends EventEmitter<GameEventMap> {
};

private playerShuffledDeck = async (data: OnChainShuffledDeckData) => {
console.log("player shuffled deck");
const onChainTableObject = await this.pendingMemo.memoize(this.queryGameState);
const shufflingPlayer = getShufflingPlayer(onChainTableObject);
if (!shufflingPlayer) {
Expand All @@ -161,6 +162,7 @@ export class Jeton extends EventEmitter<GameEventMap> {
};

private async createAndSharePrivateKeyShares(state: OnChainTableObject) {
console.log("create and share private key shares");
if (!this.zkDeck) throw new Error("zkDeck should be present");
// TODO: do not call if your player is not active player

Expand All @@ -177,6 +179,7 @@ export class Jeton extends EventEmitter<GameEventMap> {
const sharesToSend = filteredPAS.map((pas) => pas.decryptionCardShare);
this.myPrivateCardsShares = proofsAndShares
.filter((pas) => myPrivateCardsIndexes.includes(pas.cardIndex))
.sort((a, b) => a.cardIndex - b.cardIndex)
.map((pas) => pas.decryptionCardShare) as [DecryptionCardShare, DecryptionCardShare];
this.onChainDataSource.privateCardsDecryptionShares(
this.tableInfo.id,
Expand Down
11 changes: 7 additions & 4 deletions packages/ts-sdk/src/Jeton/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ export function getCardShares(tableObject: OnChainTableObject, indexes: number):
export function getCardShares(tableObject: OnChainTableObject, indexes: number[]): Uint8Array[];
export function getCardShares(tableObject: OnChainTableObject, indexes: number | number[]) {
if (tableObject.state.__variant__ !== "Playing") throw new Error("must be playing");
const arrayedShares = new Array(52);
const mapShares = tableObject.state.decryption_card_shares.data;
for (const mapShare of mapShares) {
arrayedShares[mapShare.key] = mapShare.value;
}

if (!Array.isArray(indexes)) return tableObject.state.decryption_card_shares[indexes];
if (!Array.isArray(indexes)) return hexStringToUint8Array(arrayedShares[indexes]!);

return indexes.map(
(cardIndex) => (tableObject.state as PlayingState).decryption_card_shares[cardIndex]!,
);
return indexes.map((cardIndex) => hexStringToUint8Array(arrayedShares[cardIndex]));
}

export function getCardIndexes(tableObject: OnChainTableObject, round: PublicCardRounds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export type PlayingState = {
__variant__: "Playing";
timeout: string;
deck: string;
// TODO: what does it represent?
decryption_card_shares: Record<number, Uint8Array>;
decryption_card_shares: { data: { key: number; value: string }[] };
phase: OnChainPhase;
};

Expand Down
3 changes: 2 additions & 1 deletion packages/ts-sdk/src/contracts/contractInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
contractCreateTableFunctionName,
contractDecryptShareFunctionName,
contractShuffleEncryptDeckFunctionName,
contractShuffleEventType,
contractTableCreatedEventType,
contractTableType,
} from "./contractData";
Expand Down Expand Up @@ -158,7 +159,7 @@ export async function queryEvents(tableId: string) {
const document = gql`
query MyQuery {
events(
where: {indexed_type: {_in: ["${contractTableCreatedEventType}", "${contractCheckedInEventType}", "${contractCardDecryptionShareEventType}"]},
where: {indexed_type: {_in: ["${contractTableCreatedEventType}", "${contractCheckedInEventType}","${contractShuffleEventType}", "${contractCardDecryptionShareEventType}"]},
data: {_cast: {String: {_like: "%${tableId}%"}}}},
order_by: {transaction_block_height: desc}
) {
Expand Down

0 comments on commit a646812

Please sign in to comment.