|
1 | 1 | const { Keypair } = require('stellar-sdk'); |
2 | 2 | const { TransactionBuilder, Networks } = require('stellar-sdk'); |
3 | 3 | const { fetchTomlValues } = require('../helpers/anchors'); |
4 | | -const { verifySiweMessage } = require('./siwe.service'); |
5 | | -const { keccak256 } = require('viem/utils'); |
6 | 4 |
|
7 | 5 | const { TOKEN_CONFIG } = require('../../constants/tokenConfig'); |
8 | 6 | const { SEP10_MASTER_SECRET, CLIENT_DOMAIN_SECRET } = require('../../constants/constants'); |
9 | 7 |
|
10 | 8 | const NETWORK_PASSPHRASE = Networks.PUBLIC; |
11 | 9 |
|
12 | | -async function deriveMemoFromAddress(address) { |
13 | | - const hash = keccak256(address); |
14 | | - return BigInt(hash).toString().slice(0, 15); |
15 | | -} |
16 | | - |
17 | | -// we validate a challenge for a given nonce. From it we obtain the address and derive the memo |
18 | | -// we can then ensure that the memo is the same as the one we expect from the anchor challenge |
19 | | -const validateSignatureAndGetMemo = async (nonce, userChallengeSignature, memoEnabled) => { |
20 | | - if (!userChallengeSignature || !nonce || !memoEnabled) { |
21 | | - return null; // Default memo value when single stellar account is used |
22 | | - } |
23 | | - |
24 | | - let message; |
25 | | - try { |
26 | | - // initialSiweMessage must be undefined after an initial check, |
27 | | - // message must exist on the map. |
28 | | - message = await verifySiweMessage(nonce, userChallengeSignature, undefined); |
29 | | - } catch (e) { |
30 | | - throw new Error(`Could not verify signature: ${e.message}`); |
31 | | - } |
32 | | - |
33 | | - const memo = await deriveMemoFromAddress(message.address); |
34 | | - return memo; |
35 | | -}; |
36 | | - |
37 | | -exports.signSep10Challenge = async (challengeXDR, outToken, clientPublicKey, userChallengeSignature, nonce) => { |
| 10 | +exports.signSep10Challenge = async (challengeXDR, outToken, clientPublicKey, memo) => { |
38 | 11 | const masterStellarKeypair = Keypair.fromSecret(SEP10_MASTER_SECRET); |
39 | 12 | const clientDomainStellarKeypair = Keypair.fromSecret(CLIENT_DOMAIN_SECRET); |
40 | 13 |
|
41 | 14 | const { signingKey: anchorSigningKey } = await fetchTomlValues(TOKEN_CONFIG[outToken].tomlFileUrl); |
42 | 15 | const { homeDomain, clientDomainEnabled, memoEnabled } = TOKEN_CONFIG[outToken]; |
43 | 16 |
|
44 | | - // Expected memo based on user's signature and nonce. |
45 | | - const memo = await validateSignatureAndGetMemo(nonce, userChallengeSignature, memoEnabled); |
46 | | - |
47 | 17 | const transactionSigned = new TransactionBuilder.fromXDR(challengeXDR, NETWORK_PASSPHRASE); |
48 | 18 | if (transactionSigned.source !== anchorSigningKey) { |
49 | 19 | throw new Error(`Invalid source account: ${transactionSigned.source}`); |
|
0 commit comments