Skip to content

Commit

Permalink
feat: Remove preExpandConfig logic around StateController for Nova
Browse files Browse the repository at this point in the history
  • Loading branch information
msarcev committed Apr 25, 2024
1 parent 0c06013 commit de43507
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions client/src/helpers/nova/preExpandedConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@ export function getInputsPreExpandedConfig(inputs: IInput[], unlocks: Unlock[],
const matchExpandCondition = commonOutput.unlockConditions?.find((unlockCondition) =>
INPUT_EXPAND_CONDITIONS.includes(unlockCondition.type),
);

preExpandedConfig = {
isPreExpanded: !!matchExpandCondition,
};

if (commonOutput.unlockConditions.length > 0) {
// unlockSignatureAddress is allready calculated in the input
const unlockSignatureAddress = input.address.bech32;
// special case for account unlock where the signature is the state controller address but the unlock condition is the account address
const { referencedStateControllerAddress, referencedAccountAddress } = getReferencedAddresses(
inputs,
unlocks[idx],
bech32Hrp,
);

preExpandedConfig = {
...preExpandedConfig,
Expand All @@ -61,11 +56,11 @@ export function getInputsPreExpandedConfig(inputs: IInput[], unlocks: Unlock[],
bech32Hrp,
(unlockCondition as AddressUnlockCondition).address,
)?.bech32;
return (
unlockAddress === unlockSignatureAddress ||
(unlockAddress === referencedAccountAddress &&
referencedStateControllerAddress === unlockSignatureAddress)
);

// special case for account unlock
const referencedAccountAddress = getReferencedAddresses(inputs, unlocks[idx], bech32Hrp);

return unlockAddress === unlockSignatureAddress || unlockAddress === referencedAccountAddress;
}
case UnlockConditionType.Expiration: {
const unlockAddress = AddressHelper.buildAddress(
Expand Down Expand Up @@ -100,25 +95,17 @@ export function getInputsPreExpandedConfig(inputs: IInput[], unlocks: Unlock[],
return inputsPreExpandedConfig;
}

function getReferencedAddresses(
inputs: IInput[],
unlock: Unlock,
bech32Hrp: string,
): { referencedStateControllerAddress: string; referencedAccountAddress: string } {
let referencedStateControllerAddress = "";
function getReferencedAddresses(inputs: IInput[], unlock: Unlock, bech32Hrp: string): string {
let referencedAccountAddress = "";
if (unlock.type === UnlockType.Account) {
const referencedAccountInput = inputs[(unlock as AccountUnlock).reference];
const referencedAccountOutput = referencedAccountInput?.output?.output as unknown as AccountOutput;
referencedAccountAddress =
AddressHelper.buildAddress(bech32Hrp, referencedAccountOutput.accountId, AddressType.Account)?.bech32 || "";

const referencedStateControllerAddressUC = referencedAccountOutput.unlockConditions.find(
(uc) => uc.type === UnlockConditionType.StateControllerAddress,
) as StateControllerAddressUnlockCondition;
referencedStateControllerAddress = AddressHelper.buildAddress(bech32Hrp, referencedStateControllerAddressUC.address)?.bech32 || "";
if (referencedAccountOutput?.accountId) {
referencedAccountAddress =
AddressHelper.buildAddress(bech32Hrp, referencedAccountOutput.accountId, AddressType.Account)?.bech32 || "";
}
}
return { referencedStateControllerAddress, referencedAccountAddress };
return referencedAccountAddress;
}

/**
Expand Down

0 comments on commit de43507

Please sign in to comment.