Skip to content

Commit

Permalink
fix: token parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Sep 10, 2024
1 parent a492aeb commit 015ca39
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
46 changes: 28 additions & 18 deletions packages/core/src/lib/tx/tx.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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';
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) {}
Expand Down Expand Up @@ -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<Transaction> => {
// TODO: Finalize token encoding
const token = await this.validator.call<TokenReply>('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
});
};
Expand Down Expand Up @@ -127,28 +125,40 @@ export class Tx {
unsignedTx: Transaction,
session?: {
sender: string;
privKey: string;
privKey: `0x${string}`;
}
): Promise<string> => {
let signedTx: Transaction;
const validatorUrl: string = (
JSON.parse(bytesToString(unsignedTx.apiToken)) as TokenReply
).apiUrl;
const token = await this.validator.call<TokenReply>('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<string>(
'push_sendTransaction',
[Tx.serialize(signedTx)],
validatorUrl
[bytesToHex(serializedSignedTx)],
token.apiUrl
);
};
}
26 changes: 25 additions & 1 deletion packages/core/tests/tx/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 015ca39

Please sign in to comment.