From 015ca392b33d38a8f9b2ed8cd09e73d78336f7f2 Mon Sep 17 00:00:00 2001 From: aman035 Date: Tue, 10 Sep 2024 16:18:01 +0530 Subject: [PATCH] fix: token parsing --- packages/core/src/lib/tx/tx.ts | 46 +++++++++++++++++++------------ packages/core/tests/tx/tx.test.ts | 26 ++++++++++++++++- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/packages/core/src/lib/tx/tx.ts b/packages/core/src/lib/tx/tx.ts index 2461f95b..6052c40d 100644 --- a/packages/core/src/lib/tx/tx.ts +++ b/packages/core/src/lib/tx/tx.ts @@ -1,5 +1,5 @@ import { v4 as uuidv4, parse } from 'uuid'; -import { utf8ToBytes } from '@noble/hashes/utils'; +import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils'; import { TxCategory, TxWithBlockHash } from './tx.types'; import { Transaction } from '../generated/tx'; import { InitDid } from '../generated/txData/init_did'; @@ -7,7 +7,8 @@ import { InitSessionKey } from '../generated/txData/init_session_key'; import { ENV } from '../constants'; import { Validator } from '../validator/validator'; import { TokenReply } from '../validator/validator.types'; -import { bytesToString } from 'viem'; +import { hexToBytes } from 'viem'; +import { privateKeyToAccount } from 'viem/accounts'; export class Tx { private constructor(private validator: Validator) {} @@ -72,20 +73,17 @@ export class Tx { * @param data Tx payload data in serialized form * @returns Unsigned Tx */ - createUnsigned = async ( + createUnsigned = ( category: string, recipients: string[], data: Uint8Array - ): Promise => { - // TODO: Finalize token encoding - const token = await this.validator.call('push_getApiToken'); + ): Transaction => { return Transaction.create({ type: 0, // Phase 0 only has non-value transfers category, recipients, data, salt: parse(uuidv4()), - apiToken: utf8ToBytes(JSON.stringify(token)), fee: '0', // Fee is 0 as of now }); }; @@ -127,28 +125,40 @@ export class Tx { unsignedTx: Transaction, session?: { sender: string; - privKey: string; + privKey: `0x${string}`; } ): Promise => { - let signedTx: Transaction; - const validatorUrl: string = ( - JSON.parse(bytesToString(unsignedTx.apiToken)) as TokenReply - ).apiUrl; + const token = await this.validator.call('push_getApiToken'); + let serializedSignedTx: Uint8Array; if (session) { - // TODO: sign the tx with sessionPrivKey - const tx = Transaction.create({ + const serializedUnsignedTx = Tx.serialize({ + ...unsignedTx, + sender: session.sender, + signature: new Uint8Array(0), + apiToken: utf8ToBytes( + Buffer.from(token.apiToken, 'base64').toString('utf-8') + ), + }); + const account = privateKeyToAccount(session.privKey); + const signature = await account.signMessage({ + message: { raw: serializedUnsignedTx }, + }); + serializedSignedTx = Tx.serialize({ ...unsignedTx, sender: session.sender, + signature: hexToBytes(signature), + apiToken: utf8ToBytes( + Buffer.from(token.apiToken, 'base64').toString('utf-8') + ), }); - signedTx = unsignedTx; } else { // TODO: connect with push Wallet and sign the tx - signedTx = unsignedTx; + serializedSignedTx = Tx.serialize(unsignedTx); } return await this.validator.call( 'push_sendTransaction', - [Tx.serialize(signedTx)], - validatorUrl + [bytesToHex(serializedSignedTx)], + token.apiUrl ); }; } diff --git a/packages/core/tests/tx/tx.test.ts b/packages/core/tests/tx/tx.test.ts index 571cb3bb..f2b977da 100644 --- a/packages/core/tests/tx/tx.test.ts +++ b/packages/core/tests/tx/tx.test.ts @@ -9,7 +9,9 @@ const mockInitDidTxData: InitDid = { masterPubKey: 'master_pub_key', derivedKeyIndex: 0, derivedPubKey: 'derived_pub_key', - encDerivedPrivKey: 'enc_derived_priv_key', + walletToEncDerivedKey: { + push10222n3232mwdeicej3: 'stringified_encrypted_pk', + }, }; const mockRecipients = [ 'eip155:1:0x35B84d6848D16415177c64D64504663b998A6ab4', @@ -105,4 +107,26 @@ describe('Tx', () => { const tx = await txInstance.search(txHash); expect(tx).toBeInstanceOf(Object); }); + it('should send for a tx with sessionKey', async () => { + const txInstance = await Tx.initialize(env); + const tx = txInstance.createUnsigned( + TxCategory.INIT_DID, + mockRecipients, + Tx.serializeData(mockInitDidTxData, TxCategory.INIT_DID) + ); + await txInstance.send(tx, { + sender: 'eip155:1:0x35B84d6848D16415177c64D64504663b998A6ab4', + privKey: + '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', + }); + }); + it('should send for a tx by connecting to Push Wallet', async () => { + const txInstance = await Tx.initialize(env); + const tx = txInstance.createUnsigned( + TxCategory.INIT_DID, + mockRecipients, + Tx.serializeData(mockInitDidTxData, TxCategory.INIT_DID) + ); + await txInstance.send(tx); + }); });