Skip to content

Commit

Permalink
chore: more complete solution
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Dec 19, 2024
1 parent f2b83b4 commit 4183976
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 157 deletions.
54 changes: 36 additions & 18 deletions docs/typescript/core/Blaze/classes/BlazeHelper.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Cardano addresses. These functions include:

## Methods

### getAddressHashes()
### getAddressAsType()

> `static` **getAddressHashes**(`address`): `object`
> `static` **getAddressAsType**(`address`): `object`
Helper function to parse addresses hashes from a Bech32 encoded address.

Expand All @@ -37,13 +37,9 @@ Helper function to parse addresses hashes from a Bech32 encoded address.

`object`

##### paymentCredentials
##### address

> **paymentCredentials**: `Hash28ByteBase16`
##### stakeCredentials?

> `optional` **stakeCredentials**: `string`
> **address**: `Address`
##### type

Expand All @@ -55,25 +51,47 @@ Helper function to parse addresses hashes from a Bech32 encoded address.

***

### isScriptAddress()
### isBaseAddress()

> `static` **isScriptAddress**(`address`): `boolean`
> `static` **isBaseAddress**(`type`): `boolean`
Helper function to check if an address is a script address.
Helper method to determine address type.

#### Parameters

**address**: `string`
**type**: `AddressType`

The address type.

#### Returns

`boolean`

#### Defined in

[packages/core/src/Utilities/BlazeHelper.class.ts:60](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L60)

***

### isEnterpriseAddress()

> `static` **isEnterpriseAddress**(`type`): `boolean`
Helper method to determine address type.

#### Parameters

**type**: `AddressType`

The Bech32 encoded address.
The address type.

#### Returns

`boolean`

#### Defined in

[packages/core/src/Utilities/BlazeHelper.class.ts:140](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L140)
[packages/core/src/Utilities/BlazeHelper.class.ts:47](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L47)

***

Expand All @@ -97,7 +115,7 @@ Throws a useful error if the address, network, and instance network are on the w

#### Defined in

[packages/core/src/Utilities/BlazeHelper.class.ts:186](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L186)
[packages/core/src/Utilities/BlazeHelper.class.ts:203](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L203)

***

Expand All @@ -119,7 +137,7 @@ Throws an error describing the address and contextual information.

#### Defined in

[packages/core/src/Utilities/BlazeHelper.class.ts:213](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L213)
[packages/core/src/Utilities/BlazeHelper.class.ts:230](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L230)

***

Expand All @@ -146,7 +164,7 @@ and that the address is on the correct network.

#### Defined in

[packages/core/src/Utilities/BlazeHelper.class.ts:83](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L83)
[packages/core/src/Utilities/BlazeHelper.class.ts:112](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L112)

***

Expand All @@ -168,4 +186,4 @@ Validates that an address matches the provided network.

#### Defined in

[packages/core/src/Utilities/BlazeHelper.class.ts:154](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L154)
[packages/core/src/Utilities/BlazeHelper.class.ts:167](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/Utilities/BlazeHelper.class.ts#L167)
10 changes: 5 additions & 5 deletions docs/typescript/core/Blaze/classes/DatumBuilderBlazeV1.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The current network id.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:41](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L41)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:42](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L42)

***

Expand All @@ -33,7 +33,7 @@ The error to throw when the pool ident does not match V1 constraints.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:43](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L43)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:44](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L44)

## Methods

Expand Down Expand Up @@ -102,7 +102,7 @@ An object containing the hash of the inline datum, the inline datum itself,
#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:96](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L96)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:97](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L97)

***

Expand Down Expand Up @@ -184,7 +184,7 @@ An object containing the hash of the inline datum, the inline datum itself,
#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:62](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L62)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:63](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L63)

***

Expand Down Expand Up @@ -254,4 +254,4 @@ An object comprising the hash of the inline datum, the inline datum itself,
#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:141](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L141)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts:142](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts#L142)
12 changes: 6 additions & 6 deletions docs/typescript/core/Blaze/classes/DatumBuilderBlazeV3.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ The UTxO txHash and index.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:527](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L527)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:526](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L526)

***

Expand All @@ -389,7 +389,7 @@ The hex encoded pool ident.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:503](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L503)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:502](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L502)

***

Expand All @@ -411,7 +411,7 @@ The hex encoded pool ident.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:491](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L491)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:490](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L490)

***

Expand All @@ -433,7 +433,7 @@ The hex encoded pool ident.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:515](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L515)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:514](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L514)

***

Expand Down Expand Up @@ -471,7 +471,7 @@ An object containing the staking and
#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:558](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L558)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:557](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L557)

***

Expand Down Expand Up @@ -500,4 +500,4 @@ The signing key associated with the owner, extracted from the datum. This key is

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:600](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L600)
[packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts:599](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts#L599)
77 changes: 50 additions & 27 deletions packages/core/src/DatumBuilders/DatumBuilder.Blaze.V1.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
TWithdrawOrder,
WithdrawOrder,
} from "./ContractTypes/Contract.Blaze.v1.js";
import { V1Types } from "./ContractTypes/index.js";

/**
* The Blaze implementation for building valid Datums for
Expand Down Expand Up @@ -203,54 +204,76 @@ export class DatumBuilderBlazeV1 implements DatumBuilder {
});

const { DestinationAddress, AlternateAddress } = addresses;
const destinationHashes = BlazeHelper.getAddressHashes(
DestinationAddress.address,
);

if (DestinationAddress.datum.type === EDatumType.INLINE) {
throw new Error(
"Inline datum types are not supported in V1 contracts! Convert this to a hash.",
);
}

const destinationAddressCredentialType = BlazeHelper.isScriptAddress(
const paymentPart = BlazeHelper.getPaymentHashFromBech32(
DestinationAddress.address,
)
? ("ScriptHash" as keyof TDestination["credentials"]["paymentKey"])
: "KeyHash";
);
const stakingPart = BlazeHelper.getStakingHashFromBech32(
DestinationAddress.address,
);

let paymentKeyData: V1Types.TPaymentStakingHash | undefined;
if (BlazeHelper.isScriptAddress(DestinationAddress.address)) {
paymentKeyData = {
ScriptHash: {
value: paymentPart,
},
};
} else {
paymentKeyData = {
KeyHash: {
value: paymentPart,
},
};
}

let stakingKeyData: V1Types.TPaymentStakingHash | undefined;
if (stakingPart) {
if (BlazeHelper.isScriptAddress(DestinationAddress.address)) {
stakingKeyData = {
ScriptHash: {
value: stakingPart,
},
};
} else {
stakingKeyData = {
KeyHash: {
value: stakingPart,
},
};
}
}

const destination: TDestination = {
credentials: {
paymentKey: {
[destinationAddressCredentialType]: {
value: destinationHashes.paymentCredentials,
},
},
stakingKey: destinationHashes.stakeCredentials
? {
value: {
[destinationAddressCredentialType]: {
value: destinationHashes.stakeCredentials,
},
},
}
: null,
paymentKey: paymentKeyData,
stakingKey: stakingKeyData ? { value: stakingKeyData } : null,
},
datum:
DestinationAddress.datum.type !== EDatumType.NONE
? DestinationAddress.datum.value
: null,
};

const alternateHashes =
AlternateAddress && BlazeHelper.getAddressHashes(AlternateAddress);
let alternatePaymentPart: string | null = null;
let alternateStakingPart: string | undefined;

if (AlternateAddress) {
alternatePaymentPart =
BlazeHelper.getPaymentHashFromBech32(AlternateAddress);
alternateStakingPart =
BlazeHelper.getStakingHashFromBech32(AlternateAddress);
}

const datum: TOrderAddresses = {
destination,
alternate: alternateHashes
? (alternateHashes.stakeCredentials ??
alternateHashes.paymentCredentials)
: null,
alternate: alternateStakingPart || alternatePaymentPart,
};

const data = Data.to<TOrderAddresses>(datum, OrderAddresses);
Expand Down
25 changes: 12 additions & 13 deletions packages/core/src/DatumBuilders/DatumBuilder.Blaze.V3.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ export class DatumBuilderBlazeV3 implements DatumBuilder {
network: this.network,
});

const destination = BlazeHelper.getAddressHashes(address);
let formattedDatum: V3Types.TDestination["datum"];
switch (datum.type) {
case EDatumType.NONE:
Expand All @@ -352,25 +351,28 @@ export class DatumBuilderBlazeV3 implements DatumBuilder {
);
}

const paymentPart = BlazeHelper.getPaymentHashFromBech32(address);
const stakingPart = BlazeHelper.getStakingHashFromBech32(address);

const destinationDatum: V3Types.TDestination = {
address: {
paymentCredential: BlazeHelper.isScriptAddress(address)
? {
SCredential: {
bytes: destination.paymentCredentials,
bytes: paymentPart,
},
}
: {
VKeyCredential: {
bytes: destination.paymentCredentials,
bytes: paymentPart,
},
},

stakeCredential: destination?.stakeCredentials
stakeCredential: stakingPart
? {
keyHash: {
VKeyCredential: {
bytes: destination.stakeCredentials,
bytes: stakingPart,
},
},
}
Expand All @@ -390,20 +392,17 @@ export class DatumBuilderBlazeV3 implements DatumBuilder {

public buildOwnerDatum(main: string): TDatumResult<V3Types.TMultiSigScript> {
BlazeHelper.validateAddressNetwork(main, this.network);
const { stakeCredentials, paymentCredentials, type } =
BlazeHelper.getAddressHashes(main);
const paymentPart = BlazeHelper.getPaymentHashFromBech32(main);
const stakingPart = BlazeHelper.getStakingHashFromBech32(main);

let ownerDatum: V3Types.TMultiSigScript;
if (
type === Core.AddressType.BasePaymentScriptStakeKey ||
type === Core.AddressType.BasePaymentScriptStakeScript
) {
if (BlazeHelper.isScriptAddress(main)) {
ownerDatum = {
Script: { hex: stakeCredentials || paymentCredentials },
Script: { hex: stakingPart || paymentPart },
};
} else {
ownerDatum = {
Address: { hex: stakeCredentials || paymentCredentials },
Address: { hex: stakingPart || paymentPart },
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const V1_EXPECTATIONS = {
} as TOrderAddressesArgs,
expectations: {
error:
"Invalid address. Make sure you are using a Bech32 encoded address that includes the payment key.",
"Could not find a payment key in the address: stake_test1uqxn89tuq7kdmhkvnzpy2ldz9uz7p5vf7l7ftvvh9ek4zpghgvr36",
},
},
{
Expand Down
Loading

0 comments on commit 4183976

Please sign in to comment.