|
| 1 | +import { Injectable, Logger, UnauthorizedException } from '@nestjs/common'; |
| 2 | +import { Request } from 'express'; |
| 3 | +import * as StellarSdk from 'stellar-sdk'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Wallet Signature Verification Service |
| 7 | + * |
| 8 | + * Handles verification of Stellar wallet signatures to authenticate users. |
| 9 | + * This service addresses issue #270 by ensuring that wallet address ownership |
| 10 | + * is verified before allowing sensitive operations like email updates. |
| 11 | + */ |
| 12 | +@Injectable() |
| 13 | +export class WalletSignatureService { |
| 14 | + private readonly logger = new Logger(WalletSignatureService.name); |
| 15 | + |
| 16 | + /** |
| 17 | + * Verify Stellar wallet signature |
| 18 | + * |
| 19 | + * @param signature - The signature from x-signature header |
| 20 | + * @param walletAddress - The wallet address claiming ownership |
| 21 | + * @param message - The message that was signed (default: "Login to SoroSusu") |
| 22 | + * @returns {boolean} True if signature is valid |
| 23 | + * @throws {UnauthorizedException} If signature is invalid |
| 24 | + */ |
| 25 | + verifySignature(signature: string, walletAddress: string, message: string = 'Login to SoroSusu'): boolean { |
| 26 | + try { |
| 27 | + if (!signature) { |
| 28 | + throw new UnauthorizedException('Signature is required'); |
| 29 | + } |
| 30 | + |
| 31 | + if (!walletAddress) { |
| 32 | + throw new UnauthorizedException('Wallet address is required'); |
| 33 | + } |
| 34 | + |
| 35 | + // Create a keypair from the wallet address |
| 36 | + const keypair = StellarSdk.Keypair.fromPublicKey(walletAddress); |
| 37 | + |
| 38 | + // Verify the signature |
| 39 | + const isValid = keypair.verify(message, signature); |
| 40 | + |
| 41 | + if (!isValid) { |
| 42 | + this.logger.warn(`Invalid signature for wallet ${walletAddress}`); |
| 43 | + throw new UnauthorizedException('Invalid signature'); |
| 44 | + } |
| 45 | + |
| 46 | + this.logger.log(`Signature verified successfully for wallet ${walletAddress}`); |
| 47 | + return true; |
| 48 | + } catch (error) { |
| 49 | + if (error instanceof UnauthorizedException) { |
| 50 | + throw error; |
| 51 | + } |
| 52 | + |
| 53 | + this.logger.error('Signature verification failed', error); |
| 54 | + throw new UnauthorizedException('Signature verification failed'); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Extract signature from request headers |
| 60 | + * |
| 61 | + * @param request - Express request object |
| 62 | + * @returns {string} The signature from x-signature header |
| 63 | + * @throws {UnauthorizedException} If signature header is missing |
| 64 | + */ |
| 65 | + extractSignature(request: Request): string { |
| 66 | + const signature = request.headers['x-signature'] as string; |
| 67 | + |
| 68 | + if (!signature) { |
| 69 | + this.logger.warn('Missing x-signature header in request'); |
| 70 | + throw new UnauthorizedException('x-signature header is required'); |
| 71 | + } |
| 72 | + |
| 73 | + return signature; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Verify wallet ownership for user update operations |
| 78 | + * |
| 79 | + * @param request - Express request object |
| 80 | + * @param userWalletAddress - The user's stored wallet address |
| 81 | + * @returns {boolean} True if ownership is verified |
| 82 | + * @throws {UnauthorizedException} If ownership cannot be verified |
| 83 | + */ |
| 84 | + verifyWalletOwnership(request: Request, userWalletAddress: string): boolean { |
| 85 | + try { |
| 86 | + const signature = this.extractSignature(request); |
| 87 | + |
| 88 | + if (!userWalletAddress) { |
| 89 | + throw new UnauthorizedException('User does not have a wallet address'); |
| 90 | + } |
| 91 | + |
| 92 | + return this.verifySignature(signature, userWalletAddress); |
| 93 | + } catch (error) { |
| 94 | + this.logger.error('Wallet ownership verification failed', error); |
| 95 | + throw error; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Generate message for signing |
| 101 | + * |
| 102 | + * @param customMessage - Optional custom message |
| 103 | + * @returns {string} Message to be signed by user |
| 104 | + */ |
| 105 | + generateSigningMessage(customMessage?: string): string { |
| 106 | + return customMessage || 'Login to SoroSusu'; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Validate Stellar address format |
| 111 | + * |
| 112 | + * @param address - The Stellar address to validate |
| 113 | + * @returns {boolean} True if address is valid |
| 114 | + */ |
| 115 | + isValidStellarAddress(address: string): boolean { |
| 116 | + try { |
| 117 | + StellarSdk.StrKey.decodeEd25519PublicKey(address); |
| 118 | + return true; |
| 119 | + } catch (error) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments