Skip to content

Commit 3fc814b

Browse files
committed
feat: make signatureType optional and add detection function
1 parent 495708e commit 3fc814b

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

packages/sdk/src/Actions/EventAction.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export interface ActionClaimant {
157157
*
158158
* @type {SignatureType}
159159
*/
160-
signatureType: SignatureType;
160+
signatureType?: SignatureType;
161161
/**
162162
* The 4 byte signature of the event or function
163163
*
@@ -202,7 +202,7 @@ export interface ActionStep {
202202
*
203203
* @type {SignatureType}
204204
*/
205-
signatureType: SignatureType;
205+
signatureType?: SignatureType;
206206
/**
207207
* The type of action being performed.
208208
*
@@ -1366,15 +1366,32 @@ function _fromRawActionStep<T extends RawActionStep | RawActionClaimant>(
13661366
}
13671367

13681368
/**
1369-
* Typeguard to determine if a user is supplying a simple or raw EventActionPayload
1369+
* Determines whether a signature is an event or function signature based on its format.
1370+
* - 32-byte signatures (0x + 64 chars) that don't start with 28 zeros are event signatures
1371+
* - 4-byte signatures (0x + 8 chars) or 32-byte signatures with 28 leading zeros are function signatures
13701372
*
1371-
* @param {*} opts
1372-
* @returns {opts is EventActionPayloadSimple}
1373+
* @param {Hex} signature - The signature to check
1374+
* @returns {SignatureType} The detected signature type
13731375
*/
1374-
function _isEventActionPayloadSimple(
1375-
opts: EventActionPayload,
1376-
): opts is EventActionPayloadSimple {
1377-
return Array.isArray((opts as EventActionPayloadSimple).actionSteps);
1376+
export function detectSignatureType(signature: Hex): SignatureType {
1377+
const hexWithoutPrefix = signature.slice(2);
1378+
1379+
// 4-byte function selector (8 hex chars)
1380+
if (hexWithoutPrefix.length === 8) {
1381+
return SignatureType.FUNC;
1382+
}
1383+
1384+
// I32-byte selectors (64 hex chars)
1385+
if (hexWithoutPrefix.length === 64) {
1386+
// Check if it starts with 28 bytes (56 chars) of zeros
1387+
const leadingPart = hexWithoutPrefix.slice(0, 56);
1388+
if (leadingPart === '0'.repeat(56)) {
1389+
return SignatureType.FUNC;
1390+
}
1391+
return SignatureType.EVENT;
1392+
}
1393+
1394+
throw new Error('Invalid signature format');
13781395
}
13791396

13801397
/**
@@ -1502,21 +1519,38 @@ export function prepareEventActionPayload({
15021519
],
15031520
[
15041521
{
1505-
actionClaimant: _toRawActionStep(actionClaimant),
1522+
actionClaimant: {
1523+
..._toRawActionStep(actionClaimant),
1524+
signatureType:
1525+
actionClaimant.signatureType ??
1526+
detectSignatureType(actionClaimant.signature),
1527+
},
15061528
actionStepOne: {
15071529
..._toRawActionStep(actionStepOne),
1530+
signatureType:
1531+
actionStepOne.signatureType ??
1532+
detectSignatureType(actionStepOne.signature),
15081533
actionType: actionStepOne.actionType || 0,
15091534
},
15101535
actionStepTwo: {
15111536
..._toRawActionStep(actionStepTwo),
1537+
signatureType:
1538+
actionStepTwo.signatureType ??
1539+
detectSignatureType(actionStepTwo.signature),
15121540
actionType: actionStepTwo.actionType || 0,
15131541
},
15141542
actionStepThree: {
15151543
..._toRawActionStep(actionStepThree),
1544+
signatureType:
1545+
actionStepThree.signatureType ??
1546+
detectSignatureType(actionStepThree.signature),
15161547
actionType: actionStepThree.actionType || 0,
15171548
},
15181549
actionStepFour: {
15191550
..._toRawActionStep(actionStepFour),
1551+
signatureType:
1552+
actionStepFour.signatureType ??
1553+
detectSignatureType(actionStepFour.signature),
15201554
actionType: actionStepFour.actionType || 0,
15211555
},
15221556
},

0 commit comments

Comments
 (0)