Skip to content

Commit

Permalink
Feature/delegate wallet passthrough (#334)
Browse files Browse the repository at this point in the history
* Wallet Connect events passthough to SFT

* revert package.json

* revert package-lock
  • Loading branch information
acedward authored Mar 31, 2024
1 parent 940a857 commit 0e713e5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ bin
# logs
*.log

.idea/
.idea/
87 changes: 43 additions & 44 deletions packages/engine/paima-sm/src/delegate-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,60 +267,59 @@ export class DelegateWallet {
/*
* Main entry point. Verify if command is valid and execute it.
*
* If returns TRUE this is was an intend as internal command,
* and the paima-concise command should not passed into the game STF.
* If returns TRUE if execution was valid and should be passed
* into the SFT to apply side-effects.
*/
public async process(
realAddress: string,
userAddress: string,
command: string
): Promise<boolean> {
try {
if (!command.startsWith(DelegateWallet.INTERNAL_COMMAND_PREFIX)) return false;
if (!command.startsWith(DelegateWallet.INTERNAL_COMMAND_PREFIX)) {
return true;
}
const parsed: ParsedDelegateWalletCommand = DelegateWallet.parser.start(
command
) as ParsedDelegateWalletCommand;
switch (parsed.command) {
case 'delegate':
{
const from = parsed.args.from || realAddress;
const to = parsed.args.to || realAddress;
this.validateSender(to, from, realAddress);

const { from_signature, to_signature } = parsed.args;
await Promise.all([
this.verifySignature(from, this.generateMessage(to), from_signature),
this.verifySignature(to, this.generateMessage(from), to_signature),
]);
await this.cmdDelegate(from.toLocaleLowerCase(), to.toLocaleLowerCase());
doLog(`Delegate Wallet ${from.substring(0, 8)}... -> ${to.substring(0, 8)}...`);
}
break;
case 'migrate':
{
const from = parsed.args.from || realAddress;
const to = parsed.args.to || realAddress;
this.validateSender(to, from, realAddress);

const { from_signature, to_signature } = parsed.args;
await Promise.all([
this.verifySignature(from, this.generateMessage(to), from_signature),
this.verifySignature(to, this.generateMessage(from), to_signature),
]);
await this.cmdMigrate(from.toLocaleLowerCase(), to.toLocaleLowerCase());

doLog(`Migrate Wallet ${from.substring(0, 8)}... -> ${to.substring(0, 8)}...`);
}
break;
case 'cancelDelegations':
{
const to = realAddress;
const { to_signature } = parsed.args;
await this.verifySignature(to, this.generateMessage(), to_signature);
await this.cmdCancelDelegations(to.toLocaleLowerCase());
doLog(`Cancel Delegate 'to' ${to.substring(0, 8)}...`);
}
break;
case 'delegate': {
const from = parsed.args.from || realAddress;
const to = parsed.args.to || realAddress;
this.validateSender(to, from, realAddress);

const { from_signature, to_signature } = parsed.args;
await Promise.all([
this.verifySignature(from, this.generateMessage(to), from_signature),
this.verifySignature(to, this.generateMessage(from), to_signature),
]);
await this.cmdDelegate(from.toLocaleLowerCase(), to.toLocaleLowerCase());
doLog(`Delegate Wallet ${from.substring(0, 8)}... -> ${to.substring(0, 8)}...`);
return true;
}
case 'migrate': {
const from = parsed.args.from || realAddress;
const to = parsed.args.to || realAddress;
this.validateSender(to, from, realAddress);

const { from_signature, to_signature } = parsed.args;
await Promise.all([
this.verifySignature(from, this.generateMessage(to), from_signature),
this.verifySignature(to, this.generateMessage(from), to_signature),
]);
await this.cmdMigrate(from.toLocaleLowerCase(), to.toLocaleLowerCase());

doLog(`Migrate Wallet ${from.substring(0, 8)}... -> ${to.substring(0, 8)}...`);
return true;
}
case 'cancelDelegations': {
const to = realAddress;
const { to_signature } = parsed.args;
await this.verifySignature(to, this.generateMessage(), to_signature);
await this.cmdCancelDelegations(to.toLocaleLowerCase());
doLog(`Cancel Delegate 'to' ${to.substring(0, 8)}...`);
return true;
}
default:
throw new Error(
`Delegate Wallet Internal Error : ${JSON.stringify({ parsed, command })}`
Expand All @@ -329,6 +328,6 @@ export class DelegateWallet {
} catch (e) {
doLog(`Error parsing command: ${command} ${String(e)}`);
}
return true;
return false;
}
}
60 changes: 30 additions & 30 deletions packages/engine/paima-sm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,50 +417,50 @@ async function processUserInputs(
// cancelDelegate = &wc|to_signature
const delegateWallet = new DelegateWallet(DBConn);
if (inputData.inputData.startsWith(DelegateWallet.INTERNAL_COMMAND_PREFIX)) {
await delegateWallet.process(
const status = await delegateWallet.process(
inputData.realAddress,
inputData.userAddress,
inputData.inputData
);
} else {
if (!status) continue;
} else if (inputData.userId === NO_USER_ID) {
// If wallet does not exist in address table: create it.
if (inputData.userId === NO_USER_ID) {
const newAddress = await delegateWallet.createAddress(inputData.userAddress);
inputData.userId = newAddress.id;
const newAddress = await delegateWallet.createAddress(inputData.userAddress);
inputData.userId = newAddress.id;
}

// Trigger STF
const sqlQueries = await gameStateTransition(
inputData,
latestChainData.blockNumber,
randomnessGenerator,
DBConn
);

try {
for (const [query, params] of sqlQueries) {
await query.run(params, DBConn);
}
// Trigger STF
const sqlQueries = await gameStateTransition(
inputData,
latestChainData.blockNumber,
randomnessGenerator,
await insertNonce.run(
{
nonce: inputData.inputNonce,
block_height: latestChainData.blockNumber,
},
DBConn
);

try {
for (const [query, params] of sqlQueries) {
await query.run(params, DBConn);
}
await insertNonce.run(
if (ENV.STORE_HISTORICAL_GAME_INPUTS) {
await storeGameInput.run(
{
nonce: inputData.inputNonce,
block_height: latestChainData.blockNumber,
input_data: inputData.inputData,
user_address: inputData.userAddress,
},
DBConn
);
if (ENV.STORE_HISTORICAL_GAME_INPUTS) {
await storeGameInput.run(
{
block_height: latestChainData.blockNumber,
input_data: inputData.inputData,
user_address: inputData.userAddress,
},
DBConn
);
}
} catch (err) {
doLog(`[paima-sm] Database error on gameStateTransition: ${err}`);
throw err;
}
} catch (err) {
doLog(`[paima-sm] Database error on gameStateTransition: ${err}`);
throw err;
}
}
return latestChainData.submittedData.length;
Expand Down
2 changes: 1 addition & 1 deletion packages/node-sdk/paima-db/src/delegate-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let useAddressCache = false;
export const addressCache = new Map<string, WalletDelegate>();

// Get Main Wallet and ID for address.
// If wallet does not exist, It will NOT be created in address, table.
// If wallet does not exist, It will NOT be created in address table.
export async function getMainAddress(
_address: string,
DBConn: PoolClient
Expand Down

0 comments on commit 0e713e5

Please sign in to comment.