From d3a2a94572040395c282af72b7703b38a2aa5950 Mon Sep 17 00:00:00 2001
From: mtyszczak <mateusz.tyszczak@gmail.com>
Date: Tue, 4 Mar 2025 15:15:38 +0100
Subject: [PATCH] Add structuredClone shim for metamask SES support

---
 .../hive_authority/hive_role_authority_definition.ts        | 1 +
 ts/wasm/lib/detailed/formatters/base.ts                     | 1 +
 ts/wasm/lib/detailed/formatters/waxify.ts                   | 1 +
 ts/wasm/lib/detailed/shims/structuredclone.ts               | 6 ++++++
 ts/wasm/lib/detailed/transaction.ts                         | 1 +
 .../lib/detailed/util/account_authority_caching_provider.ts | 1 +
 ts/wasm/lib/detailed/util/api_caller.ts                     | 1 +
 7 files changed, 12 insertions(+)
 create mode 100644 ts/wasm/lib/detailed/shims/structuredclone.ts

diff --git a/ts/wasm/lib/detailed/complex_operations/role_classes/categories/hive_authority/hive_role_authority_definition.ts b/ts/wasm/lib/detailed/complex_operations/role_classes/categories/hive_authority/hive_role_authority_definition.ts
index 8ce56d083..730134e7e 100644
--- a/ts/wasm/lib/detailed/complex_operations/role_classes/categories/hive_authority/hive_role_authority_definition.ts
+++ b/ts/wasm/lib/detailed/complex_operations/role_classes/categories/hive_authority/hive_role_authority_definition.ts
@@ -3,6 +3,7 @@ import { TAccountName } from "../../../../hive_apps_operations/index.js";
 import { WaxError } from "../../../../errors.js";
 import { LevelBase } from "../../level_base.js";
 import type { TPublicKey } from "../../../../interfaces";
+import { structuredClone } from "../../../../shims/structuredclone.js";
 
 export class HiveRoleAuthorityDefinition<TRole extends string> extends LevelBase<TRole> {
   public constructor(
diff --git a/ts/wasm/lib/detailed/formatters/base.ts b/ts/wasm/lib/detailed/formatters/base.ts
index 4f689f12a..1b69dd663 100644
--- a/ts/wasm/lib/detailed/formatters/base.ts
+++ b/ts/wasm/lib/detailed/formatters/base.ts
@@ -1,4 +1,5 @@
 import { DeepPartial, DeepReadonly, IWaxFormatter, IWaxFormatterOptions } from "./types";
+import { structuredClone } from "../shims/structuredclone.js";
 
 export const DEFAULT_FORMATTER_OPTIONS: IWaxFormatterOptions = {
   asset: {
diff --git a/ts/wasm/lib/detailed/formatters/waxify.ts b/ts/wasm/lib/detailed/formatters/waxify.ts
index 796e230d9..a7be8f530 100644
--- a/ts/wasm/lib/detailed/formatters/waxify.ts
+++ b/ts/wasm/lib/detailed/formatters/waxify.ts
@@ -5,6 +5,7 @@ import { WaxFormatterBase } from "./base";
 import { DefaultFormatters } from "./default_formatters";
 import Long from "long";
 import { getMetadata } from "reflect-metadata/no-conflict";
+import { structuredClone } from "../shims/structuredclone.js";
 
 export interface IMatchersData {
   matchValues: Map<string, TFormatFunction>;
diff --git a/ts/wasm/lib/detailed/shims/structuredclone.ts b/ts/wasm/lib/detailed/shims/structuredclone.ts
new file mode 100644
index 000000000..4315362f9
--- /dev/null
+++ b/ts/wasm/lib/detailed/shims/structuredclone.ts
@@ -0,0 +1,6 @@
+export const structuredClone = (value: any) => {
+  if (typeof globalThis.structuredClone === "undefined")
+    return JSON.parse(JSON.stringify(value));
+
+  return globalThis.structuredClone(value);
+};
diff --git a/ts/wasm/lib/detailed/transaction.ts b/ts/wasm/lib/detailed/transaction.ts
index 0e4bcbe65..7ec354b6e 100644
--- a/ts/wasm/lib/detailed/transaction.ts
+++ b/ts/wasm/lib/detailed/transaction.ts
@@ -10,6 +10,7 @@ import type { ApiTransaction } from "./api";
 import { safeWasmCall } from "./util/wasm_errors";
 import type { TAccountName } from "./hive_apps_operations";
 import { ISignatureProvider } from "./extensions/signatures";
+import { structuredClone } from "./shims/structuredclone.js";
 
 type TIndexBeginEncryption = {
   mainEncryptionKey: TPublicKey;
diff --git a/ts/wasm/lib/detailed/util/account_authority_caching_provider.ts b/ts/wasm/lib/detailed/util/account_authority_caching_provider.ts
index a4795e774..175489cd8 100644
--- a/ts/wasm/lib/detailed/util/account_authority_caching_provider.ts
+++ b/ts/wasm/lib/detailed/util/account_authority_caching_provider.ts
@@ -3,6 +3,7 @@ import { TAccountName } from "../hive_apps_operations";
 import type { TPublicKey } from "../interfaces";
 import { WaxError } from "../errors";
 import { ClassHandle, IAccountAuthorityProvider, wax_authority } from "wasm/lib/build_wasm/wax.common";
+import { structuredClone } from "../shims/structuredclone.js";
 
 /**
  * Helper class used during traced authority verification process.
diff --git a/ts/wasm/lib/detailed/util/api_caller.ts b/ts/wasm/lib/detailed/util/api_caller.ts
index 59878aadc..8bb3eac39 100644
--- a/ts/wasm/lib/detailed/util/api_caller.ts
+++ b/ts/wasm/lib/detailed/util/api_caller.ts
@@ -3,6 +3,7 @@ import { type TWaxApiRequest } from "../interfaces";
 import { extractBracedStrings } from "../rest-api/utils.js";
 import { objectToQueryString } from "./query_string.js";
 import { type IDetailedResponseData, type IRequestOptions, RequestHelper } from "./request_helper.js";
+import { structuredClone } from "../shims/structuredclone.js";
 
 export type TInterceptorRequestOptions = IRequestOptions & { paths: string[]; apiCallerId: string };
 
-- 
GitLab