Skip to content

Commit

Permalink
refactor: handle multiple unlock types in getInputsPreExpandedConfig (
Browse files Browse the repository at this point in the history
#1113)

* refactor: handle multiple unlock types in getInputsPreExpandedConfig

* feat: handle transitive unlocks

* refactor: create util `resolveTransitiveUnlock`

---------

Co-authored-by: Mario <[email protected]>
  • Loading branch information
VmMad and msarcev authored Feb 16, 2024
1 parent 79bfabf commit 507b49b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
17 changes: 5 additions & 12 deletions client/src/helpers/stardust/preExpandedConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import {
CommonOutput,
ExpirationUnlockCondition,
GovernorAddressUnlockCondition,
ReferenceUnlock,
SignatureUnlock,
StateControllerAddressUnlockCondition,
Unlock,
UnlockConditionType,
UnlockType,
Utils,
} from "@iota/sdk-wasm/web";
import { Bech32AddressHelper } from "~/helpers/stardust/bech32AddressHelper";
import { IInput } from "~models/api/stardust/IInput";
import { IOutput } from "~models/api/stardust/IOutput";
import { IPreExpandedConfig } from "~models/components";
import { resolveTransitiveUnlock } from "./resolveTransiviteUnlock";

const OUTPUT_EXPAND_CONDITIONS: UnlockConditionType[] = [
UnlockConditionType.Address,
Expand Down Expand Up @@ -44,15 +42,10 @@ export function getInputsPreExpandedConfig(inputs: IInput[], unlocks: Unlock[],
};
if (input?.output?.output && "unlockConditions" in input.output.output) {
const commmonOutput = input.output.output as unknown as CommonOutput;
let unlock = unlocks[idx];
if (unlock.type === UnlockType.Reference) {
const referenceUnlock = unlock as ReferenceUnlock;
unlock = unlocks[referenceUnlock.reference];
}
const unlockSignatureAddress = Utils.hexPublicKeyToBech32Address(
(unlock as SignatureUnlock).signature.publicKey,
bech32Hrp,
);

const signatureUnlock = resolveTransitiveUnlock(unlocks, idx);
const unlockSignatureAddress = Utils.hexPublicKeyToBech32Address(signatureUnlock.signature.publicKey, bech32Hrp);

preExpandedConfig = {
...preExpandedConfig,
unlockConditions: commmonOutput.unlockConditions?.map((unlockCondition) => {
Expand Down
19 changes: 19 additions & 0 deletions client/src/helpers/stardust/resolveTransiviteUnlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ReferenceUnlock, SignatureUnlock, Unlock, UnlockType } from "@iota/sdk-wasm/web";

export function resolveTransitiveUnlock(unlocks: Unlock[], unlockIndex: number): SignatureUnlock {
const unlock = unlocks[unlockIndex];
let signatureUnlock: SignatureUnlock;
if (unlock.type === UnlockType.Signature) {
signatureUnlock = unlock as SignatureUnlock;
} else {
let refUnlockIdx = unlockIndex;
// unlock references can be transitive,
// so we need to follow the path until we find the signature
do {
const referenceUnlock = unlocks[refUnlockIdx] as ReferenceUnlock;
signatureUnlock = unlocks[referenceUnlock.reference] as SignatureUnlock;
refUnlockIdx = referenceUnlock.reference;
} while (!signatureUnlock.signature);
}
return signatureUnlock;
}
30 changes: 7 additions & 23 deletions client/src/helpers/stardust/transactionsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ import {
Output,
OutputType,
PayloadType,
ReferenceUnlock,
RegularTransactionEssence,
SignatureUnlock,
StateControllerAddressUnlockCondition,
TagFeature,
TransactionPayload,
TreasuryOutput,
Unlock,
UnlockCondition,
UnlockConditionType,
UnlockType,
Utils,
UTXOInput,
} from "@iota/sdk-wasm/web";
Expand All @@ -36,6 +33,7 @@ import { IOutput } from "~models/api/stardust/IOutput";
import { MAINNET } from "~models/config/networkType";
import { StardustApiClient } from "~services/stardust/stardustApiClient";
import { Bech32AddressHelper } from "../stardust/bech32AddressHelper";
import { resolveTransitiveUnlock } from "./resolveTransiviteUnlock";

interface TransactionInputsAndOutputsResponse {
inputs: IInput[];
Expand Down Expand Up @@ -82,28 +80,14 @@ export class TransactionsHelper {

// unlock Addresses computed from public keys in unlocks
for (let i = 0; i < unlocks.length; i++) {
const unlock = payload.unlocks[i];
let signatureUnlock: SignatureUnlock;
const signatureUnlock = resolveTransitiveUnlock(unlocks, i);

if (unlock.type === UnlockType.Signature) {
signatureUnlock = unlock as SignatureUnlock;
} else {
let refUnlockIdx = i;
// unlock references can be transitive,
// so we need to follow the path until we find the signature
do {
const referenceUnlock = payload.unlocks[refUnlockIdx] as ReferenceUnlock;
signatureUnlock = payload.unlocks[referenceUnlock.reference] as SignatureUnlock;
refUnlockIdx = referenceUnlock.reference;
} while (!signatureUnlock.signature);
}

unlockAddresses.push(
Bech32AddressHelper.buildAddress(
_bechHrp,
Utils.hexPublicKeyToBech32Address(signatureUnlock.signature.publicKey, _bechHrp),
),
const address = Bech32AddressHelper.buildAddress(
_bechHrp,
Utils.hexPublicKeyToBech32Address(signatureUnlock.signature.publicKey, _bechHrp),
);

unlockAddresses.push(address);
}

const payloadEssence = payload.essence as RegularTransactionEssence;
Expand Down

0 comments on commit 507b49b

Please sign in to comment.