Skip to content

Commit

Permalink
Merge pull request #13 from nvergez/main
Browse files Browse the repository at this point in the history
Do not remove functionName & functionArgs if no args but receiver is smart contract
  • Loading branch information
andreibancioiu authored Sep 11, 2024
2 parents 4e36ecb + f9ff996 commit c12bd3e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/transaction.decoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { bech32 } from "bech32";

const SMART_CONTRACT_HEX_PUBKEY_PREFIX = "0".repeat(16);

export class TransactionDecoder {
getTransactionMetadata(transaction: TransactionToDecode): TransactionMetadata {
const metadata = this.getNormalTransactionMetadata(transaction);
Expand Down Expand Up @@ -45,7 +47,7 @@ export class TransactionDecoder {
metadata.functionArgs = args;
}

if (args.length === 0) {
if (args.length === 0 && !this.isSmartContract(transaction.receiver)) {
metadata.functionName = 'transfer';
metadata.functionArgs = undefined;
}
Expand Down Expand Up @@ -247,10 +249,19 @@ export class TransactionDecoder {
return bech32.encode('erd', words);
}

private bech32Decode(address: string): string | undefined {
const decoded = bech32.decodeUnsafe(address);
return decoded ? Buffer.from(bech32.fromWords(decoded.words)).toString('hex') : undefined;
}

private isAddressValid(address: string): boolean {
return Buffer.from(address, "hex").length == 32;
}

private isSmartContract(address: string): boolean {
return this.bech32Decode(address)?.startsWith(SMART_CONTRACT_HEX_PUBKEY_PREFIX) ?? false;
}

private isSmartContractArgument(arg: string): boolean {
if (!this.isHex(arg)) {
return false;
Expand Down
20 changes: 19 additions & 1 deletion test/decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@ test('Simple wallet to wallet transfer with dummy data text', () => {
sender: 'erd18w6yj09l9jwlpj5cjqq9eccfgulkympv7d4rj6vq4u49j8fpwzwsvx7e85',
receiver: 'erd1lkrrrn3ws9sp854kdpzer9f77eglqpeet3e3k3uxvqxw9p3eq6xqxj43r9',
value: BigInt('10000000000000000'),
functionName: 'transfer'
functionName: 'transfer',
});
});

test('Smart contract call without arguments', () => {
const decoder = new TransactionDecoder();
const metadata = decoder.getTransactionMetadata({
sender: 'erd18w6yj09l9jwlpj5cjqq9eccfgulkympv7d4rj6vq4u49j8fpwzwsvx7e85',
receiver: 'erd1qqqqqqqqqqqqqpgqmua7hcd05yxypyj7sv7pffrquy9gf86s535qxct34s',
data: 'bXlFbmRwb2ludA==',
value: '0',
});

expect(metadata).toEqual<TransactionMetadata>({
sender: 'erd18w6yj09l9jwlpj5cjqq9eccfgulkympv7d4rj6vq4u49j8fpwzwsvx7e85',
receiver: 'erd1qqqqqqqqqqqqqpgqmua7hcd05yxypyj7sv7pffrquy9gf86s535qxct34s',
value: BigInt('0'),
functionName: 'myEndpoint',
functionArgs: [],
});
});

Expand Down

0 comments on commit c12bd3e

Please sign in to comment.