Skip to content

Commit d274680

Browse files
committed
fix: change AddressData to midgardAddress from ledger-deposit branch
1 parent f4b9664 commit d274680

7 files changed

Lines changed: 188 additions & 152 deletions

File tree

demo/midgard-node/pnpm-lock.yaml

Lines changed: 65 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/midgard-node/src/commands/listen.ts

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
import { StateQueueTx } from "@/transactions/index.js";
99
import * as SDK from "@al-ft/midgard-sdk";
1010
import { NodeSdk } from "@effect/opentelemetry";
11-
import { fromHex, getAddressDetails, toHex, CML } from "@lucid-evolution/lucid";
11+
import {
12+
fromHex,
13+
getAddressDetails,
14+
toHex,
15+
CML,
16+
} from "@lucid-evolution/lucid";
1217
import { PrometheusExporter } from "@opentelemetry/exporter-prometheus";
1318
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
1419
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
@@ -23,7 +28,7 @@ import {
2328
Queue,
2429
Ref,
2530
Schedule,
26-
Schema as S
31+
Schema as S,
2732
} from "effect";
2833
import {
2934
AddressHistoryDB,
@@ -66,7 +71,6 @@ import {
6671
txQueueProcessorFiber,
6772
} from "@/fibers/index.js";
6873
import { RequestError } from "@effect/platform/HttpServerError";
69-
import { isNotNull } from "effect/Predicate";
7074

7175
const TX_ENDPOINT: string = "tx";
7276
const ADDRESS_HISTORY_ENDPOINT: string = "txs";
@@ -104,10 +108,12 @@ const parseRequestBody = <A, I>(
104108
const result = S.decodeUnknownEither(schema)(body);
105109
if (result._tag === "Left") {
106110
yield* Effect.logInfo(`Invalid request body: ${result.left}`);
107-
return yield* Effect.fail(new RequestBodyParseError({
108-
message: "Invalid request body",
109-
cause: result.left,
110-
}));
111+
return yield* Effect.fail(
112+
new RequestBodyParseError({
113+
message: "Invalid request body",
114+
cause: result.left,
115+
}),
116+
);
111117
}
112118
return result.right;
113119
});
@@ -120,10 +126,7 @@ const handleRequestBodyParseFailure = (
120126
Effect.gen(function* () {
121127
const msg = `${method} /${endpoint} - ${error.message}: ${error.cause}`;
122128
yield* Effect.logInfo(msg);
123-
return yield* HttpServerResponse.json(
124-
{ error: msg },
125-
{ status: 400 },
126-
);
129+
return yield* HttpServerResponse.json({ error: msg }, { status: 400 });
127130
});
128131

129132
const failWith500Helper = (
@@ -635,7 +638,7 @@ const postTxOrderHandler = Effect.gen(function* () {
635638
{ status: 400 },
636639
);
637640
}
638-
const refundAddressData = yield* SDK.addressDataFromBech32(refund_address);
641+
const refundAddress = yield* SDK.midgardAddressFromBech32(refund_address);
639642

640643
yield* lucid.switchToOperatorsMainWallet;
641644

@@ -645,7 +648,7 @@ const postTxOrderHandler = Effect.gen(function* () {
645648
txOrderScriptAddress: txOrderAuthValidator.spendScriptAddress,
646649
mintingPolicy: txOrderAuthValidator.mintScript,
647650
policyId: txOrderAuthValidator.policyId,
648-
refundAddress: refundAddressData,
651+
refundAddress: refundAddress,
649652
refundDatum: refund_datum,
650653
midgardTxBody: "",
651654
midgardTxWits: "",
@@ -664,7 +667,7 @@ const postTxOrderHandler = Effect.gen(function* () {
664667
});
665668
}).pipe(
666669
Effect.catchTag("RequestBodyParseError", (e) =>
667-
handleRequestBodyParseFailure("POST", TX_ORDER_ENDPOINT, e)
670+
handleRequestBodyParseFailure("POST", TX_ORDER_ENDPOINT, e),
668671
),
669672
Effect.catchTag("HttpBodyError", (e) =>
670673
failWith500("POST", TX_ORDER_ENDPOINT, e),
@@ -711,7 +714,7 @@ const postDepositHandler = Effect.gen(function* () {
711714
{ status: 400 },
712715
);
713716
}
714-
const l2DatumValue = datum ?? null
717+
const l2DatumValue = datum ?? null;
715718

716719
yield* lucid.switchToOperatorsMainWallet;
717720

@@ -741,7 +744,7 @@ const postDepositHandler = Effect.gen(function* () {
741744
});
742745
}).pipe(
743746
Effect.catchTag("RequestBodyParseError", (e) =>
744-
handleRequestBodyParseFailure("POST", DEPOSIT_ENDPOINT, e)
747+
handleRequestBodyParseFailure("POST", DEPOSIT_ENDPOINT, e),
745748
),
746749
Effect.catchTag("HttpBodyError", (e) =>
747750
failWith500("POST", DEPOSIT_ENDPOINT, e),
@@ -798,7 +801,9 @@ const postWithdrawalHandler = Effect.gen(function* () {
798801
["", new Map([["", BigInt(withdrawal_body.l2_value)]])],
799802
]);
800803

801-
const l1AddressData = yield* SDK.addressDataFromBech32(withdrawal_body.l1_address);
804+
const l1Address = yield* SDK.midgardAddressFromBech32(
805+
withdrawal_body.l1_address,
806+
);
802807

803808
const l1Datum: SDK.CardanoDatum =
804809
withdrawal_body.l1_datum === "NoDatum"
@@ -812,31 +817,28 @@ const postWithdrawalHandler = Effect.gen(function* () {
812817
}),
813818
});
814819

815-
const refundAddressData = yield* SDK.addressDataFromBech32(refund_address);
820+
const refundAddress = yield* SDK.midgardAddressFromBech32(refund_address);
816821

817822
yield* lucid.switchToOperatorsMainWallet;
818823

819-
const signedTx = yield* SDK.unsignedWithdrawalTxProgram(
820-
lucid.api,
821-
{
822-
withdrawalScriptAddress: withdrawalAuthValidator.spendScriptAddress,
823-
mintingPolicy: withdrawalAuthValidator.mintScript,
824-
policyId: withdrawalAuthValidator.policyId,
825-
withdrawalBody: {
826-
l2_outref: {
827-
txHash: { hash: withdrawal_body.l2_outref.txHash.hash },
828-
outputIndex: BigInt(withdrawal_body.l2_outref.outputIndex),
829-
},
830-
l2_owner: withdrawal_body.l2_owner,
831-
l2_value: { inner: l2ValueInner },
832-
l1_address: l1AddressData,
833-
l1_datum: l1Datum,
824+
const signedTx = yield* SDK.unsignedWithdrawalTxProgram(lucid.api, {
825+
withdrawalScriptAddress: withdrawalAuthValidator.spendScriptAddress,
826+
mintingPolicy: withdrawalAuthValidator.mintScript,
827+
policyId: withdrawalAuthValidator.policyId,
828+
withdrawalBody: {
829+
l2_outref: {
830+
txHash: { hash: withdrawal_body.l2_outref.txHash.hash },
831+
outputIndex: BigInt(withdrawal_body.l2_outref.outputIndex),
834832
},
835-
withdrawalSignature: withdrawalSignatureMap,
836-
refundAddress: refundAddressData,
837-
refundDatum: refund_datum,
833+
l2_owner: withdrawal_body.l2_owner,
834+
l2_value: { inner: l2ValueInner },
835+
l1_address: l1Address,
836+
l1_datum: l1Datum,
838837
},
839-
);
838+
withdrawalSignature: withdrawalSignatureMap,
839+
refundAddress: refundAddress,
840+
refundDatum: refund_datum,
841+
});
840842

841843
yield* Effect.logInfo(`Submitting withdrawal order tx...`);
842844
const txHash = yield* handleSignSubmit(lucid.api, signedTx);
@@ -846,7 +848,7 @@ const postWithdrawalHandler = Effect.gen(function* () {
846848
});
847849
}).pipe(
848850
Effect.catchTag("RequestBodyParseError", (e) =>
849-
handleRequestBodyParseFailure("POST", WITHDRAWAL_ENDPOINT, e)
851+
handleRequestBodyParseFailure("POST", WITHDRAWAL_ENDPOINT, e),
850852
),
851853
Effect.catchTag("HttpBodyError", (e) =>
852854
failWith500("POST", WITHDRAWAL_ENDPOINT, e),

demo/midgard-node/src/genesis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const submitGenesisTxOrders: Effect.Effect<
8686
yield* lucid.switchToOperatorsMainWallet;
8787

8888
const l2Address = config.GENESIS_UTXOS[0].address;
89-
const l2AddressData = yield* SDK.addressDataFromBech32(l2Address);
89+
const l2AddressData = yield* SDK.midgardAddressFromBech32(l2Address);
9090
const inclusionTime = Date.now();
9191

9292
const txBuilder = lucid.api

demo/midgard-sdk/src/common.ts

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Data, getAddressDetails } from "@lucid-evolution/lucid";
1+
import {
2+
AddressDetails,
3+
credentialToAddress,
4+
Data,
5+
getAddressDetails,
6+
Network,
7+
} from "@lucid-evolution/lucid";
28
import { Data as EffectData } from "effect";
39
import { Effect } from "effect";
410
import {
@@ -193,6 +199,66 @@ export const bufferToHex = (buf: Buffer): string => {
193199
}
194200
};
195201

202+
/**
203+
* Assumes the given Bech32 string is that of a Cardano address (TODO).
204+
*/
205+
export const midgardAddressFromBech32 = (
206+
bechStr: string,
207+
): Effect.Effect<MidgardAddress, Bech32DeserializationError> =>
208+
Effect.gen(function* () {
209+
const addressDetails: AddressDetails = yield* Effect.try({
210+
try: () => getAddressDetails(bechStr),
211+
catch: (e) =>
212+
new Bech32DeserializationError({
213+
message: `Failed to break down ${bechStr} to its details.`,
214+
cause: e,
215+
}),
216+
});
217+
const cred = addressDetails.paymentCredential;
218+
if (cred === undefined) {
219+
return yield* new Bech32DeserializationError({
220+
message: `Failed extracting the payment credential from ${bechStr}.`,
221+
cause: "Unknown cause.",
222+
});
223+
} else {
224+
if (cred.type === "Key") {
225+
const midgardAddress: MidgardAddress = {
226+
PublicKeyCredential: [cred.hash],
227+
};
228+
return midgardAddress;
229+
} else {
230+
const midgardAddress: MidgardAddress = {
231+
ScriptCredential: [cred.hash],
232+
};
233+
return midgardAddress;
234+
}
235+
}
236+
});
237+
238+
/**
239+
* Taking Cardano `Network` as the first argument is temporary (TODO).
240+
*/
241+
export const midgardAddressToBech32 = (
242+
network: Network,
243+
addr: MidgardAddress,
244+
): string => {
245+
if ("PublicKeyCredential" in addr) {
246+
const [pubKeyHex] = addr.PublicKeyCredential;
247+
const cred: Credential = {
248+
type: "Key",
249+
hash: pubKeyHex,
250+
};
251+
return credentialToAddress(network, cred);
252+
} else {
253+
const [scriptHashHex] = addr.ScriptCredential;
254+
const cred: Credential = {
255+
type: "Script",
256+
hash: scriptHashHex,
257+
};
258+
return credentialToAddress(network, cred);
259+
}
260+
};
261+
196262
export const H32Schema = Data.Bytes({ minLength: 32, maxLength: 32 });
197263
export type H32 = Data.Static<typeof H32Schema>;
198264
export const H32 = H32Schema as unknown as H32;
@@ -265,43 +331,9 @@ export const AddressSchema = Data.Object({
265331
export type AddressData = Data.Static<typeof AddressSchema>;
266332
export const AddressData = AddressSchema as unknown as AddressData;
267333

268-
export const addressDataFromBech32 = (
269-
address: string,
270-
): Effect.Effect<AddressData, Bech32DeserializationError> =>
271-
Effect.gen(function* () {
272-
const { paymentCredential, stakeCredential } = getAddressDetails(address);
273-
if (!paymentCredential)
274-
return yield* Effect.fail(
275-
new Bech32DeserializationError({
276-
message: "Failed to parse address data",
277-
cause: "Payment key credential is undefined",
278-
}),
279-
);
280-
return {
281-
paymentCredential:
282-
paymentCredential.type === "Key"
283-
? {
284-
PublicKeyCredential: [paymentCredential.hash],
285-
}
286-
: {
287-
ScriptCredential: [paymentCredential.hash],
288-
},
289-
stakeCredential:
290-
stakeCredential && stakeCredential.hash
291-
? {
292-
Inline: [
293-
stakeCredential.type === "Key"
294-
? {
295-
PublicKeyCredential: [stakeCredential.hash],
296-
}
297-
: {
298-
ScriptCredential: [stakeCredential.hash],
299-
},
300-
],
301-
}
302-
: null,
303-
};
304-
});
334+
export const MidgardAddressSchema = CredentialSchema;
335+
export type MidgardAddress = CredentialD;
336+
export const MidgardAddress = MidgardAddressSchema as unknown as MidgardAddress;
305337

306338
export type GenericErrorFields = {
307339
readonly message: string;

demo/midgard-sdk/src/ledger-state.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
AddressSchema,
44
H32Schema,
55
MerkleRootSchema,
6+
MidgardAddress,
7+
MidgardAddressSchema,
68
OutputReferenceSchema,
79
POSIXTimeSchema,
810
PubKeyHashSchema,
@@ -136,7 +138,7 @@ export const WithdrawalBodySchema = Data.Object({
136138
l2_outref: OutputReferenceSchema,
137139
l2_owner: Data.Bytes({ minLength: 28, maxLength: 28 }),
138140
l2_value: ValueSchema,
139-
l1_address: AddressSchema,
141+
l1_address: MidgardAddressSchema,
140142
l1_datum: CardanoDatumSchema,
141143
});
142144
export type WithdrawalBody = Data.Static<typeof WithdrawalBodySchema>;

demo/midgard-sdk/src/user-events/tx-order.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ import {
1717
getStateToken,
1818
isEventUTxOInclusionTimeInBounds,
1919
makeReturn,
20+
MidgardAddress,
21+
MidgardAddressSchema,
2022
OutputReference,
2123
UnauthenticUtxoError,
2224
utxosAtByNFTPolicyId,
2325
} from "@/common.js";
2426
import {
25-
AddressData,
26-
AddressSchema,
2727
POSIXTime,
2828
POSIXTimeSchema,
2929
HashingError,
3030
LucidError,
3131
hashHexWithBlake2b256,
3232
} from "@/common.js";
33-
import { MidgardTxCompact, TxOrderEventSchema } from "@/ledger-state.js";
33+
import { TxOrderEventSchema } from "@/ledger-state.js";
3434
import {
3535
buildUserEventMintTransaction,
3636
UserEventMintRedeemer,
@@ -42,7 +42,7 @@ export type TxOrderParams = {
4242
txOrderScriptAddress: string;
4343
mintingPolicy: Script;
4444
policyId: string;
45-
refundAddress: AddressData;
45+
refundAddress: MidgardAddress;
4646
refundDatum: string;
4747
midgardTxBody: string;
4848
midgardTxWits: string;
@@ -52,7 +52,7 @@ export type TxOrderParams = {
5252
export const TxOrderDatumSchema = Data.Object({
5353
event: TxOrderEventSchema,
5454
inclusionTime: POSIXTimeSchema,
55-
refundAddress: AddressSchema,
55+
refundAddress: MidgardAddressSchema,
5656
refundDatum: Data.Nullable(Data.Bytes()),
5757
});
5858
export type TxOrderDatum = Data.Static<typeof TxOrderDatumSchema>;

demo/midgard-sdk/src/user-events/withdrawal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2-
AddressData,
3-
AddressSchema,
42
GenericErrorFields,
53
HashingError,
64
LucidError,
5+
MidgardAddress,
6+
MidgardAddressSchema,
77
POSIXTimeSchema,
88
hashHexWithBlake2b256,
99
makeReturn,
@@ -36,14 +36,14 @@ export type WithdrawalOrderParams = {
3636
policyId: string;
3737
withdrawalBody: WithdrawalBody;
3838
withdrawalSignature: WithdrawalSignature;
39-
refundAddress: AddressData;
39+
refundAddress: MidgardAddress;
4040
refundDatum: Data;
4141
};
4242

4343
export const WithdrawalOrderDatumSchema = Data.Object({
4444
event: WithdrawalEventSchema,
4545
inclusionTime: POSIXTimeSchema,
46-
refundAddress: AddressSchema,
46+
refundAddress: MidgardAddressSchema,
4747
refundDatum: Data.Any(),
4848
});
4949
export type WithdrawalOrderDatum = Data.Static<

0 commit comments

Comments
 (0)