@@ -10,6 +10,8 @@ import {
1010} from '../resources' ;
1111import { XtzLib } from '../../src' ;
1212import { HashType } from '../../src/lib/iface' ;
13+ import { localForger } from '@taquito/local-forging' ;
14+ import { dataToSign , unsignedHex } from '../fixtures' ;
1315
1416describe ( 'XTZ util library' , function ( ) {
1517 describe ( 'address' , function ( ) {
@@ -238,6 +240,77 @@ describe('XTZ util library', function () {
238240 } ) ;
239241 } ) ;
240242
243+ describe ( 'packGenericMultisigDataToSign' , function ( ) {
244+ it ( 'should pack locally to the same bytes the node packs' , function ( ) {
245+ // The fixture dataToSign is the /pack_data RPC output for these exact parameters, so a local
246+ // match proves packGenericMultisigDataToSign is byte-identical to the node.
247+ const packed = XtzLib . Utils . packGenericMultisigDataToSign (
248+ 'KT18eKxkx71uMp28oZHjCiEj7QvFUaGzXtrZ' ,
249+ 'tz2PtJ9zgEgFVTRqy6GXsst54tH3ksEnYvvS' ,
250+ '11800000' ,
251+ '0'
252+ ) ;
253+ packed . should . equal ( dataToSign ) ;
254+ } ) ;
255+ } ) ;
256+
257+ describe ( 'assertDataToSignMatchesTransfer' , function ( ) {
258+ // The operation under test is decoded from the unsignedHex fixture with the same local forger
259+ // the transaction uses, so the multisig parameter shape is the production one.
260+ const getTransferOperation = async ( ) => {
261+ const parsed = await localForger . parse ( unsignedHex ) ;
262+ const operation = parsed . contents . find ( ( content ) => content . kind === 'transaction' && 'parameters' in content ) ;
263+ if ( ! operation ) {
264+ throw new Error ( 'fixture has no multisig transaction operation' ) ;
265+ }
266+ return operation as Parameters < typeof XtzLib . Utils . assertDataToSignMatchesTransfer > [ 0 ] ;
267+ } ;
268+
269+ it ( 'should accept the locally packed payload for the transfer' , async function ( ) {
270+ XtzLib . Utils . assertDataToSignMatchesTransfer ( await getTransferOperation ( ) , dataToSign ) ;
271+ } ) ;
272+
273+ it ( 'should reject a payload that authorizes a different transfer' , async function ( ) {
274+ // Flipping bytes yields a payload that is not derived from this transfer; accepting it would
275+ // let a tampered prebuild be signed with the user key.
276+ const tampered = dataToSign . slice ( 0 , - 4 ) + 'beef' ;
277+ const operation = await getTransferOperation ( ) ;
278+ assert . throws (
279+ ( ) => XtzLib . Utils . assertDataToSignMatchesTransfer ( operation , tampered ) ,
280+ new RegExp ( 'dataToSign does not match the transfer encoded in the transaction' )
281+ ) ;
282+ } ) ;
283+
284+ it ( 'should reject a non-multisig operation' , async function ( ) {
285+ const parsed = await localForger . parse ( unsignedHex ) ;
286+ const singlesigOperation = parsed . contents . find (
287+ ( content ) => content . kind === 'transaction' && ! ( 'parameters' in content )
288+ ) ;
289+ if ( singlesigOperation === undefined ) {
290+ // unsignedHex has no singlesig op; pass an operation without parameters directly to
291+ // exercise the same guard.
292+ const emptyOperation = { kind : 'transaction' , source : 'tz2HGMx8YxPSLguVP6usgf1D8UALPLchLoSX' } ;
293+ assert . throws (
294+ ( ) =>
295+ XtzLib . Utils . assertDataToSignMatchesTransfer (
296+ emptyOperation as unknown as Parameters < typeof XtzLib . Utils . assertDataToSignMatchesTransfer > [ 0 ] ,
297+ dataToSign
298+ ) ,
299+ new RegExp ( 'not a multisig transfer' )
300+ ) ;
301+ return ;
302+ }
303+ assert . throws (
304+ ( ) =>
305+ XtzLib . Utils . assertDataToSignMatchesTransfer (
306+ singlesigOperation as unknown as Parameters < typeof XtzLib . Utils . assertDataToSignMatchesTransfer > [ 0 ] ,
307+ dataToSign
308+ ) ,
309+ new RegExp ( 'not a multisig transfer' )
310+ ) ;
311+ } ) ;
312+ } ) ;
313+
241314 describe ( 'signature' , function ( ) {
242315 it ( 'should validate signature' , function ( ) {
243316 const validSignatures = [
0 commit comments