@metamask/ramps-controller 20.1.0 → 20.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/CHANGELOG.md +21 -1
  2. package/dist/NeoBankService-method-action-types.cjs +7 -0
  3. package/dist/NeoBankService-method-action-types.cjs.map +1 -0
  4. package/dist/NeoBankService-method-action-types.d.cts +126 -0
  5. package/dist/NeoBankService-method-action-types.d.cts.map +1 -0
  6. package/dist/NeoBankService-method-action-types.d.mts +126 -0
  7. package/dist/NeoBankService-method-action-types.d.mts.map +1 -0
  8. package/dist/NeoBankService-method-action-types.mjs +6 -0
  9. package/dist/NeoBankService-method-action-types.mjs.map +1 -0
  10. package/dist/NeoBankService.cjs +387 -0
  11. package/dist/NeoBankService.cjs.map +1 -0
  12. package/dist/NeoBankService.d.cts +191 -0
  13. package/dist/NeoBankService.d.cts.map +1 -0
  14. package/dist/NeoBankService.d.mts +191 -0
  15. package/dist/NeoBankService.d.mts.map +1 -0
  16. package/dist/NeoBankService.mjs +379 -0
  17. package/dist/NeoBankService.mjs.map +1 -0
  18. package/dist/RampsController-method-action-types.cjs.map +1 -1
  19. package/dist/RampsController-method-action-types.d.cts +91 -1
  20. package/dist/RampsController-method-action-types.d.cts.map +1 -1
  21. package/dist/RampsController-method-action-types.d.mts +91 -1
  22. package/dist/RampsController-method-action-types.d.mts.map +1 -1
  23. package/dist/RampsController-method-action-types.mjs.map +1 -1
  24. package/dist/RampsController.cjs +372 -2
  25. package/dist/RampsController.cjs.map +1 -1
  26. package/dist/RampsController.d.cts +152 -5
  27. package/dist/RampsController.d.cts.map +1 -1
  28. package/dist/RampsController.d.mts +152 -5
  29. package/dist/RampsController.d.mts.map +1 -1
  30. package/dist/RampsController.mjs +371 -1
  31. package/dist/RampsController.mjs.map +1 -1
  32. package/dist/RampsService.cjs +14 -3
  33. package/dist/RampsService.cjs.map +1 -1
  34. package/dist/RampsService.d.cts +5 -1
  35. package/dist/RampsService.d.cts.map +1 -1
  36. package/dist/RampsService.d.mts +5 -1
  37. package/dist/RampsService.d.mts.map +1 -1
  38. package/dist/RampsService.mjs +14 -3
  39. package/dist/RampsService.mjs.map +1 -1
  40. package/dist/TransakService.cjs +15 -3
  41. package/dist/TransakService.cjs.map +1 -1
  42. package/dist/TransakService.d.cts +3 -1
  43. package/dist/TransakService.d.cts.map +1 -1
  44. package/dist/TransakService.d.mts +3 -1
  45. package/dist/TransakService.d.mts.map +1 -1
  46. package/dist/TransakService.mjs +15 -3
  47. package/dist/TransakService.mjs.map +1 -1
  48. package/dist/autoramp-types.cjs +3 -0
  49. package/dist/autoramp-types.cjs.map +1 -0
  50. package/dist/autoramp-types.d.cts +22 -0
  51. package/dist/autoramp-types.d.cts.map +1 -0
  52. package/dist/autoramp-types.d.mts +22 -0
  53. package/dist/autoramp-types.d.mts.map +1 -0
  54. package/dist/autoramp-types.mjs +2 -0
  55. package/dist/autoramp-types.mjs.map +1 -0
  56. package/dist/autorampAccount.cjs +155 -0
  57. package/dist/autorampAccount.cjs.map +1 -0
  58. package/dist/autorampAccount.d.cts +126 -0
  59. package/dist/autorampAccount.d.cts.map +1 -0
  60. package/dist/autorampAccount.d.mts +126 -0
  61. package/dist/autorampAccount.d.mts.map +1 -0
  62. package/dist/autorampAccount.mjs +147 -0
  63. package/dist/autorampAccount.mjs.map +1 -0
  64. package/dist/client-identity.cjs +28 -0
  65. package/dist/client-identity.cjs.map +1 -0
  66. package/dist/client-identity.d.cts +24 -0
  67. package/dist/client-identity.d.cts.map +1 -0
  68. package/dist/client-identity.d.mts +24 -0
  69. package/dist/client-identity.d.mts.map +1 -0
  70. package/dist/client-identity.mjs +24 -0
  71. package/dist/client-identity.mjs.map +1 -0
  72. package/dist/index.cjs +24 -1
  73. package/dist/index.cjs.map +1 -1
  74. package/dist/index.d.cts +14 -3
  75. package/dist/index.d.cts.map +1 -1
  76. package/dist/index.d.mts +14 -3
  77. package/dist/index.d.mts.map +1 -1
  78. package/dist/index.mjs +6 -1
  79. package/dist/index.mjs.map +1 -1
  80. package/dist/ownership-message.cjs +26 -0
  81. package/dist/ownership-message.cjs.map +1 -0
  82. package/dist/ownership-message.d.cts +22 -0
  83. package/dist/ownership-message.d.cts.map +1 -0
  84. package/dist/ownership-message.d.mts +22 -0
  85. package/dist/ownership-message.d.mts.map +1 -0
  86. package/dist/ownership-message.mjs +22 -0
  87. package/dist/ownership-message.mjs.map +1 -0
  88. package/dist/wallet-registration-machine.cjs +144 -0
  89. package/dist/wallet-registration-machine.cjs.map +1 -0
  90. package/dist/wallet-registration-machine.d.cts +81 -0
  91. package/dist/wallet-registration-machine.d.cts.map +1 -0
  92. package/dist/wallet-registration-machine.d.mts +81 -0
  93. package/dist/wallet-registration-machine.d.mts.map +1 -0
  94. package/dist/wallet-registration-machine.mjs +139 -0
  95. package/dist/wallet-registration-machine.mjs.map +1 -0
  96. package/dist/wallet-registration-service.cjs +351 -0
  97. package/dist/wallet-registration-service.cjs.map +1 -0
  98. package/dist/wallet-registration-service.d.cts +144 -0
  99. package/dist/wallet-registration-service.d.cts.map +1 -0
  100. package/dist/wallet-registration-service.d.mts +144 -0
  101. package/dist/wallet-registration-service.d.mts.map +1 -0
  102. package/dist/wallet-registration-service.mjs +344 -0
  103. package/dist/wallet-registration-service.mjs.map +1 -0
  104. package/package.json +3 -3
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Pure, hand-rolled finite state machine for the MoonPay Iron self-hosted
3
+ * wallet registration signing step. It follows the FSM convention used
4
+ * elsewhere in `core` (no XState dependency): a single pure `transition`
5
+ * reducer plus a data-driven transition table.
6
+ *
7
+ * Side effects (server lookups, signing, POSTing) live in the interpreter that
8
+ * drives this machine; every effect result is fed back in as an event, so the
9
+ * machine itself stays deterministic and trivially testable.
10
+ */
11
+ /** Every state in the signing step. */
12
+ export type WalletRegistrationStatus = 'idle' | 'preparing' | 'awaitingUnlock' | 'signing' | 'submitting' | 'disambiguate409' | 'checkThenRetry' | 'lookupUnavailable' | 'registered' | 'alreadyRegistered' | 'registeredDisabled' | 'failedRetryable' | 'failedTerminal' | 'cancelled';
13
+ /** Machine context carried across transitions. */
14
+ export type WalletRegistrationContext = {
15
+ /** Number of sign attempts made so far (used for the retry ceiling). */
16
+ attempts: number;
17
+ /** Maximum number of sign attempts before a retryable failure is surfaced. */
18
+ maxAttempts: number;
19
+ };
20
+ export type WalletRegistrationState = {
21
+ status: WalletRegistrationStatus;
22
+ context: WalletRegistrationContext;
23
+ };
24
+ /** Events the interpreter dispatches into the machine. */
25
+ export type WalletRegistrationEvent = {
26
+ type: 'START';
27
+ } | {
28
+ type: 'WALLET_LOCKED';
29
+ } | {
30
+ type: 'WALLET_UNLOCKED';
31
+ } | {
32
+ type: 'LOOKUP_ACTIVE';
33
+ } | {
34
+ type: 'LOOKUP_DISABLED';
35
+ } | {
36
+ type: 'LOOKUP_ABSENT';
37
+ } | {
38
+ type: 'LOOKUP_FAILED';
39
+ } | {
40
+ type: 'SIGN_OK';
41
+ } | {
42
+ type: 'SIGN_REJECTED';
43
+ } | {
44
+ type: 'SIGN_FAILED';
45
+ retryable: boolean;
46
+ } | {
47
+ type: 'SUBMIT_OK';
48
+ } | {
49
+ type: 'SUBMIT_CONFLICT';
50
+ } | {
51
+ type: 'SUBMIT_TRANSIENT';
52
+ } | {
53
+ type: 'SUBMIT_VALIDATION';
54
+ utcRollover: boolean;
55
+ } | {
56
+ type: 'SUBMIT_TERMINAL';
57
+ } | {
58
+ type: 'SUBMIT_RATE_LIMITED';
59
+ } | {
60
+ type: 'RETRY';
61
+ } | {
62
+ type: 'CANCEL';
63
+ };
64
+ /**
65
+ * Creates the initial idle state.
66
+ *
67
+ * @param maxAttempts - Optional retry ceiling for sign attempts.
68
+ * @returns A fresh idle machine state.
69
+ */
70
+ export declare function createInitialState(maxAttempts?: number): WalletRegistrationState;
71
+ /**
72
+ * Pure transition reducer. Unhandled (state, event) pairs are no-ops, which is
73
+ * how the machine enforces "one in-flight operation" (a second `START` while
74
+ * busy is ignored) and how terminal states stay put.
75
+ *
76
+ * @param state - Current machine state.
77
+ * @param event - Event to apply.
78
+ * @returns The next state (or the same state for unhandled events).
79
+ */
80
+ export declare function transition(state: WalletRegistrationState, event: WalletRegistrationEvent): WalletRegistrationState;
81
+ //# sourceMappingURL=wallet-registration-machine.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet-registration-machine.d.mts","sourceRoot":"","sources":["../src/wallet-registration-machine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,uCAAuC;AACvC,MAAM,MAAM,wBAAwB,GAChC,MAAM,GACN,WAAW,GACX,gBAAgB,GAChB,SAAS,GACT,YAAY,GACZ,iBAAiB,GACjB,gBAAgB,GAChB,mBAAmB,GACnB,YAAY,GACZ,mBAAmB,GACnB,oBAAoB,GACpB,iBAAiB,GACjB,gBAAgB,GAChB,WAAW,CAAC;AAEhB,kDAAkD;AAClD,MAAM,MAAM,yBAAyB,GAAG;IACtC,wEAAwE;IACxE,QAAQ,EAAE,MAAM,CAAC;IACjB,8EAA8E;IAC9E,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACpC,MAAM,EAAE,wBAAwB,CAAC;IACjC,OAAO,EAAE,yBAAyB,CAAC;CACpC,CAAC;AAEF,0DAA0D;AAC1D,MAAM,MAAM,uBAAuB,GAC/B;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GACjB;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,GACzB;IAAE,IAAI,EAAE,iBAAiB,CAAA;CAAE,GAC3B;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,GACzB;IAAE,IAAI,EAAE,iBAAiB,CAAA;CAAE,GAC3B;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,GACzB;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,GACzB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,GACzB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,GAC3C;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;IAAE,IAAI,EAAE,iBAAiB,CAAA;CAAE,GAC3B;IAAE,IAAI,EAAE,kBAAkB,CAAA;CAAE,GAC5B;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,GACnD;IAAE,IAAI,EAAE,iBAAiB,CAAA;CAAE,GAC3B;IAAE,IAAI,EAAE,qBAAqB,CAAA;CAAE,GAC/B;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GACjB;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC;AAWvB;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,WAAW,GAAE,MAA6B,GACzC,uBAAuB,CAEzB;AA4HD;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,uBAAuB,EAC9B,KAAK,EAAE,uBAAuB,GAC7B,uBAAuB,CAGzB"}
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Pure, hand-rolled finite state machine for the MoonPay Iron self-hosted
3
+ * wallet registration signing step. It follows the FSM convention used
4
+ * elsewhere in `core` (no XState dependency): a single pure `transition`
5
+ * reducer plus a data-driven transition table.
6
+ *
7
+ * Side effects (server lookups, signing, POSTing) live in the interpreter that
8
+ * drives this machine; every effect result is fed back in as an event, so the
9
+ * machine itself stays deterministic and trivially testable.
10
+ */
11
+ const DEFAULT_MAX_ATTEMPTS = 3;
12
+ /**
13
+ * Creates the initial idle state.
14
+ *
15
+ * @param maxAttempts - Optional retry ceiling for sign attempts.
16
+ * @returns A fresh idle machine state.
17
+ */
18
+ export function createInitialState(maxAttempts = DEFAULT_MAX_ATTEMPTS) {
19
+ return { status: 'idle', context: { attempts: 0, maxAttempts } };
20
+ }
21
+ /**
22
+ * Builds a handler that moves to a status while preserving context.
23
+ *
24
+ * @param status - Target status.
25
+ * @returns A handler transitioning to `status`.
26
+ */
27
+ function keep(status) {
28
+ return (state) => ({ status, context: state.context });
29
+ }
30
+ /**
31
+ * Builds a handler that moves to a status and resets the retry context. Used
32
+ * when the user (or app resume) starts a fresh attempt from scratch.
33
+ *
34
+ * @param status - Target status.
35
+ * @returns A handler transitioning to `status` with reset context.
36
+ */
37
+ function reset(status) {
38
+ return (state) => ({
39
+ status,
40
+ context: { ...state.context, attempts: 0 },
41
+ });
42
+ }
43
+ /**
44
+ * Moves to `signing` and counts this as a new sign attempt.
45
+ *
46
+ * @param state - Current state.
47
+ * @returns The `signing` state with an incremented attempt count.
48
+ */
49
+ const toSigning = (state) => ({
50
+ status: 'signing',
51
+ context: { ...state.context, attempts: state.context.attempts + 1 },
52
+ });
53
+ const toPreparing = reset('preparing');
54
+ const toAlreadyRegistered = keep('alreadyRegistered');
55
+ const toRegisteredDisabled = keep('registeredDisabled');
56
+ const toLookupUnavailable = keep('lookupUnavailable');
57
+ const toCancelled = keep('cancelled');
58
+ const signFailed = (state, event) => {
59
+ const { retryable } = event;
60
+ return retryable
61
+ ? keep('failedRetryable')(state, event)
62
+ : keep('failedTerminal')(state, event);
63
+ };
64
+ const submitValidation = (state, event) => {
65
+ const { utcRollover } = event;
66
+ return utcRollover && state.context.attempts < state.context.maxAttempts
67
+ ? toSigning(state, event)
68
+ : keep('failedTerminal')(state, event);
69
+ };
70
+ const checkThenRetryAbsent = (state, event) => state.context.attempts < state.context.maxAttempts
71
+ ? toSigning(state, event)
72
+ : keep('failedRetryable')(state, event);
73
+ const TABLE = {
74
+ idle: {
75
+ START: toPreparing,
76
+ },
77
+ preparing: {
78
+ LOOKUP_ACTIVE: toAlreadyRegistered,
79
+ LOOKUP_DISABLED: toRegisteredDisabled,
80
+ LOOKUP_ABSENT: toSigning,
81
+ LOOKUP_FAILED: toLookupUnavailable,
82
+ },
83
+ awaitingUnlock: {
84
+ WALLET_UNLOCKED: keep('signing'),
85
+ },
86
+ signing: {
87
+ SIGN_OK: keep('submitting'),
88
+ SIGN_REJECTED: toCancelled,
89
+ SIGN_FAILED: signFailed,
90
+ WALLET_LOCKED: keep('awaitingUnlock'),
91
+ CANCEL: toCancelled,
92
+ },
93
+ submitting: {
94
+ SUBMIT_OK: keep('registered'),
95
+ SUBMIT_CONFLICT: keep('disambiguate409'),
96
+ SUBMIT_TRANSIENT: keep('checkThenRetry'),
97
+ SUBMIT_VALIDATION: submitValidation,
98
+ SUBMIT_TERMINAL: keep('failedTerminal'),
99
+ SUBMIT_RATE_LIMITED: keep('failedRetryable'),
100
+ CANCEL: toCancelled,
101
+ },
102
+ disambiguate409: {
103
+ LOOKUP_ACTIVE: toAlreadyRegistered,
104
+ LOOKUP_DISABLED: toRegisteredDisabled,
105
+ LOOKUP_ABSENT: keep('failedRetryable'),
106
+ LOOKUP_FAILED: toLookupUnavailable,
107
+ CANCEL: toCancelled,
108
+ },
109
+ checkThenRetry: {
110
+ LOOKUP_ACTIVE: toAlreadyRegistered,
111
+ LOOKUP_DISABLED: toRegisteredDisabled,
112
+ LOOKUP_ABSENT: checkThenRetryAbsent,
113
+ LOOKUP_FAILED: toLookupUnavailable,
114
+ CANCEL: toCancelled,
115
+ },
116
+ failedRetryable: {
117
+ RETRY: toPreparing,
118
+ },
119
+ lookupUnavailable: {
120
+ RETRY: toPreparing,
121
+ },
122
+ cancelled: {
123
+ RETRY: toPreparing,
124
+ },
125
+ };
126
+ /**
127
+ * Pure transition reducer. Unhandled (state, event) pairs are no-ops, which is
128
+ * how the machine enforces "one in-flight operation" (a second `START` while
129
+ * busy is ignored) and how terminal states stay put.
130
+ *
131
+ * @param state - Current machine state.
132
+ * @param event - Event to apply.
133
+ * @returns The next state (or the same state for unhandled events).
134
+ */
135
+ export function transition(state, event) {
136
+ const handler = TABLE[state.status]?.[event.type];
137
+ return handler ? handler(state, event) : state;
138
+ }
139
+ //# sourceMappingURL=wallet-registration-machine.mjs.map
@@ -0,0 +1 @@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, hand-rolled finite state machine for the MoonPay Iron self-hosted\n * wallet registration signing step. It follows the FSM convention used\n * elsewhere in `core` (no XState dependency): a single pure `transition`\n * reducer plus a data-driven transition table.\n *\n * Side effects (server lookups, signing, POSTing) live in the interpreter that\n * drives this machine; every effect result is fed back in as an event, so the\n * machine itself stays deterministic and trivially testable.\n */\n\n/** Every state in the signing step. */\nexport type WalletRegistrationStatus =\n | 'idle'\n | 'preparing'\n | 'awaitingUnlock'\n | 'signing'\n | 'submitting'\n | 'disambiguate409'\n | 'checkThenRetry'\n | 'lookupUnavailable'\n | 'registered'\n | 'alreadyRegistered'\n | 'registeredDisabled'\n | 'failedRetryable'\n | 'failedTerminal'\n | 'cancelled';\n\n/** Machine context carried across transitions. */\nexport type WalletRegistrationContext = {\n /** Number of sign attempts made so far (used for the retry ceiling). */\n attempts: number;\n /** Maximum number of sign attempts before a retryable failure is surfaced. */\n maxAttempts: number;\n};\n\nexport type WalletRegistrationState = {\n status: WalletRegistrationStatus;\n context: WalletRegistrationContext;\n};\n\n/** Events the interpreter dispatches into the machine. */\nexport type WalletRegistrationEvent =\n | { type: 'START' }\n | { type: 'WALLET_LOCKED' }\n | { type: 'WALLET_UNLOCKED' }\n | { type: 'LOOKUP_ACTIVE' }\n | { type: 'LOOKUP_DISABLED' }\n | { type: 'LOOKUP_ABSENT' }\n | { type: 'LOOKUP_FAILED' }\n | { type: 'SIGN_OK' }\n | { type: 'SIGN_REJECTED' }\n | { type: 'SIGN_FAILED'; retryable: boolean }\n | { type: 'SUBMIT_OK' }\n | { type: 'SUBMIT_CONFLICT' }\n | { type: 'SUBMIT_TRANSIENT' }\n | { type: 'SUBMIT_VALIDATION'; utcRollover: boolean }\n | { type: 'SUBMIT_TERMINAL' }\n | { type: 'SUBMIT_RATE_LIMITED' }\n | { type: 'RETRY' }\n | { type: 'CANCEL' };\n\ntype EventType = WalletRegistrationEvent['type'];\n\ntype Handler = (\n state: WalletRegistrationState,\n event: WalletRegistrationEvent,\n) => WalletRegistrationState;\n\nconst DEFAULT_MAX_ATTEMPTS = 3;\n\n/**\n * Creates the initial idle state.\n *\n * @param maxAttempts - Optional retry ceiling for sign attempts.\n * @returns A fresh idle machine state.\n */\nexport function createInitialState(\n maxAttempts: number = DEFAULT_MAX_ATTEMPTS,\n): WalletRegistrationState {\n return { status: 'idle', context: { attempts: 0, maxAttempts } };\n}\n\n/**\n * Builds a handler that moves to a status while preserving context.\n *\n * @param status - Target status.\n * @returns A handler transitioning to `status`.\n */\nfunction keep(status: WalletRegistrationStatus): Handler {\n return (state) => ({ status, context: state.context });\n}\n\n/**\n * Builds a handler that moves to a status and resets the retry context. Used\n * when the user (or app resume) starts a fresh attempt from scratch.\n *\n * @param status - Target status.\n * @returns A handler transitioning to `status` with reset context.\n */\nfunction reset(status: WalletRegistrationStatus): Handler {\n return (state) => ({\n status,\n context: { ...state.context, attempts: 0 },\n });\n}\n\n/**\n * Moves to `signing` and counts this as a new sign attempt.\n *\n * @param state - Current state.\n * @returns The `signing` state with an incremented attempt count.\n */\nconst toSigning: Handler = (state) => ({\n status: 'signing',\n context: { ...state.context, attempts: state.context.attempts + 1 },\n});\n\nconst toPreparing = reset('preparing');\nconst toAlreadyRegistered = keep('alreadyRegistered');\nconst toRegisteredDisabled = keep('registeredDisabled');\nconst toLookupUnavailable = keep('lookupUnavailable');\nconst toCancelled = keep('cancelled');\n\nconst signFailed: Handler = (state, event) => {\n const { retryable } = event as Extract<\n WalletRegistrationEvent,\n { type: 'SIGN_FAILED' }\n >;\n return retryable\n ? keep('failedRetryable')(state, event)\n : keep('failedTerminal')(state, event);\n};\n\nconst submitValidation: Handler = (state, event) => {\n const { utcRollover } = event as Extract<\n WalletRegistrationEvent,\n { type: 'SUBMIT_VALIDATION' }\n >;\n return utcRollover && state.context.attempts < state.context.maxAttempts\n ? toSigning(state, event)\n : keep('failedTerminal')(state, event);\n};\n\nconst checkThenRetryAbsent: Handler = (state, event) =>\n state.context.attempts < state.context.maxAttempts\n ? toSigning(state, event)\n : keep('failedRetryable')(state, event);\n\nconst TABLE: Partial<\n Record<WalletRegistrationStatus, Partial<Record<EventType, Handler>>>\n> = {\n idle: {\n START: toPreparing,\n },\n preparing: {\n LOOKUP_ACTIVE: toAlreadyRegistered,\n LOOKUP_DISABLED: toRegisteredDisabled,\n LOOKUP_ABSENT: toSigning,\n LOOKUP_FAILED: toLookupUnavailable,\n },\n awaitingUnlock: {\n WALLET_UNLOCKED: keep('signing'),\n },\n signing: {\n SIGN_OK: keep('submitting'),\n SIGN_REJECTED: toCancelled,\n SIGN_FAILED: signFailed,\n WALLET_LOCKED: keep('awaitingUnlock'),\n CANCEL: toCancelled,\n },\n submitting: {\n SUBMIT_OK: keep('registered'),\n SUBMIT_CONFLICT: keep('disambiguate409'),\n SUBMIT_TRANSIENT: keep('checkThenRetry'),\n SUBMIT_VALIDATION: submitValidation,\n SUBMIT_TERMINAL: keep('failedTerminal'),\n SUBMIT_RATE_LIMITED: keep('failedRetryable'),\n CANCEL: toCancelled,\n },\n disambiguate409: {\n LOOKUP_ACTIVE: toAlreadyRegistered,\n LOOKUP_DISABLED: toRegisteredDisabled,\n LOOKUP_ABSENT: keep('failedRetryable'),\n LOOKUP_FAILED: toLookupUnavailable,\n CANCEL: toCancelled,\n },\n checkThenRetry: {\n LOOKUP_ACTIVE: toAlreadyRegistered,\n LOOKUP_DISABLED: toRegisteredDisabled,\n LOOKUP_ABSENT: checkThenRetryAbsent,\n LOOKUP_FAILED: toLookupUnavailable,\n CANCEL: toCancelled,\n },\n failedRetryable: {\n RETRY: toPreparing,\n },\n lookupUnavailable: {\n RETRY: toPreparing,\n },\n cancelled: {\n RETRY: toPreparing,\n },\n};\n\n/**\n * Pure transition reducer. Unhandled (state, event) pairs are no-ops, which is\n * how the machine enforces \"one in-flight operation\" (a second `START` while\n * busy is ignored) and how terminal states stay put.\n *\n * @param state - Current machine state.\n * @param event - Event to apply.\n * @returns The next state (or the same state for unhandled events).\n */\nexport function transition(\n state: WalletRegistrationState,\n event: WalletRegistrationEvent,\n): WalletRegistrationState {\n const handler = TABLE[state.status]?.[event.type];\n return handler ? handler(state, event) : state;\n}\n"]}
@@ -0,0 +1,351 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _WalletRegistrationService_instances, _WalletRegistrationService_fetch, _WalletRegistrationService_baseUrl, _WalletRegistrationService_getAuthToken, _WalletRegistrationService_getExternalId, _WalletRegistrationService_toHttpError, _WalletRegistrationService_toRegistration;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.WalletRegistrationService = exports.extractErrorBody = exports.createIdempotencyKey = exports.WalletRegistrationError = void 0;
16
+ /** Typed error carrying enough context for state transitions. */
17
+ class WalletRegistrationError extends Error {
18
+ constructor(kind, options) {
19
+ super(options.message ?? `wallet registration failed: ${kind}`);
20
+ this.name = 'WalletRegistrationError';
21
+ this.kind = kind;
22
+ this.httpStatus = options.httpStatus;
23
+ this.body = options.body;
24
+ }
25
+ }
26
+ exports.WalletRegistrationError = WalletRegistrationError;
27
+ /**
28
+ * Normalizes a Monad EVM address for case-insensitive comparison.
29
+ *
30
+ * @param address - Raw address string.
31
+ * @returns The comparison key for the address.
32
+ */
33
+ function normalizeAddress(address) {
34
+ return address.toLowerCase();
35
+ }
36
+ /**
37
+ * Maps an HTTP status to the typed error kind the state machine reacts to.
38
+ *
39
+ * @param status - HTTP status code from the proxy/Iron response.
40
+ * @returns The corresponding error kind.
41
+ */
42
+ function mapStatusToKind(status) {
43
+ switch (status) {
44
+ case 400:
45
+ return 'validation';
46
+ case 401:
47
+ return 'unauthorized';
48
+ case 403:
49
+ return 'forbidden';
50
+ case 404:
51
+ return 'notFound';
52
+ case 409:
53
+ return 'conflict';
54
+ case 429:
55
+ return 'rateLimited';
56
+ default:
57
+ return status >= 500 ? 'transient' : 'validation';
58
+ }
59
+ }
60
+ /**
61
+ * Builds a client-side Idempotency-Key for MoonPay POSTs. Prefer a stable
62
+ * caller-supplied key across retries of the same proof.
63
+ *
64
+ * @returns A random UUID when available, otherwise a timestamped fallback.
65
+ */
66
+ function createIdempotencyKey() {
67
+ const cryptoObj = globalThis.crypto;
68
+ if (typeof cryptoObj?.randomUUID === 'function') {
69
+ return cryptoObj.randomUUID();
70
+ }
71
+ return `wallet-reg-${Date.now()}-${Math.random().toString(16).slice(2)}`;
72
+ }
73
+ exports.createIdempotencyKey = createIdempotencyKey;
74
+ /**
75
+ * Extracts a human-readable error body from a transparent neobank-proxy
76
+ * response. Upstream may return a plain string or a JSON value; both are
77
+ * mirrored 1:1 (no `{ code: 'iron_error' }` envelope).
78
+ *
79
+ * @param raw - Raw response text.
80
+ * @returns Normalized body string for {@link WalletRegistrationError}.
81
+ */
82
+ function extractErrorBody(raw) {
83
+ const trimmed = raw.trim();
84
+ if (!trimmed) {
85
+ return raw;
86
+ }
87
+ try {
88
+ const parsed = JSON.parse(trimmed);
89
+ if (typeof parsed === 'string') {
90
+ return parsed;
91
+ }
92
+ if (parsed && typeof parsed === 'object') {
93
+ const { message } = parsed;
94
+ if (typeof message === 'string') {
95
+ return message;
96
+ }
97
+ }
98
+ return trimmed;
99
+ }
100
+ catch {
101
+ return trimmed;
102
+ }
103
+ }
104
+ exports.extractErrorBody = extractErrorBody;
105
+ /**
106
+ * Data service that talks to the Money Movement neobank-proxy for MoonPay Iron
107
+ * self-hosted wallet registration. It never calls Iron directly, so the Iron
108
+ * API key never ships in the client.
109
+ */
110
+ class WalletRegistrationService {
111
+ constructor(options) {
112
+ _WalletRegistrationService_instances.add(this);
113
+ _WalletRegistrationService_fetch.set(this, void 0);
114
+ _WalletRegistrationService_baseUrl.set(this, void 0);
115
+ _WalletRegistrationService_getAuthToken.set(this, void 0);
116
+ _WalletRegistrationService_getExternalId.set(this, void 0);
117
+ __classPrivateFieldSet(this, _WalletRegistrationService_fetch, options.fetch, "f");
118
+ __classPrivateFieldSet(this, _WalletRegistrationService_baseUrl, options.baseUrl.replace(/\/$/u, ''), "f");
119
+ __classPrivateFieldSet(this, _WalletRegistrationService_getAuthToken, options.getAuthToken, "f");
120
+ __classPrivateFieldSet(this, _WalletRegistrationService_getExternalId, options.getExternalId, "f");
121
+ }
122
+ /**
123
+ * Resolves Iron's internal customer id via
124
+ * `GET /neobank/customers/{external_id}/external`, using the MetaMask
125
+ * profile/canonical id as `external_id`.
126
+ *
127
+ * @returns Iron's internal customer id.
128
+ */
129
+ async getMoonpayCustomerId() {
130
+ const [token, externalId] = await Promise.all([
131
+ __classPrivateFieldGet(this, _WalletRegistrationService_getAuthToken, "f").call(this),
132
+ __classPrivateFieldGet(this, _WalletRegistrationService_getExternalId, "f").call(this),
133
+ ]);
134
+ if (!externalId) {
135
+ throw new WalletRegistrationError('malformedResponse', {
136
+ message: 'MetaMask external id (canonical profile id) is empty',
137
+ });
138
+ }
139
+ const response = await __classPrivateFieldGet(this, _WalletRegistrationService_fetch, "f").call(this, `${__classPrivateFieldGet(this, _WalletRegistrationService_baseUrl, "f")}/neobank/customers/${encodeURIComponent(externalId)}/external`, {
140
+ method: 'GET',
141
+ headers: {
142
+ accept: 'application/json',
143
+ authorization: `Bearer ${token}`,
144
+ },
145
+ });
146
+ if (!response.ok) {
147
+ throw await __classPrivateFieldGet(this, _WalletRegistrationService_instances, "m", _WalletRegistrationService_toHttpError).call(this, response);
148
+ }
149
+ let payload;
150
+ try {
151
+ payload = await response.json();
152
+ }
153
+ catch {
154
+ throw new WalletRegistrationError('malformedResponse', {
155
+ message: 'MoonPay customer body was not valid JSON',
156
+ });
157
+ }
158
+ const { id } = payload;
159
+ if (typeof id !== 'string' || id.length === 0) {
160
+ throw new WalletRegistrationError('malformedResponse', {
161
+ message: 'MoonPay customer body missing id',
162
+ });
163
+ }
164
+ return id;
165
+ }
166
+ /**
167
+ * Reconciles a wallet against the customer's registered self-hosted addresses
168
+ * via `GET /neobank/addresses/crypto/{customer_id}?filter=SelfHosted`.
169
+ * Upstream returns all self-hosted chains; Monad filtering stays client-side
170
+ * for the POC. Transport failures are reported as `lookupUnavailable`;
171
+ * malformed successful responses are reported as `malformedResponse`.
172
+ * Neither is downgraded to `absent`.
173
+ *
174
+ * @param request - Customer id and Monad address to reconcile.
175
+ * @returns The active / disabled / absent status for the address.
176
+ */
177
+ async getRegistrationStatus(request) {
178
+ const { customerId, address, blockchain } = request;
179
+ let response;
180
+ try {
181
+ const token = await __classPrivateFieldGet(this, _WalletRegistrationService_getAuthToken, "f").call(this);
182
+ const url = new URL(`${__classPrivateFieldGet(this, _WalletRegistrationService_baseUrl, "f")}/neobank/addresses/crypto/${encodeURIComponent(customerId)}`);
183
+ url.searchParams.set('filter', 'SelfHosted');
184
+ response = await __classPrivateFieldGet(this, _WalletRegistrationService_fetch, "f").call(this, url.toString(), {
185
+ method: 'GET',
186
+ headers: {
187
+ accept: 'application/json',
188
+ authorization: `Bearer ${token}`,
189
+ },
190
+ });
191
+ }
192
+ catch (error) {
193
+ throw new WalletRegistrationError('lookupUnavailable', {
194
+ message: 'self-hosted address lookup failed',
195
+ body: error instanceof Error ? error.message : undefined,
196
+ });
197
+ }
198
+ if (!response.ok) {
199
+ let body;
200
+ try {
201
+ body = extractErrorBody(await response.text());
202
+ }
203
+ catch (error) {
204
+ body = error instanceof Error ? error.message : '';
205
+ }
206
+ throw new WalletRegistrationError('lookupUnavailable', {
207
+ httpStatus: response.status,
208
+ body,
209
+ });
210
+ }
211
+ let payload;
212
+ try {
213
+ payload = await response.json();
214
+ }
215
+ catch {
216
+ throw new WalletRegistrationError('malformedResponse', {
217
+ message: 'self-hosted address list body was not valid JSON',
218
+ });
219
+ }
220
+ if (!Array.isArray(payload)) {
221
+ throw new WalletRegistrationError('malformedResponse', {
222
+ message: 'expected an array of registered addresses',
223
+ });
224
+ }
225
+ const target = normalizeAddress(address);
226
+ const match = payload.find((entry) => {
227
+ const record = entry;
228
+ const walletAddress = record.wallet_address;
229
+ if (typeof walletAddress !== 'string') {
230
+ return false;
231
+ }
232
+ return (normalizeAddress(walletAddress) === target &&
233
+ record.blockchain === blockchain);
234
+ });
235
+ if (!match) {
236
+ return { type: 'absent' };
237
+ }
238
+ const registration = __classPrivateFieldGet(this, _WalletRegistrationService_instances, "m", _WalletRegistrationService_toRegistration).call(this, match);
239
+ return registration.disabled
240
+ ? { type: 'disabled', registration }
241
+ : { type: 'active', registration };
242
+ }
243
+ /**
244
+ * Registers a self-hosted wallet through neobank-proxy
245
+ * `POST /neobank/addresses/crypto/selfhosted`. The client supplies
246
+ * `customer_id` and an `Idempotency-Key` (generated when omitted). Every
247
+ * non-2xx response is mapped to a typed error; `409` is deliberately
248
+ * surfaced as an ambiguous `conflict` that the caller must reconcile with a
249
+ * follow-up status lookup.
250
+ *
251
+ * @param request - Customer id, address, blockchain, message, and signature.
252
+ * @returns The registered outcome on success.
253
+ */
254
+ async registerSelfHostedWallet(request) {
255
+ const idempotencyKey = request.idempotencyKey ?? createIdempotencyKey();
256
+ let response;
257
+ try {
258
+ const token = await __classPrivateFieldGet(this, _WalletRegistrationService_getAuthToken, "f").call(this);
259
+ response = await __classPrivateFieldGet(this, _WalletRegistrationService_fetch, "f").call(this, `${__classPrivateFieldGet(this, _WalletRegistrationService_baseUrl, "f")}/neobank/addresses/crypto/selfhosted`, {
260
+ method: 'POST',
261
+ headers: {
262
+ accept: 'application/json',
263
+ 'content-type': 'application/json',
264
+ authorization: `Bearer ${token}`,
265
+ 'Idempotency-Key': idempotencyKey,
266
+ },
267
+ body: JSON.stringify({
268
+ customer_id: request.customerId,
269
+ address: request.address,
270
+ blockchain: request.blockchain,
271
+ message: request.message,
272
+ signature: request.signature,
273
+ }),
274
+ });
275
+ }
276
+ catch (error) {
277
+ throw new WalletRegistrationError('transient', {
278
+ message: 'self-hosted registration request failed',
279
+ body: error instanceof Error ? error.message : undefined,
280
+ });
281
+ }
282
+ if (!response.ok) {
283
+ throw await __classPrivateFieldGet(this, _WalletRegistrationService_instances, "m", _WalletRegistrationService_toHttpError).call(this, response);
284
+ }
285
+ let payload;
286
+ try {
287
+ payload = await response.json();
288
+ }
289
+ catch {
290
+ throw new WalletRegistrationError('malformedResponse', {
291
+ message: 'registration success body was not valid JSON',
292
+ });
293
+ }
294
+ const record = payload;
295
+ if (typeof record.id !== 'string' || typeof record.address !== 'string') {
296
+ throw new WalletRegistrationError('malformedResponse', {
297
+ message: 'registration success body missing id/address',
298
+ });
299
+ }
300
+ return {
301
+ type: 'registered',
302
+ registration: {
303
+ id: record.id,
304
+ address: record.address,
305
+ blockchain: request.blockchain,
306
+ disabled: Boolean(record.disabled),
307
+ isSelf: true,
308
+ },
309
+ };
310
+ }
311
+ }
312
+ exports.WalletRegistrationService = WalletRegistrationService;
313
+ _WalletRegistrationService_fetch = new WeakMap(), _WalletRegistrationService_baseUrl = new WeakMap(), _WalletRegistrationService_getAuthToken = new WeakMap(), _WalletRegistrationService_getExternalId = new WeakMap(), _WalletRegistrationService_instances = new WeakSet(), _WalletRegistrationService_toHttpError = async function _WalletRegistrationService_toHttpError(response) {
314
+ let raw = '';
315
+ try {
316
+ raw = await response.text();
317
+ }
318
+ catch {
319
+ return new WalletRegistrationError('malformedResponse', {
320
+ httpStatus: response.status,
321
+ message: 'error body could not be read',
322
+ });
323
+ }
324
+ const { status } = response;
325
+ const kind = mapStatusToKind(status);
326
+ const body = extractErrorBody(raw);
327
+ return new WalletRegistrationError(kind, {
328
+ httpStatus: status,
329
+ body,
330
+ message: body || undefined,
331
+ });
332
+ }, _WalletRegistrationService_toRegistration = function _WalletRegistrationService_toRegistration(record) {
333
+ if (typeof record.id !== 'string' || record.id.length === 0) {
334
+ throw new WalletRegistrationError('malformedResponse', {
335
+ message: 'registered address missing id',
336
+ });
337
+ }
338
+ if (typeof record.wallet_address !== 'string') {
339
+ throw new WalletRegistrationError('malformedResponse', {
340
+ message: 'registered address missing wallet_address',
341
+ });
342
+ }
343
+ return {
344
+ id: record.id,
345
+ address: record.wallet_address,
346
+ blockchain: 'Monad',
347
+ disabled: Boolean(record.disabled),
348
+ isSelf: Boolean(record.is_self),
349
+ };
350
+ };
351
+ //# sourceMappingURL=wallet-registration-service.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet-registration-service.cjs","sourceRoot":"","sources":["../src/wallet-registration-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAmDA,iEAAiE;AACjE,MAAa,uBAAwB,SAAQ,KAAK;IAOhD,YACE,IAAiC,EACjC,OAIC;QAED,KAAK,CAAC,OAAO,CAAC,OAAO,IAAI,+BAA+B,IAAI,EAAE,CAAC,CAAC;QAChE,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACrC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAC3B,CAAC;CACF;AArBD,0DAqBC;AA0CD;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAe;IACvC,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CAAC,MAAc;IACrC,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,GAAG;YACN,OAAO,YA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9B,UAAU,EAAE,OAAO;QACnB,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC;QAClC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC;KAChC,CAAC;AACJ,CAAC","sourcesContent":["/** The only blockchain supported by the Money Account POC. */\nexport type Blockchain = 'Monad';\n\n/** Normalized view of a single registered self-hosted address. */\nexport type SelfHostedRegistration = {\n id: string;\n address: string;\n blockchain: Blockchain;\n disabled: boolean;\n isSelf: boolean;\n};\n\n/** Result of reconciling a wallet against the customer's registered addresses. */\nexport type RegistrationStatus =\n | { type: 'active'; registration: SelfHostedRegistration }\n | { type: 'disabled'; registration: SelfHostedRegistration }\n | { type: 'absent' };\n\n/**\n * Discriminated error kinds surfaced to the state machine. Every non-success\n * path maps to exactly one of these so the machine can decide deterministically.\n */\nexport type WalletRegistrationErrorKind =\n | 'validation'\n | 'unauthorized'\n | 'forbidden'\n | 'notFound'\n | 'conflict'\n | 'rateLimited'\n | 'transient'\n | 'lookupUnavailable'\n | 'malformedResponse';\n\n/** Minimal HTTP response shape, so the service is environment-agnostic. */\ntype HttpResponse = {\n ok: boolean;\n status: number;\n json: () => Promise<unknown>;\n text: () => Promise<string>;\n};\n\n/** Minimal `fetch`-like function the service depends on. */\ntype FetchLike = (\n url: string,\n init?: {\n method?: string;\n headers?: Record<string, string>;\n body?: string;\n },\n) => Promise<HttpResponse>;\n\n/** Typed error carrying enough context for state transitions. */\nexport class WalletRegistrationError extends Error {\n readonly kind: WalletRegistrationErrorKind;\n\n readonly httpStatus?: number;\n\n readonly body?: string;\n\n constructor(\n kind: WalletRegistrationErrorKind,\n options: {\n message?: string;\n httpStatus?: number;\n body?: string;\n },\n ) {\n super(options.message ?? `wallet registration failed: ${kind}`);\n this.name = 'WalletRegistrationError';\n this.kind = kind;\n this.httpStatus = options.httpStatus;\n this.body = options.body;\n }\n}\n\nexport type WalletRegistrationServiceOptions = {\n fetch: FetchLike;\n /**\n * Base URL of the Money Movement neobank-proxy host\n * (e.g. `https://on-ramp.dev-api.cx.metamask.io`). Paths are under `/neobank`.\n */\n baseUrl: string;\n getAuthToken: () => Promise<string>;\n /**\n * MetaMask profile / partner external id used as MoonPay `external_id`\n * (typically `AuthenticationController:getSessionProfile().canonicalProfileId`).\n */\n getExternalId: () => Promise<string>;\n};\n\nexport type GetRegistrationStatusRequest = {\n customerId: string;\n address: string;\n blockchain: Blockchain;\n};\n\nexport type RegisterSelfHostedWalletRequest = {\n customerId: string;\n address: string;\n blockchain: Blockchain;\n message: string;\n signature: string;\n /**\n * Stable key reused across retries of the same ownership proof. Generated\n * when omitted.\n */\n idempotencyKey?: string;\n};\n\n/** Successful registration outcome. */\nexport type RegistrationOutcome = {\n type: 'registered';\n registration: SelfHostedRegistration;\n};\n\n/**\n * Normalizes a Monad EVM address for case-insensitive comparison.\n *\n * @param address - Raw address string.\n * @returns The comparison key for the address.\n */\nfunction normalizeAddress(address: string): string {\n return address.toLowerCase();\n}\n\n/**\n * Maps an HTTP status to the typed error kind the state machine reacts to.\n *\n * @param status - HTTP status code from the proxy/Iron response.\n * @returns The corresponding error kind.\n */\nfunction mapStatusToKind(status: number): WalletRegistrationErrorKind {\n switch (status) {\n case 400:\n return 'validation';\n case 401:\n return 'unauthorized';\n case 403:\n return 'forbidden';\n case 404:\n return 'notFound';\n case 409:\n return 'conflict';\n case 429:\n return 'rateLimited';\n default:\n return status >= 500 ? 'transient' : 'validation';\n }\n}\n\n/**\n * Builds a client-side Idempotency-Key for MoonPay POSTs. Prefer a stable\n * caller-supplied key across retries of the same proof.\n *\n * @returns A random UUID when available, otherwise a timestamped fallback.\n */\nexport function createIdempotencyKey(): string {\n const cryptoObj = globalThis.crypto as\n | { randomUUID?: () => string }\n | undefined;\n if (typeof cryptoObj?.randomUUID === 'function') {\n return cryptoObj.randomUUID();\n }\n return `wallet-reg-${Date.now()}-${Math.random().toString(16).slice(2)}`;\n}\n\n/**\n * Extracts a human-readable error body from a transparent neobank-proxy\n * response. Upstream may return a plain string or a JSON value; both are\n * mirrored 1:1 (no `{ code: 'iron_error' }` envelope).\n *\n * @param raw - Raw response text.\n * @returns Normalized body string for {@link WalletRegistrationError}.\n */\nexport function extractErrorBody(raw: string): string {\n const trimmed = raw.trim();\n if (!trimmed) {\n return raw;\n }\n try {\n const parsed: unknown = JSON.parse(trimmed);\n if (typeof parsed === 'string') {\n return parsed;\n }\n if (parsed && typeof parsed === 'object') {\n const { message } = parsed as { message?: unknown };\n if (typeof message === 'string') {\n return message;\n }\n }\n return trimmed;\n } catch {\n return trimmed;\n }\n}\n\n/**\n * Data service that talks to the Money Movement neobank-proxy for MoonPay Iron\n * self-hosted wallet registration. It never calls Iron directly, so the Iron\n * API key never ships in the client.\n */\nexport class WalletRegistrationService {\n readonly #fetch: FetchLike;\n\n readonly #baseUrl: string;\n\n readonly #getAuthToken: () => Promise<string>;\n\n readonly #getExternalId: () => Promise<string>;\n\n constructor(options: WalletRegistrationServiceOptions) {\n this.#fetch = options.fetch;\n this.#baseUrl = options.baseUrl.replace(/\\/$/u, '');\n this.#getAuthToken = options.getAuthToken;\n this.#getExternalId = options.getExternalId;\n }\n\n /**\n * Resolves Iron's internal customer id via\n * `GET /neobank/customers/{external_id}/external`, using the MetaMask\n * profile/canonical id as `external_id`.\n *\n * @returns Iron's internal customer id.\n */\n async getMoonpayCustomerId(): Promise<string> {\n const [token, externalId] = await Promise.all([\n this.#getAuthToken(),\n this.#getExternalId(),\n ]);\n if (!externalId) {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'MetaMask external id (canonical profile id) is empty',\n });\n }\n\n const response = await this.#fetch(\n `${this.#baseUrl}/neobank/customers/${encodeURIComponent(externalId)}/external`,\n {\n method: 'GET',\n headers: {\n accept: 'application/json',\n authorization: `Bearer ${token}`,\n },\n },\n );\n\n if (!response.ok) {\n throw await this.#toHttpError(response);\n }\n\n let payload: unknown;\n try {\n payload = await response.json();\n } catch {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'MoonPay customer body was not valid JSON',\n });\n }\n\n const { id } = payload as { id?: unknown };\n if (typeof id !== 'string' || id.length === 0) {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'MoonPay customer body missing id',\n });\n }\n return id;\n }\n\n /**\n * Reconciles a wallet against the customer's registered self-hosted addresses\n * via `GET /neobank/addresses/crypto/{customer_id}?filter=SelfHosted`.\n * Upstream returns all self-hosted chains; Monad filtering stays client-side\n * for the POC. Transport failures are reported as `lookupUnavailable`;\n * malformed successful responses are reported as `malformedResponse`.\n * Neither is downgraded to `absent`.\n *\n * @param request - Customer id and Monad address to reconcile.\n * @returns The active / disabled / absent status for the address.\n */\n async getRegistrationStatus(\n request: GetRegistrationStatusRequest,\n ): Promise<RegistrationStatus> {\n const { customerId, address, blockchain } = request;\n\n let response: HttpResponse;\n try {\n const token = await this.#getAuthToken();\n const url = new URL(\n `${this.#baseUrl}/neobank/addresses/crypto/${encodeURIComponent(customerId)}`,\n );\n url.searchParams.set('filter', 'SelfHosted');\n response = await this.#fetch(url.toString(), {\n method: 'GET',\n headers: {\n accept: 'application/json',\n authorization: `Bearer ${token}`,\n },\n });\n } catch (error) {\n throw new WalletRegistrationError('lookupUnavailable', {\n message: 'self-hosted address lookup failed',\n body: error instanceof Error ? error.message : undefined,\n });\n }\n\n if (!response.ok) {\n let body: string;\n try {\n body = extractErrorBody(await response.text());\n } catch (error) {\n body = error instanceof Error ? error.message : '';\n }\n throw new WalletRegistrationError('lookupUnavailable', {\n httpStatus: response.status,\n body,\n });\n }\n\n let payload: unknown;\n try {\n payload = await response.json();\n } catch {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'self-hosted address list body was not valid JSON',\n });\n }\n if (!Array.isArray(payload)) {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'expected an array of registered addresses',\n });\n }\n\n const target = normalizeAddress(address);\n const match = payload.find((entry) => {\n const record = entry as Record<string, unknown>;\n const walletAddress = record.wallet_address;\n if (typeof walletAddress !== 'string') {\n return false;\n }\n return (\n normalizeAddress(walletAddress) === target &&\n record.blockchain === blockchain\n );\n }) as Record<string, unknown> | undefined;\n\n if (!match) {\n return { type: 'absent' };\n }\n\n const registration = this.#toRegistration(match);\n return registration.disabled\n ? { type: 'disabled', registration }\n : { type: 'active', registration };\n }\n\n /**\n * Registers a self-hosted wallet through neobank-proxy\n * `POST /neobank/addresses/crypto/selfhosted`. The client supplies\n * `customer_id` and an `Idempotency-Key` (generated when omitted). Every\n * non-2xx response is mapped to a typed error; `409` is deliberately\n * surfaced as an ambiguous `conflict` that the caller must reconcile with a\n * follow-up status lookup.\n *\n * @param request - Customer id, address, blockchain, message, and signature.\n * @returns The registered outcome on success.\n */\n async registerSelfHostedWallet(\n request: RegisterSelfHostedWalletRequest,\n ): Promise<RegistrationOutcome> {\n const idempotencyKey = request.idempotencyKey ?? createIdempotencyKey();\n let response: HttpResponse;\n try {\n const token = await this.#getAuthToken();\n response = await this.#fetch(\n `${this.#baseUrl}/neobank/addresses/crypto/selfhosted`,\n {\n method: 'POST',\n headers: {\n accept: 'application/json',\n 'content-type': 'application/json',\n authorization: `Bearer ${token}`,\n 'Idempotency-Key': idempotencyKey,\n },\n body: JSON.stringify({\n customer_id: request.customerId,\n address: request.address,\n blockchain: request.blockchain,\n message: request.message,\n signature: request.signature,\n }),\n },\n );\n } catch (error) {\n throw new WalletRegistrationError('transient', {\n message: 'self-hosted registration request failed',\n body: error instanceof Error ? error.message : undefined,\n });\n }\n\n if (!response.ok) {\n throw await this.#toHttpError(response);\n }\n\n let payload: unknown;\n try {\n payload = await response.json();\n } catch {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'registration success body was not valid JSON',\n });\n }\n\n const record = payload as Record<string, unknown>;\n if (typeof record.id !== 'string' || typeof record.address !== 'string') {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'registration success body missing id/address',\n });\n }\n\n return {\n type: 'registered',\n registration: {\n id: record.id,\n address: record.address,\n blockchain: request.blockchain,\n disabled: Boolean(record.disabled),\n isSelf: true,\n },\n };\n }\n\n async #toHttpError(response: HttpResponse): Promise<WalletRegistrationError> {\n let raw = '';\n try {\n raw = await response.text();\n } catch {\n return new WalletRegistrationError('malformedResponse', {\n httpStatus: response.status,\n message: 'error body could not be read',\n });\n }\n\n const { status } = response;\n const kind = mapStatusToKind(status);\n const body = extractErrorBody(raw);\n return new WalletRegistrationError(kind, {\n httpStatus: status,\n body,\n message: body || undefined,\n });\n }\n\n #toRegistration(record: Record<string, unknown>): SelfHostedRegistration {\n if (typeof record.id !== 'string' || record.id.length === 0) {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'registered address missing id',\n });\n }\n if (typeof record.wallet_address !== 'string') {\n throw new WalletRegistrationError('malformedResponse', {\n message: 'registered address missing wallet_address',\n });\n }\n return {\n id: record.id,\n address: record.wallet_address,\n blockchain: 'Monad',\n disabled: Boolean(record.disabled),\n isSelf: Boolean(record.is_self),\n };\n }\n}\n"]}