From 5c152c8c624227d86c75be90a7166e9b605774d8 Mon Sep 17 00:00:00 2001 From: Edward Alvarado Date: Thu, 25 Apr 2024 12:53:02 -0400 Subject: [PATCH] Updated "cancelDelegations = &wc|to?". Removed Delegation CONTRACT_ADDRESS in signature --- .../engine/paima-sm/src/delegate-wallet.ts | 48 ++++++++++++------- .../src/sql/wallet-delegation.queries.ts | 4 +- .../paima-db/src/sql/wallet-delegation.sql | 3 +- .../src/delegate-wallet/index.ts | 4 +- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/packages/engine/paima-sm/src/delegate-wallet.ts b/packages/engine/paima-sm/src/delegate-wallet.ts index ac64228bb..b8a3e79df 100644 --- a/packages/engine/paima-sm/src/delegate-wallet.ts +++ b/packages/engine/paima-sm/src/delegate-wallet.ts @@ -1,14 +1,13 @@ -import Web3 from 'web3'; import type { PoolClient } from 'pg'; import { PaimaParser } from '@paima/concise'; import { ENV, doLog } from '@paima/utils'; -import type { IVerify } from '@paima/crypto'; import { CryptoManager } from '@paima/crypto'; import type { IGetAddressFromAddressResult } from '@paima/db'; import { addressCache, deleteDelegationTo, + deleteDelegationsFrom, enableManualCache, getAddressFromAddress, getDelegation, @@ -32,7 +31,7 @@ type ParsedDelegateWalletCommand = command: 'migrate'; args: { from: string; to: string; from_signature: string; to_signature: string }; } - | { command: 'cancelDelegations'; args: { to_signature: string } }; + | { command: 'cancelDelegations'; args: { to: string } }; // Delegate Wallet manages cache cleanup. enableManualCache(); @@ -47,7 +46,7 @@ export class DelegateWallet { private static readonly delegationGrammar = ` delegate = &wd|from?|to?|from_signature|to_signature migrate = &wm|from?|to?|from_signature|to_signature - cancelDelegations = &wc|to_signature + cancelDelegations = &wc|to? `; private static readonly parserCommands = { @@ -64,7 +63,7 @@ export class DelegateWallet { to_signature: PaimaParser.NCharsParser(0, 1024), }, cancelDelegations: { - to_signature: PaimaParser.NCharsParser(0, 1024), + to: PaimaParser.OptionalParser('', PaimaParser.WalletAddress()), }, }; @@ -77,9 +76,7 @@ export class DelegateWallet { /* Generate Plaintext Message */ private generateMessage(internalMessage: string = ''): string { - return `${DelegateWallet.DELEGATE_WALLET_PREFIX}${ - DelegateWallet.SEP - }${internalMessage.toLocaleLowerCase()}${DelegateWallet.SEP}${ENV.CONTRACT_ADDRESS}`; + return `${DelegateWallet.DELEGATE_WALLET_PREFIX}${DelegateWallet.SEP}${internalMessage.toLocaleLowerCase()}`; } private validateSender(to: string, from: string, realAddress: string): void { @@ -255,11 +252,26 @@ export class DelegateWallet { } // Cancel Delegations. - // Delete all delegations from where TO=to - private async cmdCancelDelegations(to: string): Promise { - const [toAddress] = await getAddressFromAddress.run({ address: to }, this.DBConn); - if (!toAddress) throw new Error('Invalid Address'); - await deleteDelegationTo.run({ to_id: toAddress.id }, this.DBConn); + // if "to" is provided, delete delegations for "from" -> "to" + // if "to" is not provided, delete all delegations for "from" -> * + private async cmdCancelDelegations(from: string, to: string): Promise { + const [fromAddress] = await getAddressFromAddress.run({ address: from }, this.DBConn); + if (!fromAddress) throw new Error('Invalid Address'); + + if (to) { + const [toAddress] = await getAddressFromAddress.run({ address: to }, this.DBConn); + if (!toAddress) throw new Error('Invalid Address'); + + await deleteDelegationTo.run( + { + to_id: toAddress.id, + from_id: fromAddress.id, + }, + this.DBConn + ); + } else { + await deleteDelegationsFrom.run({ from_id: fromAddress.id }, this.DBConn); + } addressCache.clear(); } @@ -313,11 +325,11 @@ export class DelegateWallet { 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)}...`); + const { to } = parsed.args; + await this.cmdCancelDelegations(userAddress.toLocaleLowerCase(), to.toLocaleLowerCase()); + doLog( + `Cancel Delegate ${userAddress.substring(0, 8)}... -> ${to ? to.substring(0, 8) + '...' : '*'}` + ); return true; } default: diff --git a/packages/node-sdk/paima-db/src/sql/wallet-delegation.queries.ts b/packages/node-sdk/paima-db/src/sql/wallet-delegation.queries.ts index 96133a2f8..61a33011a 100644 --- a/packages/node-sdk/paima-db/src/sql/wallet-delegation.queries.ts +++ b/packages/node-sdk/paima-db/src/sql/wallet-delegation.queries.ts @@ -320,6 +320,7 @@ export const deleteDelegationsFrom = new PreparedQuery(deleteDelegationToIR); diff --git a/packages/node-sdk/paima-db/src/sql/wallet-delegation.sql b/packages/node-sdk/paima-db/src/sql/wallet-delegation.sql index ca20cd613..b563da902 100644 --- a/packages/node-sdk/paima-db/src/sql/wallet-delegation.sql +++ b/packages/node-sdk/paima-db/src/sql/wallet-delegation.sql @@ -47,7 +47,8 @@ WHERE from_id = :from_id!; /* @name deleteDelegationTo */ DELETE FROM delegations -WHERE to_id = :to_id!; +WHERE to_id = :to_id! +and from_id = :from_id!; /* @name deleteAddress */ DELETE FROM addresses diff --git a/packages/paima-sdk/paima-mw-core/src/delegate-wallet/index.ts b/packages/paima-sdk/paima-mw-core/src/delegate-wallet/index.ts index 0a8922c3d..34971519d 100644 --- a/packages/paima-sdk/paima-mw-core/src/delegate-wallet/index.ts +++ b/packages/paima-sdk/paima-mw-core/src/delegate-wallet/index.ts @@ -104,9 +104,7 @@ export class WalletConnectHelper { private static readonly SEP = ':'; public buildMessageToSign(subMessage: string): string { - return `${WalletConnectHelper.DELEGATE_WALLET_PREFIX}${ - WalletConnectHelper.SEP - }${subMessage.toLocaleLowerCase()}${WalletConnectHelper.SEP}${ENV.CONTRACT_ADDRESS}`; + return `${WalletConnectHelper.DELEGATE_WALLET_PREFIX}${WalletConnectHelper.SEP}${subMessage.toLocaleLowerCase()}`; } private getProvider(walletType: AddressType): IProvider {