Skip to content

Commit 7f42a30

Browse files
committed
fix: renamed EXP unit test to a more comprehensive name and added verify
1 parent 6f11ce7 commit 7f42a30

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

packages/thorest/tests/thor/transactions/EXP.unit.test.ts packages/thorest/tests/thor/transactions/TransactionDelegationAndVerification.unit.test.ts

+74-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import {
44
Clause,
55
HexUInt,
66
networkInfo,
7+
Secp256k1,
78
Transaction,
89
type TransactionBody,
910
VET
1011
} from '@vechain/sdk-core';
11-
import {
12-
type FetchHttpClient,
13-
type HttpPath,
14-
type HttpQuery
15-
} from '../../../src';
12+
import { type FetchHttpClient, type HttpPath } from '../../../src';
13+
14+
import { secp256k1 as nc_secp256k1 } from '@noble/curves/secp256k1';
1615

1716
const mockHttpClient = <T>(response: T): FetchHttpClient => {
1817
return {
@@ -110,7 +109,7 @@ describe('unit tests', () => {
110109
'/transactions' as unknown as HttpPath,
111110
{
112111
query: HexUInt.of(tx.encoded).toString()
113-
} as unknown as HttpQuery
112+
}
114113
);
115114
expect(await txResult.json()).toEqual(mockTxResponse);
116115

@@ -120,4 +119,73 @@ describe('unit tests', () => {
120119
);
121120
expect(await txReceipt.json()).toEqual(mockTxReceiptResponse);
122121
});
122+
123+
test('verify signatures', () => {
124+
const latestBlock = {
125+
id: '0x0000000000000000000000000000000000000000000000000000000000000123'
126+
};
127+
const gasToPay = { totalGas: 21000 };
128+
const gasPayerPublicKey = Secp256k1.derivePublicKey(
129+
gasPayer.privateKey.bytes,
130+
false
131+
);
132+
const txA = Transaction.of({
133+
chainTag: networkInfo.solo.chainTag,
134+
blockRef: latestBlock?.id.slice(0, 18) ?? '0x0',
135+
expiration: 0,
136+
clauses,
137+
gasPriceCoef: 0,
138+
gas: gasToPay.totalGas,
139+
dependsOn: null,
140+
nonce: 1,
141+
reserved: {
142+
features: 1 // set the transaction to be delegated
143+
}
144+
});
145+
const as = txA.signAsSender(sender.privateKey.bytes);
146+
const ap = as.signAsGasPayer(sender.address, gasPayer.privateKey.bytes);
147+
const sigmaA = nc_secp256k1.Signature.fromCompact(
148+
ap.signature?.slice(-65).slice(0, 64) as Uint8Array
149+
);
150+
const hashA = ap.getTransactionHash(sender.address).bytes;
151+
const isVerifiedA = nc_secp256k1.verify(
152+
sigmaA,
153+
hashA,
154+
gasPayerPublicKey
155+
);
156+
expect(isVerifiedA).toBe(true);
157+
158+
const txB = Transaction.of({
159+
chainTag: networkInfo.solo.chainTag,
160+
blockRef: latestBlock?.id.slice(0, 18) ?? '0x0',
161+
expiration: 0,
162+
clauses,
163+
gasPriceCoef: 0,
164+
gas: gasToPay.totalGas,
165+
dependsOn: null,
166+
nonce: 2,
167+
reserved: {
168+
features: 1 // set the transaction to be delegated
169+
}
170+
});
171+
const bs = txB.signAsSender(sender.privateKey.bytes);
172+
const bp = bs.signAsGasPayer(sender.address, gasPayer.privateKey.bytes);
173+
const sigmaB = nc_secp256k1.Signature.fromCompact(
174+
bp.signature?.slice(-65).slice(0, 64) as Uint8Array
175+
);
176+
const hashB = bp.getTransactionHash(sender.address).bytes;
177+
const isVerifiedB = nc_secp256k1.verify(
178+
sigmaB,
179+
hashB,
180+
gasPayerPublicKey
181+
);
182+
expect(isVerifiedB).toBe(true);
183+
184+
const isVerifiedForge = nc_secp256k1.verify(
185+
sigmaA,
186+
hashB,
187+
gasPayerPublicKey
188+
);
189+
expect(isVerifiedForge).toBe(false);
190+
});
123191
});

0 commit comments

Comments
 (0)