@metamask/claims-controller 0.3.0 → 0.4.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.
- package/CHANGELOG.md +20 -1
- package/dist/ClaimsController.cjs +8 -2
- package/dist/ClaimsController.cjs.map +1 -1
- package/dist/ClaimsController.d.cts.map +1 -1
- package/dist/ClaimsController.d.mts.map +1 -1
- package/dist/ClaimsController.mjs +8 -2
- package/dist/ClaimsController.mjs.map +1 -1
- package/dist/ClaimsService.cjs +73 -40
- package/dist/ClaimsService.cjs.map +1 -1
- package/dist/ClaimsService.d.cts.map +1 -1
- package/dist/ClaimsService.d.mts.map +1 -1
- package/dist/ClaimsService.mjs +73 -40
- package/dist/ClaimsService.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +1 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +1 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils.cjs +49 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +16 -0
- package/dist/utils.d.cts.map +1 -0
- package/dist/utils.d.mts +16 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +44 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.4.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Capture claims error and report to sentry using `Messenger.captureException` method from `@metamask/messenger`. ([#7553](https://github.com/MetaMask/core/pull/7553))
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
|
|
19
|
+
- Bump `@metamask/controller-utils` from `^11.16.0` to `^11.17.0` ([#7534](https://github.com/MetaMask/core/pull/7534))
|
|
20
|
+
|
|
21
|
+
## [0.3.1]
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- Added `updatedAt` field to the claims draft. ([#7523](https://github.com/MetaMask/core/pull/7523))
|
|
26
|
+
|
|
10
27
|
## [0.3.0]
|
|
11
28
|
|
|
12
29
|
### Added
|
|
@@ -42,7 +59,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
42
59
|
- `generateMessageForClaimSignature`: generate message to sign for the claim signature.
|
|
43
60
|
- `verifyClaimSignature`: verify claim signature produced by user.
|
|
44
61
|
|
|
45
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/claims-controller@0.
|
|
62
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/claims-controller@0.4.0...HEAD
|
|
63
|
+
[0.4.0]: https://github.com/MetaMask/core/compare/@metamask/claims-controller@0.3.1...@metamask/claims-controller@0.4.0
|
|
64
|
+
[0.3.1]: https://github.com/MetaMask/core/compare/@metamask/claims-controller@0.3.0...@metamask/claims-controller@0.3.1
|
|
46
65
|
[0.3.0]: https://github.com/MetaMask/core/compare/@metamask/claims-controller@0.2.0...@metamask/claims-controller@0.3.0
|
|
47
66
|
[0.2.0]: https://github.com/MetaMask/core/compare/@metamask/claims-controller@0.1.0...@metamask/claims-controller@0.2.0
|
|
48
67
|
[0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/claims-controller@0.1.0
|
|
@@ -139,18 +139,24 @@ class ClaimsController extends base_controller_1.BaseController {
|
|
|
139
139
|
const { drafts } = this.state;
|
|
140
140
|
const isExistingDraft = drafts.some((existingDraft) => draft.draftId && existingDraft.draftId === draft.draftId);
|
|
141
141
|
if (isExistingDraft) {
|
|
142
|
+
const updatedAt = new Date().toISOString();
|
|
142
143
|
this.update((state) => {
|
|
143
144
|
state.drafts = state.drafts.map((existingDraft) => existingDraft.draftId === draft.draftId
|
|
144
|
-
? {
|
|
145
|
+
? {
|
|
146
|
+
...existingDraft,
|
|
147
|
+
...draft,
|
|
148
|
+
updatedAt,
|
|
149
|
+
}
|
|
145
150
|
: existingDraft);
|
|
146
151
|
});
|
|
147
|
-
return draft;
|
|
152
|
+
return { ...draft, updatedAt };
|
|
148
153
|
}
|
|
149
154
|
// generate a new draft id, name and add it to the state
|
|
150
155
|
const draftId = `draft-${Date.now()}`;
|
|
151
156
|
const newDraft = {
|
|
152
157
|
...draft,
|
|
153
158
|
draftId,
|
|
159
|
+
updatedAt: new Date().toISOString(),
|
|
154
160
|
};
|
|
155
161
|
this.update((state) => {
|
|
156
162
|
state.drafts.push(newDraft);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClaimsController.cjs","sourceRoot":"","sources":["../src/ClaimsController.ts"],"names":[],"mappings":";;;;;;;;;AAKA,+DAA2D;AAC3D,iEAA+D;AAG/D,2CAA4D;AAU5D,+CAKqB;AA2CrB,MAAM,6BAA6B,GAAyC;IAC1E,MAAM,EAAE;QACN,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,oBAAoB,EAAE;QACpB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,MAAM,EAAE;QACN,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF;;;;GAIG;AACH,SAAgB,+BAA+B;IAC7C,OAAO;QACL,oBAAoB,EAAE,yCAA6B;QACnD,MAAM,EAAE,EAAE;QACV,MAAM,EAAE,EAAE;KACX,CAAC;AACJ,CAAC;AAND,0EAMC;AAED,MAAa,gBAAi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MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,MAAM,GAAG,EAAE,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;CAgBF;AAzMD,4CAyMC;kJAT6B,KAAyB;IACnD,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;IAC9C,MAAM,uBAAuB,GAAG,cAAc,CAAC,IAAI,CACjD,CAAC,aAAa,EAAE,EAAE,CAAC,aAAa,CAAC,cAAc,KAAK,KAAK,CAAC,cAAc,CACzE,CAAC;IACF,IAAI,uBAAuB,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,yCAA6B,CAAC,uBAAuB,CAAC,CAAC;IACzE,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { detectSIWE, toHex } from '@metamask/controller-utils';\nimport type { KeyringControllerSignPersonalMessageAction } from '@metamask/keyring-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { bytesToHex, stringToBytes } from '@metamask/utils';\n\nimport type {\n ClaimsServiceFetchClaimsConfigurationsAction,\n ClaimsServiceGenerateMessageForClaimSignatureAction,\n ClaimsServiceGetClaimByIdAction,\n ClaimsServiceGetClaimsAction,\n ClaimsServiceGetClaimsApiUrlAction,\n ClaimsServiceGetRequestHeadersAction,\n} from './ClaimsService';\nimport {\n ClaimsControllerErrorMessages,\n CONTROLLER_NAME,\n DEFAULT_CLAIMS_CONFIGURATIONS,\n SERVICE_NAME,\n} from './constants';\nimport type {\n Claim,\n ClaimDraft,\n ClaimsConfigurations,\n ClaimsControllerState,\n CreateClaimRequest,\n SubmitClaimConfig,\n} from './types';\n\nexport type ClaimsControllerGetStateAction = ControllerGetStateAction<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerActions = ClaimsControllerGetStateAction;\n\nexport type AllowedActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction\n | ClaimsServiceGetClaimsAction\n | KeyringControllerSignPersonalMessageAction;\n\nexport type ClaimsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerMessenger = Messenger<\n typeof CONTROLLER_NAME,\n ClaimsControllerActions | AllowedActions,\n ClaimsControllerStateChangeEvent\n>;\n\nexport type ClaimsControllerOptions = {\n messenger: ClaimsControllerMessenger;\n state?: Partial<ClaimsControllerState>;\n};\n\nconst ClaimsControllerStateMetadata: StateMetadata<ClaimsControllerState> = {\n claims: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n claimsConfigurations: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n drafts: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\n/**\n * Get an initial default state for the controller.\n *\n * @returns The initial default controller state.\n */\nexport function getDefaultClaimsControllerState(): ClaimsControllerState {\n return {\n claimsConfigurations: DEFAULT_CLAIMS_CONFIGURATIONS,\n claims: [],\n drafts: [],\n };\n}\n\nexport class ClaimsController extends BaseController<\n typeof CONTROLLER_NAME,\n ClaimsControllerState,\n ClaimsControllerMessenger\n> {\n constructor({ messenger, state }: ClaimsControllerOptions) {\n super({\n messenger,\n metadata: ClaimsControllerStateMetadata,\n name: CONTROLLER_NAME,\n state: { ...getDefaultClaimsControllerState(), ...state },\n });\n }\n\n /**\n * Fetch the required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurations> {\n const configurations = await this.messenger.call(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n );\n\n const supportedNetworks = configurations.networks.map((network) =>\n toHex(network),\n );\n const claimsConfigurations = {\n validSubmissionWindowDays: configurations.validSubmissionWindowDays,\n supportedNetworks,\n };\n\n this.update((state) => {\n state.claimsConfigurations = claimsConfigurations;\n });\n return claimsConfigurations;\n }\n\n /**\n * Get required config for submitting a claim.\n *\n * @param claim - The claim request to get the required config for.\n * @returns The required config for submitting the claim.\n */\n async getSubmitClaimConfig(\n claim: CreateClaimRequest,\n ): Promise<SubmitClaimConfig> {\n // Validate the claim before submitting it.\n this.#validateSubmitClaimRequest(claim);\n\n const headers = await this.messenger.call(\n `${SERVICE_NAME}:getRequestHeaders`,\n );\n const baseUrl = this.messenger.call(`${SERVICE_NAME}:getClaimsApiUrl`);\n const url = `${baseUrl}/claims`;\n\n return {\n data: claim,\n headers,\n method: 'POST',\n url,\n };\n }\n\n /**\n * Generate a signature for a claim.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The signature for the claim.\n */\n async generateClaimSignature(\n chainId: number,\n walletAddress: `0x${string}`,\n ): Promise<string> {\n // generate the message to be signed\n const { message } = await this.messenger.call(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n chainId,\n walletAddress,\n );\n\n // generate and parse the SIWE message\n const messageBytes = stringToBytes(message);\n const messageHex = bytesToHex(messageBytes);\n const siwe = detectSIWE({ data: messageHex });\n if (!siwe.isSIWEMessage) {\n throw new Error(ClaimsControllerErrorMessages.INVALID_SIGNATURE_MESSAGE);\n }\n\n // sign the message\n const signature = await this.messenger.call(\n 'KeyringController:signPersonalMessage',\n {\n data: message,\n from: walletAddress,\n siwe,\n },\n );\n\n return signature;\n }\n\n /**\n * Get the list of claims for the current user.\n *\n * @returns The list of claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n const claims = await this.messenger.call(`${SERVICE_NAME}:getClaims`);\n this.update((state) => {\n state.claims = claims;\n });\n return claims;\n }\n\n /**\n * Save a claim draft to the state.\n * If the draft name is not provided, a default name will be generated.\n * If the draft with the same id already exists, it will be updated.\n *\n * @param draft - The draft to save.\n * @returns The saved draft.\n */\n saveOrUpdateClaimDraft(draft: Partial<ClaimDraft>): ClaimDraft {\n const { drafts } = this.state;\n\n const isExistingDraft = drafts.some(\n (existingDraft) =>\n draft.draftId && existingDraft.draftId === draft.draftId,\n );\n\n if (isExistingDraft) {\n this.update((state) => {\n state.drafts = state.drafts.map((existingDraft) =>\n existingDraft.draftId === draft.draftId\n ? { ...existingDraft, ...draft }\n : existingDraft,\n );\n });\n return draft as ClaimDraft;\n }\n\n // generate a new draft id, name and add it to the state\n const draftId = `draft-${Date.now()}`;\n\n const newDraft: ClaimDraft = {\n ...draft,\n draftId,\n };\n\n this.update((state) => {\n state.drafts.push(newDraft);\n });\n\n return newDraft;\n }\n\n /**\n * Get the list of claim drafts.\n *\n * @returns The list of claim drafts.\n */\n getClaimDrafts(): ClaimDraft[] {\n return this.state.drafts;\n }\n\n /**\n * Delete a claim draft from the state.\n *\n * @param draftId - The ID of the draft to delete.\n */\n deleteClaimDraft(draftId: string): void {\n this.update((state) => {\n state.drafts = state.drafts.filter((draft) => draft.draftId !== draftId);\n });\n }\n\n /**\n * Delete all claim drafts from the state.\n */\n deleteAllClaimDrafts(): void {\n this.update((state) => {\n state.drafts = [];\n });\n }\n\n /**\n * Validate the claim before submitting it.\n *\n * @param claim - The claim to validate.\n */\n #validateSubmitClaimRequest(claim: CreateClaimRequest): void {\n const { claims: existingClaims } = this.state;\n const isClaimAlreadySubmitted = existingClaims.some(\n (existingClaim) => existingClaim.impactedTxHash === claim.impactedTxHash,\n );\n if (isClaimAlreadySubmitted) {\n throw new Error(ClaimsControllerErrorMessages.CLAIM_ALREADY_SUBMITTED);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ClaimsController.cjs","sourceRoot":"","sources":["../src/ClaimsController.ts"],"names":[],"mappings":";;;;;;;;;AAKA,+DAA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6B,CAAC,uBAAuB,CAAC,CAAC;IACzE,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { detectSIWE, toHex } from '@metamask/controller-utils';\nimport type { KeyringControllerSignPersonalMessageAction } from '@metamask/keyring-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { bytesToHex, stringToBytes } from '@metamask/utils';\n\nimport type {\n ClaimsServiceFetchClaimsConfigurationsAction,\n ClaimsServiceGenerateMessageForClaimSignatureAction,\n ClaimsServiceGetClaimByIdAction,\n ClaimsServiceGetClaimsAction,\n ClaimsServiceGetClaimsApiUrlAction,\n ClaimsServiceGetRequestHeadersAction,\n} from './ClaimsService';\nimport {\n ClaimsControllerErrorMessages,\n CONTROLLER_NAME,\n DEFAULT_CLAIMS_CONFIGURATIONS,\n SERVICE_NAME,\n} from './constants';\nimport type {\n Claim,\n ClaimDraft,\n ClaimsConfigurations,\n ClaimsControllerState,\n CreateClaimRequest,\n SubmitClaimConfig,\n} from './types';\n\nexport type ClaimsControllerGetStateAction = ControllerGetStateAction<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerActions = ClaimsControllerGetStateAction;\n\nexport type AllowedActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction\n | ClaimsServiceGetClaimsAction\n | KeyringControllerSignPersonalMessageAction;\n\nexport type ClaimsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerMessenger = Messenger<\n typeof CONTROLLER_NAME,\n ClaimsControllerActions | AllowedActions,\n ClaimsControllerStateChangeEvent\n>;\n\nexport type ClaimsControllerOptions = {\n messenger: ClaimsControllerMessenger;\n state?: Partial<ClaimsControllerState>;\n};\n\nconst ClaimsControllerStateMetadata: StateMetadata<ClaimsControllerState> = {\n claims: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n claimsConfigurations: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n drafts: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\n/**\n * Get an initial default state for the controller.\n *\n * @returns The initial default controller state.\n */\nexport function getDefaultClaimsControllerState(): ClaimsControllerState {\n return {\n claimsConfigurations: DEFAULT_CLAIMS_CONFIGURATIONS,\n claims: [],\n drafts: [],\n };\n}\n\nexport class ClaimsController extends BaseController<\n typeof CONTROLLER_NAME,\n ClaimsControllerState,\n ClaimsControllerMessenger\n> {\n constructor({ messenger, state }: ClaimsControllerOptions) {\n super({\n messenger,\n metadata: ClaimsControllerStateMetadata,\n name: CONTROLLER_NAME,\n state: { ...getDefaultClaimsControllerState(), ...state },\n });\n }\n\n /**\n * Fetch the required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurations> {\n const configurations = await this.messenger.call(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n );\n\n const supportedNetworks = configurations.networks.map((network) =>\n toHex(network),\n );\n const claimsConfigurations = {\n validSubmissionWindowDays: configurations.validSubmissionWindowDays,\n supportedNetworks,\n };\n\n this.update((state) => {\n state.claimsConfigurations = claimsConfigurations;\n });\n return claimsConfigurations;\n }\n\n /**\n * Get required config for submitting a claim.\n *\n * @param claim - The claim request to get the required config for.\n * @returns The required config for submitting the claim.\n */\n async getSubmitClaimConfig(\n claim: CreateClaimRequest,\n ): Promise<SubmitClaimConfig> {\n // Validate the claim before submitting it.\n this.#validateSubmitClaimRequest(claim);\n\n const headers = await this.messenger.call(\n `${SERVICE_NAME}:getRequestHeaders`,\n );\n const baseUrl = this.messenger.call(`${SERVICE_NAME}:getClaimsApiUrl`);\n const url = `${baseUrl}/claims`;\n\n return {\n data: claim,\n headers,\n method: 'POST',\n url,\n };\n }\n\n /**\n * Generate a signature for a claim.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The signature for the claim.\n */\n async generateClaimSignature(\n chainId: number,\n walletAddress: `0x${string}`,\n ): Promise<string> {\n // generate the message to be signed\n const { message } = await this.messenger.call(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n chainId,\n walletAddress,\n );\n\n // generate and parse the SIWE message\n const messageBytes = stringToBytes(message);\n const messageHex = bytesToHex(messageBytes);\n const siwe = detectSIWE({ data: messageHex });\n if (!siwe.isSIWEMessage) {\n throw new Error(ClaimsControllerErrorMessages.INVALID_SIGNATURE_MESSAGE);\n }\n\n // sign the message\n const signature = await this.messenger.call(\n 'KeyringController:signPersonalMessage',\n {\n data: message,\n from: walletAddress,\n siwe,\n },\n );\n\n return signature;\n }\n\n /**\n * Get the list of claims for the current user.\n *\n * @returns The list of claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n const claims = await this.messenger.call(`${SERVICE_NAME}:getClaims`);\n this.update((state) => {\n state.claims = claims;\n });\n return claims;\n }\n\n /**\n * Save a claim draft to the state.\n * If the draft name is not provided, a default name will be generated.\n * If the draft with the same id already exists, it will be updated.\n *\n * @param draft - The draft to save.\n * @returns The saved draft.\n */\n saveOrUpdateClaimDraft(draft: Partial<ClaimDraft>): ClaimDraft {\n const { drafts } = this.state;\n\n const isExistingDraft = drafts.some(\n (existingDraft) =>\n draft.draftId && existingDraft.draftId === draft.draftId,\n );\n\n if (isExistingDraft) {\n const updatedAt = new Date().toISOString();\n this.update((state) => {\n state.drafts = state.drafts.map((existingDraft) =>\n existingDraft.draftId === draft.draftId\n ? {\n ...existingDraft,\n ...draft,\n updatedAt,\n }\n : existingDraft,\n );\n });\n return { ...draft, updatedAt } as ClaimDraft;\n }\n\n // generate a new draft id, name and add it to the state\n const draftId = `draft-${Date.now()}`;\n\n const newDraft: ClaimDraft = {\n ...draft,\n draftId,\n updatedAt: new Date().toISOString(),\n };\n\n this.update((state) => {\n state.drafts.push(newDraft);\n });\n\n return newDraft;\n }\n\n /**\n * Get the list of claim drafts.\n *\n * @returns The list of claim drafts.\n */\n getClaimDrafts(): ClaimDraft[] {\n return this.state.drafts;\n }\n\n /**\n * Delete a claim draft from the state.\n *\n * @param draftId - The ID of the draft to delete.\n */\n deleteClaimDraft(draftId: string): void {\n this.update((state) => {\n state.drafts = state.drafts.filter((draft) => draft.draftId !== draftId);\n });\n }\n\n /**\n * Delete all claim drafts from the state.\n */\n deleteAllClaimDrafts(): void {\n this.update((state) => {\n state.drafts = [];\n });\n }\n\n /**\n * Validate the claim before submitting it.\n *\n * @param claim - The claim to validate.\n */\n #validateSubmitClaimRequest(claim: CreateClaimRequest): void {\n const { claims: existingClaims } = this.state;\n const isClaimAlreadySubmitted = existingClaims.some(\n (existingClaim) => existingClaim.impactedTxHash === claim.impactedTxHash,\n );\n if (isClaimAlreadySubmitted) {\n throw new Error(ClaimsControllerErrorMessages.CLAIM_ALREADY_SUBMITTED);\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClaimsController.d.cts","sourceRoot":"","sources":["../src/ClaimsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,KAAK,EAAE,0CAA0C,EAAE,qCAAqC;AAC/F,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAGrD,OAAO,KAAK,EACV,4CAA4C,EAC5C,mDAAmD,EACnD,+BAA+B,EAC/B,4BAA4B,EAC5B,kCAAkC,EAClC,oCAAoC,EACrC,4BAAwB;AACzB,OAAO,EAEL,eAAe,EAGhB,wBA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,CACjC,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACpC,SAAS,EAAE,yBAAyB,CAAC;IACrC,KAAK,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,CAAC;CACxC,CAAC;AAuBF;;;;GAIG;AACH,wBAAgB,+BAA+B,IAAI,qBAAqB,CAMvE;AAED,qBAAa,gBAAiB,SAAQ,cAAc,CAClD,OAAO,eAAe,EACtB,qBAAqB,EACrB,yBAAyB,CAC1B;;gBACa,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,uBAAuB;IASzD;;;;OAIG;IACG,yBAAyB,IAAI,OAAO,CAAC,oBAAoB,CAAC;IAmBhE;;;;;OAKG;IACG,oBAAoB,CACxB,KAAK,EAAE,kBAAkB,GACxB,OAAO,CAAC,iBAAiB,CAAC;IAkB7B;;;;;;OAMG;IACG,sBAAsB,CAC1B,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,KAAK,MAAM,EAAE,GAC3B,OAAO,CAAC,MAAM,CAAC;IA6BlB;;;;OAIG;IACG,SAAS,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;IAQnC;;;;;;;OAOG;IACH,sBAAsB,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU;IAwC9D;;;;OAIG;IACH,cAAc,IAAI,UAAU,EAAE;IAI9B;;;;OAIG;IACH,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAMvC;;OAEG;IACH,oBAAoB,IAAI,IAAI;CAoB7B"}
|
|
@@ -135,18 +135,24 @@ export class ClaimsController extends BaseController {
|
|
|
135
135
|
const { drafts } = this.state;
|
|
136
136
|
const isExistingDraft = drafts.some((existingDraft) => draft.draftId && existingDraft.draftId === draft.draftId);
|
|
137
137
|
if (isExistingDraft) {
|
|
138
|
+
const updatedAt = new Date().toISOString();
|
|
138
139
|
this.update((state) => {
|
|
139
140
|
state.drafts = state.drafts.map((existingDraft) => existingDraft.draftId === draft.draftId
|
|
140
|
-
? {
|
|
141
|
+
? {
|
|
142
|
+
...existingDraft,
|
|
143
|
+
...draft,
|
|
144
|
+
updatedAt,
|
|
145
|
+
}
|
|
141
146
|
: existingDraft);
|
|
142
147
|
});
|
|
143
|
-
return draft;
|
|
148
|
+
return { ...draft, updatedAt };
|
|
144
149
|
}
|
|
145
150
|
// generate a new draft id, name and add it to the state
|
|
146
151
|
const draftId = `draft-${Date.now()}`;
|
|
147
152
|
const newDraft = {
|
|
148
153
|
...draft,
|
|
149
154
|
draftId,
|
|
155
|
+
updatedAt: new Date().toISOString(),
|
|
150
156
|
};
|
|
151
157
|
this.update((state) => {
|
|
152
158
|
state.drafts.push(newDraft);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClaimsController.mjs","sourceRoot":"","sources":["../src/ClaimsController.ts"],"names":[],"mappings":";;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,mCAAmC;AAG/D,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,wBAAwB;AAU5D,OAAO,EACL,6BAA6B,EAC7B,eAAe,EACf,6BAA6B,EAC7B,YAAY,EACb,wBAAoB;AA2CrB,MAAM,6BAA6B,GAAyC;IAC1E,MAAM,EAAE;QACN,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,oBAAoB,EAAE;QACpB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,MAAM,EAAE;QACN,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,+BAA+B;IAC7C,OAAO;QACL,oBAAoB,E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C,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,oBAAoB;QAClB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,MAAM,GAAG,EAAE,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;CAgBF;kJAT6B,KAAyB;IACnD,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;IAC9C,MAAM,uBAAuB,GAAG,cAAc,CAAC,IAAI,CACjD,CAAC,aAAa,EAAE,EAAE,CAAC,aAAa,CAAC,cAAc,KAAK,KAAK,CAAC,cAAc,CACzE,CAAC;IACF,IAAI,uBAAuB,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,uBAAuB,CAAC,CAAC;IACzE,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { detectSIWE, toHex } from '@metamask/controller-utils';\nimport type { KeyringControllerSignPersonalMessageAction } from '@metamask/keyring-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { bytesToHex, stringToBytes } from '@metamask/utils';\n\nimport type {\n ClaimsServiceFetchClaimsConfigurationsAction,\n ClaimsServiceGenerateMessageForClaimSignatureAction,\n ClaimsServiceGetClaimByIdAction,\n ClaimsServiceGetClaimsAction,\n ClaimsServiceGetClaimsApiUrlAction,\n ClaimsServiceGetRequestHeadersAction,\n} from './ClaimsService';\nimport {\n ClaimsControllerErrorMessages,\n CONTROLLER_NAME,\n DEFAULT_CLAIMS_CONFIGURATIONS,\n SERVICE_NAME,\n} from './constants';\nimport type {\n Claim,\n ClaimDraft,\n ClaimsConfigurations,\n ClaimsControllerState,\n CreateClaimRequest,\n SubmitClaimConfig,\n} from './types';\n\nexport type ClaimsControllerGetStateAction = ControllerGetStateAction<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerActions = ClaimsControllerGetStateAction;\n\nexport type AllowedActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction\n | ClaimsServiceGetClaimsAction\n | KeyringControllerSignPersonalMessageAction;\n\nexport type ClaimsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerMessenger = Messenger<\n typeof CONTROLLER_NAME,\n ClaimsControllerActions | AllowedActions,\n ClaimsControllerStateChangeEvent\n>;\n\nexport type ClaimsControllerOptions = {\n messenger: ClaimsControllerMessenger;\n state?: Partial<ClaimsControllerState>;\n};\n\nconst ClaimsControllerStateMetadata: StateMetadata<ClaimsControllerState> = {\n claims: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n claimsConfigurations: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n drafts: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\n/**\n * Get an initial default state for the controller.\n *\n * @returns The initial default controller state.\n */\nexport function getDefaultClaimsControllerState(): ClaimsControllerState {\n return {\n claimsConfigurations: DEFAULT_CLAIMS_CONFIGURATIONS,\n claims: [],\n drafts: [],\n };\n}\n\nexport class ClaimsController extends BaseController<\n typeof CONTROLLER_NAME,\n ClaimsControllerState,\n ClaimsControllerMessenger\n> {\n constructor({ messenger, state }: ClaimsControllerOptions) {\n super({\n messenger,\n metadata: ClaimsControllerStateMetadata,\n name: CONTROLLER_NAME,\n state: { ...getDefaultClaimsControllerState(), ...state },\n });\n }\n\n /**\n * Fetch the required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurations> {\n const configurations = await this.messenger.call(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n );\n\n const supportedNetworks = configurations.networks.map((network) =>\n toHex(network),\n );\n const claimsConfigurations = {\n validSubmissionWindowDays: configurations.validSubmissionWindowDays,\n supportedNetworks,\n };\n\n this.update((state) => {\n state.claimsConfigurations = claimsConfigurations;\n });\n return claimsConfigurations;\n }\n\n /**\n * Get required config for submitting a claim.\n *\n * @param claim - The claim request to get the required config for.\n * @returns The required config for submitting the claim.\n */\n async getSubmitClaimConfig(\n claim: CreateClaimRequest,\n ): Promise<SubmitClaimConfig> {\n // Validate the claim before submitting it.\n this.#validateSubmitClaimRequest(claim);\n\n const headers = await this.messenger.call(\n `${SERVICE_NAME}:getRequestHeaders`,\n );\n const baseUrl = this.messenger.call(`${SERVICE_NAME}:getClaimsApiUrl`);\n const url = `${baseUrl}/claims`;\n\n return {\n data: claim,\n headers,\n method: 'POST',\n url,\n };\n }\n\n /**\n * Generate a signature for a claim.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The signature for the claim.\n */\n async generateClaimSignature(\n chainId: number,\n walletAddress: `0x${string}`,\n ): Promise<string> {\n // generate the message to be signed\n const { message } = await this.messenger.call(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n chainId,\n walletAddress,\n );\n\n // generate and parse the SIWE message\n const messageBytes = stringToBytes(message);\n const messageHex = bytesToHex(messageBytes);\n const siwe = detectSIWE({ data: messageHex });\n if (!siwe.isSIWEMessage) {\n throw new Error(ClaimsControllerErrorMessages.INVALID_SIGNATURE_MESSAGE);\n }\n\n // sign the message\n const signature = await this.messenger.call(\n 'KeyringController:signPersonalMessage',\n {\n data: message,\n from: walletAddress,\n siwe,\n },\n );\n\n return signature;\n }\n\n /**\n * Get the list of claims for the current user.\n *\n * @returns The list of claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n const claims = await this.messenger.call(`${SERVICE_NAME}:getClaims`);\n this.update((state) => {\n state.claims = claims;\n });\n return claims;\n }\n\n /**\n * Save a claim draft to the state.\n * If the draft name is not provided, a default name will be generated.\n * If the draft with the same id already exists, it will be updated.\n *\n * @param draft - The draft to save.\n * @returns The saved draft.\n */\n saveOrUpdateClaimDraft(draft: Partial<ClaimDraft>): ClaimDraft {\n const { drafts } = this.state;\n\n const isExistingDraft = drafts.some(\n (existingDraft) =>\n draft.draftId && existingDraft.draftId === draft.draftId,\n );\n\n if (isExistingDraft) {\n this.update((state) => {\n state.drafts = state.drafts.map((existingDraft) =>\n existingDraft.draftId === draft.draftId\n ? { ...existingDraft, ...draft }\n : existingDraft,\n );\n });\n return draft as ClaimDraft;\n }\n\n // generate a new draft id, name and add it to the state\n const draftId = `draft-${Date.now()}`;\n\n const newDraft: ClaimDraft = {\n ...draft,\n draftId,\n };\n\n this.update((state) => {\n state.drafts.push(newDraft);\n });\n\n return newDraft;\n }\n\n /**\n * Get the list of claim drafts.\n *\n * @returns The list of claim drafts.\n */\n getClaimDrafts(): ClaimDraft[] {\n return this.state.drafts;\n }\n\n /**\n * Delete a claim draft from the state.\n *\n * @param draftId - The ID of the draft to delete.\n */\n deleteClaimDraft(draftId: string): void {\n this.update((state) => {\n state.drafts = state.drafts.filter((draft) => draft.draftId !== draftId);\n });\n }\n\n /**\n * Delete all claim drafts from the state.\n */\n deleteAllClaimDrafts(): void {\n this.update((state) => {\n state.drafts = [];\n });\n }\n\n /**\n * Validate the claim before submitting it.\n *\n * @param claim - The claim to validate.\n */\n #validateSubmitClaimRequest(claim: CreateClaimRequest): void {\n const { claims: existingClaims } = this.state;\n const isClaimAlreadySubmitted = existingClaims.some(\n (existingClaim) => existingClaim.impactedTxHash === claim.impactedTxHash,\n );\n if (isClaimAlreadySubmitted) {\n throw new Error(ClaimsControllerErrorMessages.CLAIM_ALREADY_SUBMITTED);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ClaimsController.mjs","sourceRoot":"","sources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jD,CAAC,aAAa,EAAE,EAAE,CAAC,aAAa,CAAC,cAAc,KAAK,KAAK,CAAC,cAAc,CACzE,CAAC;IACF,IAAI,uBAAuB,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,uBAAuB,CAAC,CAAC;IACzE,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { detectSIWE, toHex } from '@metamask/controller-utils';\nimport type { KeyringControllerSignPersonalMessageAction } from '@metamask/keyring-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { bytesToHex, stringToBytes } from '@metamask/utils';\n\nimport type {\n ClaimsServiceFetchClaimsConfigurationsAction,\n ClaimsServiceGenerateMessageForClaimSignatureAction,\n ClaimsServiceGetClaimByIdAction,\n ClaimsServiceGetClaimsAction,\n ClaimsServiceGetClaimsApiUrlAction,\n ClaimsServiceGetRequestHeadersAction,\n} from './ClaimsService';\nimport {\n ClaimsControllerErrorMessages,\n CONTROLLER_NAME,\n DEFAULT_CLAIMS_CONFIGURATIONS,\n SERVICE_NAME,\n} from './constants';\nimport type {\n Claim,\n ClaimDraft,\n ClaimsConfigurations,\n ClaimsControllerState,\n CreateClaimRequest,\n SubmitClaimConfig,\n} from './types';\n\nexport type ClaimsControllerGetStateAction = ControllerGetStateAction<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerActions = ClaimsControllerGetStateAction;\n\nexport type AllowedActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction\n | ClaimsServiceGetClaimsAction\n | KeyringControllerSignPersonalMessageAction;\n\nexport type ClaimsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof CONTROLLER_NAME,\n ClaimsControllerState\n>;\n\nexport type ClaimsControllerMessenger = Messenger<\n typeof CONTROLLER_NAME,\n ClaimsControllerActions | AllowedActions,\n ClaimsControllerStateChangeEvent\n>;\n\nexport type ClaimsControllerOptions = {\n messenger: ClaimsControllerMessenger;\n state?: Partial<ClaimsControllerState>;\n};\n\nconst ClaimsControllerStateMetadata: StateMetadata<ClaimsControllerState> = {\n claims: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n claimsConfigurations: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n drafts: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\n/**\n * Get an initial default state for the controller.\n *\n * @returns The initial default controller state.\n */\nexport function getDefaultClaimsControllerState(): ClaimsControllerState {\n return {\n claimsConfigurations: DEFAULT_CLAIMS_CONFIGURATIONS,\n claims: [],\n drafts: [],\n };\n}\n\nexport class ClaimsController extends BaseController<\n typeof CONTROLLER_NAME,\n ClaimsControllerState,\n ClaimsControllerMessenger\n> {\n constructor({ messenger, state }: ClaimsControllerOptions) {\n super({\n messenger,\n metadata: ClaimsControllerStateMetadata,\n name: CONTROLLER_NAME,\n state: { ...getDefaultClaimsControllerState(), ...state },\n });\n }\n\n /**\n * Fetch the required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurations> {\n const configurations = await this.messenger.call(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n );\n\n const supportedNetworks = configurations.networks.map((network) =>\n toHex(network),\n );\n const claimsConfigurations = {\n validSubmissionWindowDays: configurations.validSubmissionWindowDays,\n supportedNetworks,\n };\n\n this.update((state) => {\n state.claimsConfigurations = claimsConfigurations;\n });\n return claimsConfigurations;\n }\n\n /**\n * Get required config for submitting a claim.\n *\n * @param claim - The claim request to get the required config for.\n * @returns The required config for submitting the claim.\n */\n async getSubmitClaimConfig(\n claim: CreateClaimRequest,\n ): Promise<SubmitClaimConfig> {\n // Validate the claim before submitting it.\n this.#validateSubmitClaimRequest(claim);\n\n const headers = await this.messenger.call(\n `${SERVICE_NAME}:getRequestHeaders`,\n );\n const baseUrl = this.messenger.call(`${SERVICE_NAME}:getClaimsApiUrl`);\n const url = `${baseUrl}/claims`;\n\n return {\n data: claim,\n headers,\n method: 'POST',\n url,\n };\n }\n\n /**\n * Generate a signature for a claim.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The signature for the claim.\n */\n async generateClaimSignature(\n chainId: number,\n walletAddress: `0x${string}`,\n ): Promise<string> {\n // generate the message to be signed\n const { message } = await this.messenger.call(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n chainId,\n walletAddress,\n );\n\n // generate and parse the SIWE message\n const messageBytes = stringToBytes(message);\n const messageHex = bytesToHex(messageBytes);\n const siwe = detectSIWE({ data: messageHex });\n if (!siwe.isSIWEMessage) {\n throw new Error(ClaimsControllerErrorMessages.INVALID_SIGNATURE_MESSAGE);\n }\n\n // sign the message\n const signature = await this.messenger.call(\n 'KeyringController:signPersonalMessage',\n {\n data: message,\n from: walletAddress,\n siwe,\n },\n );\n\n return signature;\n }\n\n /**\n * Get the list of claims for the current user.\n *\n * @returns The list of claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n const claims = await this.messenger.call(`${SERVICE_NAME}:getClaims`);\n this.update((state) => {\n state.claims = claims;\n });\n return claims;\n }\n\n /**\n * Save a claim draft to the state.\n * If the draft name is not provided, a default name will be generated.\n * If the draft with the same id already exists, it will be updated.\n *\n * @param draft - The draft to save.\n * @returns The saved draft.\n */\n saveOrUpdateClaimDraft(draft: Partial<ClaimDraft>): ClaimDraft {\n const { drafts } = this.state;\n\n const isExistingDraft = drafts.some(\n (existingDraft) =>\n draft.draftId && existingDraft.draftId === draft.draftId,\n );\n\n if (isExistingDraft) {\n const updatedAt = new Date().toISOString();\n this.update((state) => {\n state.drafts = state.drafts.map((existingDraft) =>\n existingDraft.draftId === draft.draftId\n ? {\n ...existingDraft,\n ...draft,\n updatedAt,\n }\n : existingDraft,\n );\n });\n return { ...draft, updatedAt } as ClaimDraft;\n }\n\n // generate a new draft id, name and add it to the state\n const draftId = `draft-${Date.now()}`;\n\n const newDraft: ClaimDraft = {\n ...draft,\n draftId,\n updatedAt: new Date().toISOString(),\n };\n\n this.update((state) => {\n state.drafts.push(newDraft);\n });\n\n return newDraft;\n }\n\n /**\n * Get the list of claim drafts.\n *\n * @returns The list of claim drafts.\n */\n getClaimDrafts(): ClaimDraft[] {\n return this.state.drafts;\n }\n\n /**\n * Delete a claim draft from the state.\n *\n * @param draftId - The ID of the draft to delete.\n */\n deleteClaimDraft(draftId: string): void {\n this.update((state) => {\n state.drafts = state.drafts.filter((draft) => draft.draftId !== draftId);\n });\n }\n\n /**\n * Delete all claim drafts from the state.\n */\n deleteAllClaimDrafts(): void {\n this.update((state) => {\n state.drafts = [];\n });\n }\n\n /**\n * Validate the claim before submitting it.\n *\n * @param claim - The claim to validate.\n */\n #validateSubmitClaimRequest(claim: CreateClaimRequest): void {\n const { claims: existingClaims } = this.state;\n const isClaimAlreadySubmitted = existingClaims.some(\n (existingClaim) => existingClaim.impactedTxHash === claim.impactedTxHash,\n );\n if (isClaimAlreadySubmitted) {\n throw new Error(ClaimsControllerErrorMessages.CLAIM_ALREADY_SUBMITTED);\n }\n }\n}\n"]}
|
package/dist/ClaimsService.cjs
CHANGED
|
@@ -14,6 +14,7 @@ var _ClaimsService_env, _ClaimsService_fetch, _ClaimsService_messenger;
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.ClaimsService = void 0;
|
|
16
16
|
const constants_1 = require("./constants.cjs");
|
|
17
|
+
const utils_1 = require("./utils.cjs");
|
|
17
18
|
class ClaimsService {
|
|
18
19
|
constructor({ env, messenger, fetchFunction }) {
|
|
19
20
|
this.name = constants_1.SERVICE_NAME; // required for Modular Initialization
|
|
@@ -36,16 +37,24 @@ class ClaimsService {
|
|
|
36
37
|
* @returns The required configurations for the claims service.
|
|
37
38
|
*/
|
|
38
39
|
async fetchClaimsConfigurations() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
try {
|
|
41
|
+
const headers = await this.getRequestHeaders();
|
|
42
|
+
const url = `${this.getClaimsApiUrl()}/configurations`;
|
|
43
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
44
|
+
headers,
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
const error = await (0, utils_1.getErrorFromResponse)(response);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
const configurations = await response.json();
|
|
51
|
+
return configurations;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('fetchClaimsConfigurations', error);
|
|
55
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.((0, utils_1.createSentryError)(constants_1.ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS, error));
|
|
45
56
|
throw new Error(constants_1.ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS);
|
|
46
57
|
}
|
|
47
|
-
const configurations = await response.json();
|
|
48
|
-
return configurations;
|
|
49
58
|
}
|
|
50
59
|
/**
|
|
51
60
|
* Get the claims for the current user.
|
|
@@ -53,16 +62,24 @@ class ClaimsService {
|
|
|
53
62
|
* @returns The claims for the current user.
|
|
54
63
|
*/
|
|
55
64
|
async getClaims() {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
try {
|
|
66
|
+
const headers = await this.getRequestHeaders();
|
|
67
|
+
const url = `${this.getClaimsApiUrl()}/claims`;
|
|
68
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
69
|
+
headers,
|
|
70
|
+
});
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
const error = await (0, utils_1.getErrorFromResponse)(response);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
const claims = await response.json();
|
|
76
|
+
return claims;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
console.error('getClaims', error);
|
|
80
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.((0, utils_1.createSentryError)(constants_1.ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS, error));
|
|
62
81
|
throw new Error(constants_1.ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS);
|
|
63
82
|
}
|
|
64
|
-
const claims = await response.json();
|
|
65
|
-
return claims;
|
|
66
83
|
}
|
|
67
84
|
/**
|
|
68
85
|
* Get the claim by id.
|
|
@@ -71,16 +88,24 @@ class ClaimsService {
|
|
|
71
88
|
* @returns The claim by id.
|
|
72
89
|
*/
|
|
73
90
|
async getClaimById(id) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
91
|
+
try {
|
|
92
|
+
const headers = await this.getRequestHeaders();
|
|
93
|
+
const url = `${this.getClaimsApiUrl()}/claims/byId/${id}`;
|
|
94
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
95
|
+
headers,
|
|
96
|
+
});
|
|
97
|
+
if (!response.ok) {
|
|
98
|
+
const error = await (0, utils_1.getErrorFromResponse)(response);
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
const claim = await response.json();
|
|
102
|
+
return claim;
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error('getClaimById', error);
|
|
106
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.((0, utils_1.createSentryError)(constants_1.ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID, error));
|
|
80
107
|
throw new Error(constants_1.ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID);
|
|
81
108
|
}
|
|
82
|
-
const claim = await response.json();
|
|
83
|
-
return claim;
|
|
84
109
|
}
|
|
85
110
|
/**
|
|
86
111
|
* Generate a message to be signed by the user for the claim request.
|
|
@@ -90,24 +115,32 @@ class ClaimsService {
|
|
|
90
115
|
* @returns The message for the claim signature.
|
|
91
116
|
*/
|
|
92
117
|
async generateMessageForClaimSignature(chainId, walletAddress) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
118
|
+
try {
|
|
119
|
+
const headers = await this.getRequestHeaders();
|
|
120
|
+
const url = `${this.getClaimsApiUrl()}/signature/generateMessage`;
|
|
121
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
122
|
+
method: 'POST',
|
|
123
|
+
headers: {
|
|
124
|
+
...headers,
|
|
125
|
+
'Content-Type': 'application/json',
|
|
126
|
+
},
|
|
127
|
+
body: JSON.stringify({
|
|
128
|
+
chainId,
|
|
129
|
+
walletAddress,
|
|
130
|
+
}),
|
|
131
|
+
});
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
const error = await (0, utils_1.getErrorFromResponse)(response);
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
const message = await response.json();
|
|
137
|
+
return message;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
console.error('generateMessageForClaimSignature', error);
|
|
141
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.((0, utils_1.createSentryError)(constants_1.ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED, error));
|
|
107
142
|
throw new Error(constants_1.ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED);
|
|
108
143
|
}
|
|
109
|
-
const message = await response.json();
|
|
110
|
-
return message;
|
|
111
144
|
}
|
|
112
145
|
/**
|
|
113
146
|
* Create the headers for the current request.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClaimsService.cjs","sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,+CAIqB;AA8DrB,MAAa,aAAa;IASxB,YAAY,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,EAAuB;QARzD,SAAI,GAAG,wBAAY,CAAC,CAAC,sCAAsC;QAE3D,qCAAU;QAEV,uCAAqB;QAErB,2CAAmC;QAG1C,uBAAA,IAAI,sBAAQ,GAAG,MAAA,CAAC;QAChB,uBAAA,IAAI,4BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wBAAU,aAAa,MAAA,CAAC;QAE5B,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,4BAA4B,EAC3C,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC,CAC1C,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,YAAY,EAC3B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAC1B,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,eAAe,EAC9B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAC7B,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,oBAAoB,EACnC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAClC,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,kBAAkB,EACjC,IAA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C,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,uBAAA,IAAI,gCAAW,CAAC,IAAI,CAC5C,yCAAyC,CAC1C,CAAC;QACF,OAAO;YACL,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,eAAe;QACb,OAAO,GAAG,8BAAkB,CAAC,uBAAA,IAAI,0BAAK,CAAC,EAAE,CAAC;IAC5C,CAAC;CACF;AAhKD,sCAgKC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CLAIMS_API_URL_MAP,\n ClaimsServiceErrorMessages,\n SERVICE_NAME,\n} from './constants';\nimport type { Env } from './constants';\nimport type {\n Claim,\n ClaimsConfigurationsResponse,\n GenerateSignatureMessageResponse,\n} from './types';\n\nexport type ClaimsServiceFetchClaimsConfigurationsAction = {\n type: `${typeof SERVICE_NAME}:fetchClaimsConfigurations`;\n handler: ClaimsService['fetchClaimsConfigurations'];\n};\n\nexport type ClaimsServiceGetClaimsAction = {\n type: `${typeof SERVICE_NAME}:getClaims`;\n handler: ClaimsService['getClaims'];\n};\n\nexport type ClaimsServiceGetClaimByIdAction = {\n type: `${typeof SERVICE_NAME}:getClaimById`;\n handler: ClaimsService['getClaimById'];\n};\n\nexport type ClaimsServiceGetRequestHeadersAction = {\n type: `${typeof SERVICE_NAME}:getRequestHeaders`;\n handler: ClaimsService['getRequestHeaders'];\n};\n\nexport type ClaimsServiceGetClaimsApiUrlAction = {\n type: `${typeof SERVICE_NAME}:getClaimsApiUrl`;\n handler: ClaimsService['getClaimsApiUrl'];\n};\n\nexport type ClaimsServiceGenerateMessageForClaimSignatureAction = {\n type: `${typeof SERVICE_NAME}:generateMessageForClaimSignature`;\n handler: ClaimsService['generateMessageForClaimSignature'];\n};\n\nexport type ClaimsServiceActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type ClaimsServiceEvents = never;\n\nexport type ClaimsServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n ClaimsServiceActions | AllowedActions\n>;\n\nexport type ClaimsServiceConfig = {\n env: Env;\n messenger: ClaimsServiceMessenger;\n fetchFunction: typeof fetch;\n};\n\nexport class ClaimsService {\n readonly name = SERVICE_NAME; // required for Modular Initialization\n\n readonly #env: Env;\n\n readonly #fetch: typeof fetch;\n\n readonly #messenger: ClaimsServiceMessenger;\n\n constructor({ env, messenger, fetchFunction }: ClaimsServiceConfig) {\n this.#env = env;\n this.#messenger = messenger;\n this.#fetch = fetchFunction;\n\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n this.fetchClaimsConfigurations.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaims`,\n this.getClaims.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimById`,\n this.getClaimById.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getRequestHeaders`,\n this.getRequestHeaders.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimsApiUrl`,\n this.getClaimsApiUrl.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n this.generateMessageForClaimSignature.bind(this),\n );\n }\n\n /**\n * Fetch required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurationsResponse> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/configurations`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n throw new Error(\n ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS,\n );\n }\n\n const configurations = await response.json();\n return configurations;\n }\n\n /**\n * Get the claims for the current user.\n *\n * @returns The claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS);\n }\n\n const claims = await response.json();\n return claims;\n }\n\n /**\n * Get the claim by id.\n *\n * @param id - The id of the claim to get.\n * @returns The claim by id.\n */\n async getClaimById(id: string): Promise<Claim> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims/byId/${id}`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID);\n }\n\n const claim = await response.json();\n return claim;\n }\n\n /**\n * Generate a message to be signed by the user for the claim request.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The message for the claim signature.\n */\n async generateMessageForClaimSignature(\n chainId: number,\n walletAddress: Hex,\n ): Promise<GenerateSignatureMessageResponse> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/signature/generateMessage`;\n const response = await this.#fetch(url, {\n method: 'POST',\n headers: {\n ...headers,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n chainId,\n walletAddress,\n }),\n });\n\n if (!response.ok) {\n throw new Error(\n ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED,\n );\n }\n\n const message = await response.json();\n return message;\n }\n\n /**\n * Create the headers for the current request.\n *\n * @returns The headers for the current request.\n */\n async getRequestHeaders(): Promise<Record<string, string>> {\n const bearerToken = await this.#messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n return {\n Authorization: `Bearer ${bearerToken}`,\n };\n }\n\n /**\n * Get the URL for the claims API for the current environment.\n *\n * @returns The URL for the claims API for the current environment.\n */\n getClaimsApiUrl(): string {\n return `${CLAIMS_API_URL_MAP[this.#env]}`;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ClaimsService.cjs","sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,+CAIqB;AAOrB,uCAAkE;AAwDlE,MAAa,aAAa;IASxB,YAAY,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,EAAuB;QARzD,SAAI,GAAG,wBAAY,CAAC,CAAC,sCAAsC;QAE3D,qCAAU;QAEV,uCAAqB;QAErB,2CAAmC;QAG1C,uBAAA,IAAI,sBAAQ,GAAG,MAAA,CAAC;QAChB,uBAAA,IAAI,4BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wBAAU,aAAa,MAAA,CAAC;QAE5B,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,4BAA4B,EAC3C,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC,CAC1C,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,YAAY,EAC3B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAC1B,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,eAAe,EAC9B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAC7B,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,oBAAoB,EACnC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAClC,CAAC;QACF,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,wBAAY,kBAAkB,EACjC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAA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F,CAAC;YACF,MAAM,IAAI,KAAK,CACb,sCAA0B,CAAC,mCAAmC,CAC/D,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,uBAAA,IAAI,gCAAW,CAAC,IAAI,CAC5C,yCAAyC,CAC1C,CAAC;QACF,OAAO;YACL,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,eAAe;QACb,OAAO,GAAG,8BAAkB,CAAC,uBAAA,IAAI,0BAAK,CAAC,EAAE,CAAC;IAC5C,CAAC;CACF;AAhND,sCAgNC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CLAIMS_API_URL_MAP,\n ClaimsServiceErrorMessages,\n SERVICE_NAME,\n} from './constants';\nimport type { Env } from './constants';\nimport type {\n Claim,\n ClaimsConfigurationsResponse,\n GenerateSignatureMessageResponse,\n} from './types';\nimport { createSentryError, getErrorFromResponse } from './utils';\n\nexport type ClaimsServiceFetchClaimsConfigurationsAction = {\n type: `${typeof SERVICE_NAME}:fetchClaimsConfigurations`;\n handler: ClaimsService['fetchClaimsConfigurations'];\n};\n\nexport type ClaimsServiceGetClaimsAction = {\n type: `${typeof SERVICE_NAME}:getClaims`;\n handler: ClaimsService['getClaims'];\n};\n\nexport type ClaimsServiceGetClaimByIdAction = {\n type: `${typeof SERVICE_NAME}:getClaimById`;\n handler: ClaimsService['getClaimById'];\n};\n\nexport type ClaimsServiceGetRequestHeadersAction = {\n type: `${typeof SERVICE_NAME}:getRequestHeaders`;\n handler: ClaimsService['getRequestHeaders'];\n};\n\nexport type ClaimsServiceGetClaimsApiUrlAction = {\n type: `${typeof SERVICE_NAME}:getClaimsApiUrl`;\n handler: ClaimsService['getClaimsApiUrl'];\n};\n\nexport type ClaimsServiceGenerateMessageForClaimSignatureAction = {\n type: `${typeof SERVICE_NAME}:generateMessageForClaimSignature`;\n handler: ClaimsService['generateMessageForClaimSignature'];\n};\n\nexport type ClaimsServiceActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type ClaimsServiceEvents = never;\n\nexport type ClaimsServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n ClaimsServiceActions | AllowedActions\n>;\n\nexport type ClaimsServiceConfig = {\n env: Env;\n messenger: ClaimsServiceMessenger;\n fetchFunction: typeof fetch;\n};\n\nexport class ClaimsService {\n readonly name = SERVICE_NAME; // required for Modular Initialization\n\n readonly #env: Env;\n\n readonly #fetch: typeof fetch;\n\n readonly #messenger: ClaimsServiceMessenger;\n\n constructor({ env, messenger, fetchFunction }: ClaimsServiceConfig) {\n this.#env = env;\n this.#messenger = messenger;\n this.#fetch = fetchFunction;\n\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n this.fetchClaimsConfigurations.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaims`,\n this.getClaims.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimById`,\n this.getClaimById.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getRequestHeaders`,\n this.getRequestHeaders.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimsApiUrl`,\n this.getClaimsApiUrl.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n this.generateMessageForClaimSignature.bind(this),\n );\n }\n\n /**\n * Fetch required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurationsResponse> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/configurations`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const configurations = await response.json();\n return configurations;\n } catch (error) {\n console.error('fetchClaimsConfigurations', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS,\n error as Error,\n ),\n );\n throw new Error(\n ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS,\n );\n }\n }\n\n /**\n * Get the claims for the current user.\n *\n * @returns The claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const claims = await response.json();\n return claims;\n } catch (error) {\n console.error('getClaims', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS,\n error as Error,\n ),\n );\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS);\n }\n }\n\n /**\n * Get the claim by id.\n *\n * @param id - The id of the claim to get.\n * @returns The claim by id.\n */\n async getClaimById(id: string): Promise<Claim> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims/byId/${id}`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const claim = await response.json();\n return claim;\n } catch (error) {\n console.error('getClaimById', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID,\n error as Error,\n ),\n );\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID);\n }\n }\n\n /**\n * Generate a message to be signed by the user for the claim request.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The message for the claim signature.\n */\n async generateMessageForClaimSignature(\n chainId: number,\n walletAddress: Hex,\n ): Promise<GenerateSignatureMessageResponse> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/signature/generateMessage`;\n const response = await this.#fetch(url, {\n method: 'POST',\n headers: {\n ...headers,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n chainId,\n walletAddress,\n }),\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const message = await response.json();\n return message;\n } catch (error) {\n console.error('generateMessageForClaimSignature', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED,\n error as Error,\n ),\n );\n throw new Error(\n ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED,\n );\n }\n }\n\n /**\n * Create the headers for the current request.\n *\n * @returns The headers for the current request.\n */\n async getRequestHeaders(): Promise<Record<string, string>> {\n const bearerToken = await this.#messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n return {\n Authorization: `Bearer ${bearerToken}`,\n };\n }\n\n /**\n * Get the URL for the claims API for the current environment.\n *\n * @returns The URL for the claims API for the current environment.\n */\n getClaimsApiUrl(): string {\n return `${CLAIMS_API_URL_MAP[this.#env]}`;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClaimsService.d.cts","sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAClF,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,EAGL,YAAY,EACb,wBAAoB;AACrB,OAAO,KAA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sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAClF,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,EAGL,YAAY,EACb,wBAAoB;AACrB,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAoB;AACvC,OAAO,KAAK,EACV,KAAK,EACL,4BAA4B,EAC5B,gCAAgC,EACjC,oBAAgB;
|
|
1
|
+
{"version":3,"file":"ClaimsService.d.mts","sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAClF,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,EAGL,YAAY,EACb,wBAAoB;AACrB,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAoB;AACvC,OAAO,KAAK,EACV,KAAK,EACL,4BAA4B,EAC5B,gCAAgC,EACjC,oBAAgB;AAGjB,MAAM,MAAM,4CAA4C,GAAG;IACzD,IAAI,EAAE,GAAG,OAAO,YAAY,4BAA4B,CAAC;IACzD,OAAO,EAAE,aAAa,CAAC,2BAA2B,CAAC,CAAC;CACrD,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,IAAI,EAAE,GAAG,OAAO,YAAY,YAAY,CAAC;IACzC,OAAO,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG;IAC5C,IAAI,EAAE,GAAG,OAAO,YAAY,eAAe,CAAC;IAC5C,OAAO,EAAE,aAAa,CAAC,cAAc,CAAC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,oCAAoC,GAAG;IACjD,IAAI,EAAE,GAAG,OAAO,YAAY,oBAAoB,CAAC;IACjD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G,GACjB,OAAO,CAAC,gCAAgC,CAAC;IAqC5C;;;;OAIG;IACG,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAS1D;;;;OAIG;IACH,eAAe,IAAI,MAAM;CAG1B"}
|
package/dist/ClaimsService.mjs
CHANGED
|
@@ -11,6 +11,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
11
|
};
|
|
12
12
|
var _ClaimsService_env, _ClaimsService_fetch, _ClaimsService_messenger;
|
|
13
13
|
import { CLAIMS_API_URL_MAP, ClaimsServiceErrorMessages, SERVICE_NAME } from "./constants.mjs";
|
|
14
|
+
import { createSentryError, getErrorFromResponse } from "./utils.mjs";
|
|
14
15
|
export class ClaimsService {
|
|
15
16
|
constructor({ env, messenger, fetchFunction }) {
|
|
16
17
|
this.name = SERVICE_NAME; // required for Modular Initialization
|
|
@@ -33,16 +34,24 @@ export class ClaimsService {
|
|
|
33
34
|
* @returns The required configurations for the claims service.
|
|
34
35
|
*/
|
|
35
36
|
async fetchClaimsConfigurations() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
try {
|
|
38
|
+
const headers = await this.getRequestHeaders();
|
|
39
|
+
const url = `${this.getClaimsApiUrl()}/configurations`;
|
|
40
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
41
|
+
headers,
|
|
42
|
+
});
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
const error = await getErrorFromResponse(response);
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
const configurations = await response.json();
|
|
48
|
+
return configurations;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error('fetchClaimsConfigurations', error);
|
|
52
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.(createSentryError(ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS, error));
|
|
42
53
|
throw new Error(ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS);
|
|
43
54
|
}
|
|
44
|
-
const configurations = await response.json();
|
|
45
|
-
return configurations;
|
|
46
55
|
}
|
|
47
56
|
/**
|
|
48
57
|
* Get the claims for the current user.
|
|
@@ -50,16 +59,24 @@ export class ClaimsService {
|
|
|
50
59
|
* @returns The claims for the current user.
|
|
51
60
|
*/
|
|
52
61
|
async getClaims() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
try {
|
|
63
|
+
const headers = await this.getRequestHeaders();
|
|
64
|
+
const url = `${this.getClaimsApiUrl()}/claims`;
|
|
65
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
66
|
+
headers,
|
|
67
|
+
});
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
const error = await getErrorFromResponse(response);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
const claims = await response.json();
|
|
73
|
+
return claims;
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error('getClaims', error);
|
|
77
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.(createSentryError(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS, error));
|
|
59
78
|
throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS);
|
|
60
79
|
}
|
|
61
|
-
const claims = await response.json();
|
|
62
|
-
return claims;
|
|
63
80
|
}
|
|
64
81
|
/**
|
|
65
82
|
* Get the claim by id.
|
|
@@ -68,16 +85,24 @@ export class ClaimsService {
|
|
|
68
85
|
* @returns The claim by id.
|
|
69
86
|
*/
|
|
70
87
|
async getClaimById(id) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
88
|
+
try {
|
|
89
|
+
const headers = await this.getRequestHeaders();
|
|
90
|
+
const url = `${this.getClaimsApiUrl()}/claims/byId/${id}`;
|
|
91
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
92
|
+
headers,
|
|
93
|
+
});
|
|
94
|
+
if (!response.ok) {
|
|
95
|
+
const error = await getErrorFromResponse(response);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
const claim = await response.json();
|
|
99
|
+
return claim;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error('getClaimById', error);
|
|
103
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.(createSentryError(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID, error));
|
|
77
104
|
throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID);
|
|
78
105
|
}
|
|
79
|
-
const claim = await response.json();
|
|
80
|
-
return claim;
|
|
81
106
|
}
|
|
82
107
|
/**
|
|
83
108
|
* Generate a message to be signed by the user for the claim request.
|
|
@@ -87,24 +112,32 @@ export class ClaimsService {
|
|
|
87
112
|
* @returns The message for the claim signature.
|
|
88
113
|
*/
|
|
89
114
|
async generateMessageForClaimSignature(chainId, walletAddress) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
115
|
+
try {
|
|
116
|
+
const headers = await this.getRequestHeaders();
|
|
117
|
+
const url = `${this.getClaimsApiUrl()}/signature/generateMessage`;
|
|
118
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
119
|
+
method: 'POST',
|
|
120
|
+
headers: {
|
|
121
|
+
...headers,
|
|
122
|
+
'Content-Type': 'application/json',
|
|
123
|
+
},
|
|
124
|
+
body: JSON.stringify({
|
|
125
|
+
chainId,
|
|
126
|
+
walletAddress,
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
const error = await getErrorFromResponse(response);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
const message = await response.json();
|
|
134
|
+
return message;
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
console.error('generateMessageForClaimSignature', error);
|
|
138
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").captureException?.(createSentryError(ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED, error));
|
|
104
139
|
throw new Error(ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED);
|
|
105
140
|
}
|
|
106
|
-
const message = await response.json();
|
|
107
|
-
return message;
|
|
108
141
|
}
|
|
109
142
|
/**
|
|
110
143
|
* Create the headers for the current request.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClaimsService.mjs","sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,OAAO,EACL,kBAAkB,EAClB,0BAA0B,EAC1B,YAAY,EACb,wBAAoB;AA8DrB,MAAM,OAAO,aAAa;IASxB,YAAY,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,EAAuB;QARzD,SAAI,GAAG,YAAY,CAAC,CAAC,sCAAsC;QAE3D,qCAAU;QAEV,uCAAqB;QAErB,2CAAmC;QAG1C,uBAAA,IAAI,sBAAQ,GAAG,MAAA,CAAC;QAChB,uBAAA,IAAI,4BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wBAAU,aAAa,MAAA,CAAC;QAE5B,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,YAAY,4BAA4B,EAC3C,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC,CAC1C,CAAC;QACF,uBAAA,IAAI,gCAAW,CA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AG,EAAE;YACtC,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,GAAG,OAAO;gBACV,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;gBACnB,OAAO;gBACP,aAAa;aACd,CAAC;SACH,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CACb,0BAA0B,CAAC,mCAAmC,CAC/D,CAAC;QACJ,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtC,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,uBAAA,IAAI,gCAAW,CAAC,IAAI,CAC5C,yCAAyC,CAC1C,CAAC;QACF,OAAO;YACL,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,eAAe;QACb,OAAO,GAAG,kBAAkB,CAAC,uBAAA,IAAI,0BAAK,CAAC,EAAE,CAAC;IAC5C,CAAC;CACF","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CLAIMS_API_URL_MAP,\n ClaimsServiceErrorMessages,\n SERVICE_NAME,\n} from './constants';\nimport type { Env } from './constants';\nimport type {\n Claim,\n ClaimsConfigurationsResponse,\n GenerateSignatureMessageResponse,\n} from './types';\n\nexport type ClaimsServiceFetchClaimsConfigurationsAction = {\n type: `${typeof SERVICE_NAME}:fetchClaimsConfigurations`;\n handler: ClaimsService['fetchClaimsConfigurations'];\n};\n\nexport type ClaimsServiceGetClaimsAction = {\n type: `${typeof SERVICE_NAME}:getClaims`;\n handler: ClaimsService['getClaims'];\n};\n\nexport type ClaimsServiceGetClaimByIdAction = {\n type: `${typeof SERVICE_NAME}:getClaimById`;\n handler: ClaimsService['getClaimById'];\n};\n\nexport type ClaimsServiceGetRequestHeadersAction = {\n type: `${typeof SERVICE_NAME}:getRequestHeaders`;\n handler: ClaimsService['getRequestHeaders'];\n};\n\nexport type ClaimsServiceGetClaimsApiUrlAction = {\n type: `${typeof SERVICE_NAME}:getClaimsApiUrl`;\n handler: ClaimsService['getClaimsApiUrl'];\n};\n\nexport type ClaimsServiceGenerateMessageForClaimSignatureAction = {\n type: `${typeof SERVICE_NAME}:generateMessageForClaimSignature`;\n handler: ClaimsService['generateMessageForClaimSignature'];\n};\n\nexport type ClaimsServiceActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type ClaimsServiceEvents = never;\n\nexport type ClaimsServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n ClaimsServiceActions | AllowedActions\n>;\n\nexport type ClaimsServiceConfig = {\n env: Env;\n messenger: ClaimsServiceMessenger;\n fetchFunction: typeof fetch;\n};\n\nexport class ClaimsService {\n readonly name = SERVICE_NAME; // required for Modular Initialization\n\n readonly #env: Env;\n\n readonly #fetch: typeof fetch;\n\n readonly #messenger: ClaimsServiceMessenger;\n\n constructor({ env, messenger, fetchFunction }: ClaimsServiceConfig) {\n this.#env = env;\n this.#messenger = messenger;\n this.#fetch = fetchFunction;\n\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n this.fetchClaimsConfigurations.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaims`,\n this.getClaims.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimById`,\n this.getClaimById.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getRequestHeaders`,\n this.getRequestHeaders.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimsApiUrl`,\n this.getClaimsApiUrl.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n this.generateMessageForClaimSignature.bind(this),\n );\n }\n\n /**\n * Fetch required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurationsResponse> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/configurations`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n throw new Error(\n ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS,\n );\n }\n\n const configurations = await response.json();\n return configurations;\n }\n\n /**\n * Get the claims for the current user.\n *\n * @returns The claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS);\n }\n\n const claims = await response.json();\n return claims;\n }\n\n /**\n * Get the claim by id.\n *\n * @param id - The id of the claim to get.\n * @returns The claim by id.\n */\n async getClaimById(id: string): Promise<Claim> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims/byId/${id}`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID);\n }\n\n const claim = await response.json();\n return claim;\n }\n\n /**\n * Generate a message to be signed by the user for the claim request.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The message for the claim signature.\n */\n async generateMessageForClaimSignature(\n chainId: number,\n walletAddress: Hex,\n ): Promise<GenerateSignatureMessageResponse> {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/signature/generateMessage`;\n const response = await this.#fetch(url, {\n method: 'POST',\n headers: {\n ...headers,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n chainId,\n walletAddress,\n }),\n });\n\n if (!response.ok) {\n throw new Error(\n ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED,\n );\n }\n\n const message = await response.json();\n return message;\n }\n\n /**\n * Create the headers for the current request.\n *\n * @returns The headers for the current request.\n */\n async getRequestHeaders(): Promise<Record<string, string>> {\n const bearerToken = await this.#messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n return {\n Authorization: `Bearer ${bearerToken}`,\n };\n }\n\n /**\n * Get the URL for the claims API for the current environment.\n *\n * @returns The URL for the claims API for the current environment.\n */\n getClaimsApiUrl(): string {\n return `${CLAIMS_API_URL_MAP[this.#env]}`;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ClaimsService.mjs","sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,OAAO,EACL,kBAAkB,EAClB,0BAA0B,EAC1B,YAAY,EACb,wBAAoB;AAOrB,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,oBAAgB;AAwDlE,MAAM,OAAO,aAAa;IASxB,YAAY,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,EAAuB;QARzD,SAAI,GAAG,YAAY,CAAC,CAAC,sCAAsC;QAE3D,qCAAU;QAEV,uCAAqB;QAErB,2CAAmC;QAG1C,uBAAA,IAAI,sBAAQ,GAAG,MAAA,CAAC;QAChB,uBAAA,IAAI,4BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wBAAU,aAAa,MAAA,CAAC;QAE5B,uBAAA,IAAI,gCAAW,CAAC,qBAAqB,CACnC,GAAG,YAAY,4BAA4B,EAC3C,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC,CAC1C,CAAC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AAC;aACH,CAAC,CAAC;YAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,MAAM,KAAK,GAAG,MAAM,oBAAoB,CAAC,QAAQ,CAAC,CAAC;gBACnD,MAAM,KAAK,CAAC;YACd,CAAC;YAED,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACtC,OAAO,OAAO,CAAC;QACjB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAC;YACzD,uBAAA,IAAI,gCAAW,CAAC,gBAAgB,EAAE,CAChC,iBAAiB,CACf,0BAA0B,CAAC,mCAAmC,EAC9D,KAAc,CACf,CACF,CAAC;YACF,MAAM,IAAI,KAAK,CACb,0BAA0B,CAAC,mCAAmC,CAC/D,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,uBAAA,IAAI,gCAAW,CAAC,IAAI,CAC5C,yCAAyC,CAC1C,CAAC;QACF,OAAO;YACL,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,eAAe;QACb,OAAO,GAAG,kBAAkB,CAAC,uBAAA,IAAI,0BAAK,CAAC,EAAE,CAAC;IAC5C,CAAC;CACF","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CLAIMS_API_URL_MAP,\n ClaimsServiceErrorMessages,\n SERVICE_NAME,\n} from './constants';\nimport type { Env } from './constants';\nimport type {\n Claim,\n ClaimsConfigurationsResponse,\n GenerateSignatureMessageResponse,\n} from './types';\nimport { createSentryError, getErrorFromResponse } from './utils';\n\nexport type ClaimsServiceFetchClaimsConfigurationsAction = {\n type: `${typeof SERVICE_NAME}:fetchClaimsConfigurations`;\n handler: ClaimsService['fetchClaimsConfigurations'];\n};\n\nexport type ClaimsServiceGetClaimsAction = {\n type: `${typeof SERVICE_NAME}:getClaims`;\n handler: ClaimsService['getClaims'];\n};\n\nexport type ClaimsServiceGetClaimByIdAction = {\n type: `${typeof SERVICE_NAME}:getClaimById`;\n handler: ClaimsService['getClaimById'];\n};\n\nexport type ClaimsServiceGetRequestHeadersAction = {\n type: `${typeof SERVICE_NAME}:getRequestHeaders`;\n handler: ClaimsService['getRequestHeaders'];\n};\n\nexport type ClaimsServiceGetClaimsApiUrlAction = {\n type: `${typeof SERVICE_NAME}:getClaimsApiUrl`;\n handler: ClaimsService['getClaimsApiUrl'];\n};\n\nexport type ClaimsServiceGenerateMessageForClaimSignatureAction = {\n type: `${typeof SERVICE_NAME}:generateMessageForClaimSignature`;\n handler: ClaimsService['generateMessageForClaimSignature'];\n};\n\nexport type ClaimsServiceActions =\n | ClaimsServiceFetchClaimsConfigurationsAction\n | ClaimsServiceGetClaimsAction\n | ClaimsServiceGetClaimByIdAction\n | ClaimsServiceGetRequestHeadersAction\n | ClaimsServiceGetClaimsApiUrlAction\n | ClaimsServiceGenerateMessageForClaimSignatureAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type ClaimsServiceEvents = never;\n\nexport type ClaimsServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n ClaimsServiceActions | AllowedActions\n>;\n\nexport type ClaimsServiceConfig = {\n env: Env;\n messenger: ClaimsServiceMessenger;\n fetchFunction: typeof fetch;\n};\n\nexport class ClaimsService {\n readonly name = SERVICE_NAME; // required for Modular Initialization\n\n readonly #env: Env;\n\n readonly #fetch: typeof fetch;\n\n readonly #messenger: ClaimsServiceMessenger;\n\n constructor({ env, messenger, fetchFunction }: ClaimsServiceConfig) {\n this.#env = env;\n this.#messenger = messenger;\n this.#fetch = fetchFunction;\n\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:fetchClaimsConfigurations`,\n this.fetchClaimsConfigurations.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaims`,\n this.getClaims.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimById`,\n this.getClaimById.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getRequestHeaders`,\n this.getRequestHeaders.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:getClaimsApiUrl`,\n this.getClaimsApiUrl.bind(this),\n );\n this.#messenger.registerActionHandler(\n `${SERVICE_NAME}:generateMessageForClaimSignature`,\n this.generateMessageForClaimSignature.bind(this),\n );\n }\n\n /**\n * Fetch required configurations for the claims service.\n *\n * @returns The required configurations for the claims service.\n */\n async fetchClaimsConfigurations(): Promise<ClaimsConfigurationsResponse> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/configurations`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const configurations = await response.json();\n return configurations;\n } catch (error) {\n console.error('fetchClaimsConfigurations', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS,\n error as Error,\n ),\n );\n throw new Error(\n ClaimsServiceErrorMessages.FAILED_TO_FETCH_CONFIGURATIONS,\n );\n }\n }\n\n /**\n * Get the claims for the current user.\n *\n * @returns The claims for the current user.\n */\n async getClaims(): Promise<Claim[]> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const claims = await response.json();\n return claims;\n } catch (error) {\n console.error('getClaims', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS,\n error as Error,\n ),\n );\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS);\n }\n }\n\n /**\n * Get the claim by id.\n *\n * @param id - The id of the claim to get.\n * @returns The claim by id.\n */\n async getClaimById(id: string): Promise<Claim> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/claims/byId/${id}`;\n const response = await this.#fetch(url, {\n headers,\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const claim = await response.json();\n return claim;\n } catch (error) {\n console.error('getClaimById', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID,\n error as Error,\n ),\n );\n throw new Error(ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID);\n }\n }\n\n /**\n * Generate a message to be signed by the user for the claim request.\n *\n * @param chainId - The chain id of the claim.\n * @param walletAddress - The impacted wallet address of the claim.\n * @returns The message for the claim signature.\n */\n async generateMessageForClaimSignature(\n chainId: number,\n walletAddress: Hex,\n ): Promise<GenerateSignatureMessageResponse> {\n try {\n const headers = await this.getRequestHeaders();\n const url = `${this.getClaimsApiUrl()}/signature/generateMessage`;\n const response = await this.#fetch(url, {\n method: 'POST',\n headers: {\n ...headers,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n chainId,\n walletAddress,\n }),\n });\n\n if (!response.ok) {\n const error = await getErrorFromResponse(response);\n throw error;\n }\n\n const message = await response.json();\n return message;\n } catch (error) {\n console.error('generateMessageForClaimSignature', error);\n this.#messenger.captureException?.(\n createSentryError(\n ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED,\n error as Error,\n ),\n );\n throw new Error(\n ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED,\n );\n }\n }\n\n /**\n * Create the headers for the current request.\n *\n * @returns The headers for the current request.\n */\n async getRequestHeaders(): Promise<Record<string, string>> {\n const bearerToken = await this.#messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n return {\n Authorization: `Bearer ${bearerToken}`,\n };\n }\n\n /**\n * Get the URL for the claims API for the current environment.\n *\n * @returns The URL for the claims API for the current environment.\n */\n getClaimsApiUrl(): string {\n return `${CLAIMS_API_URL_MAP[this.#env]}`;\n }\n}\n"]}
|
package/dist/types.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport type { ClaimStatusEnum } from './constants';\n\nexport type Attachment = {\n publicUrl: string;\n contentType: string;\n originalname: string;\n};\n\nexport type ClaimsConfigurations = {\n /**\n * The number of days the claim is valid for submission.\n */\n validSubmissionWindowDays: number;\n\n /**\n * List of supported chain IDs in hexadecimal format.\n */\n supportedNetworks: `0x${string}`[];\n};\n\nexport type ClaimsConfigurationsResponse = Omit<\n ClaimsConfigurations,\n 'supportedNetworks'\n> & {\n /**\n * List of supported chain IDs.\n * Claims API response for `supportedNetworks` field (in decimal format).\n */\n networks: number[];\n};\n\nexport type Claim = {\n id: string;\n shortId: string;\n chainId: string;\n email: string;\n impactedWalletAddress: Hex;\n impactedTxHash: Hex;\n reimbursementWalletAddress: Hex;\n description: string;\n signature: Hex;\n attachments?: Attachment[];\n status: ClaimStatusEnum;\n createdAt: string;\n updatedAt: string;\n intercomId?: string;\n};\n\nexport type ClaimDraft = Partial<\n Omit<\n Claim,\n
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport type { ClaimStatusEnum } from './constants';\n\nexport type Attachment = {\n publicUrl: string;\n contentType: string;\n originalname: string;\n};\n\nexport type ClaimsConfigurations = {\n /**\n * The number of days the claim is valid for submission.\n */\n validSubmissionWindowDays: number;\n\n /**\n * List of supported chain IDs in hexadecimal format.\n */\n supportedNetworks: `0x${string}`[];\n};\n\nexport type ClaimsConfigurationsResponse = Omit<\n ClaimsConfigurations,\n 'supportedNetworks'\n> & {\n /**\n * List of supported chain IDs.\n * Claims API response for `supportedNetworks` field (in decimal format).\n */\n networks: number[];\n};\n\nexport type Claim = {\n id: string;\n shortId: string;\n chainId: string;\n email: string;\n impactedWalletAddress: Hex;\n impactedTxHash: Hex;\n reimbursementWalletAddress: Hex;\n description: string;\n signature: Hex;\n attachments?: Attachment[];\n status: ClaimStatusEnum;\n createdAt: string;\n updatedAt: string;\n intercomId?: string;\n};\n\nexport type ClaimDraft = Partial<\n Omit<\n Claim,\n 'id' | 'shortId' | 'createdAt' | 'intercomId' | 'status' | 'attachments'\n >\n> & {\n /**\n * The draft ID.\n */\n draftId: string;\n};\n\nexport type CreateClaimRequest = Omit<\n Claim,\n 'id' | 'shortId' | 'createdAt' | 'updatedAt' | 'intercomId' | 'status'\n>;\n\nexport type ClaimsControllerState = {\n /**\n * List of claims.\n */\n claims: Claim[];\n\n /**\n * The claims configurations.\n * This is used to store the claims configurations fetched from the backend.\n */\n claimsConfigurations: ClaimsConfigurations;\n\n /**\n * List of claim drafts before submission.\n */\n drafts: ClaimDraft[];\n};\n\nexport type SubmitClaimConfig = {\n /**\n * The sanitized and validated data to be submitted.\n */\n data: CreateClaimRequest;\n /**\n * The headers to be used in the request.\n */\n headers: Record<string, string>;\n /**\n * The HTTP method to submit.\n */\n method: 'POST';\n /**\n * The URL to submit the claim to.\n */\n url: string;\n};\n\nexport type GenerateSignatureMessageResponse = {\n message: string;\n nonce: string;\n};\n"]}
|
package/dist/types.d.cts
CHANGED
|
@@ -38,7 +38,7 @@ export type Claim = {
|
|
|
38
38
|
updatedAt: string;
|
|
39
39
|
intercomId?: string;
|
|
40
40
|
};
|
|
41
|
-
export type ClaimDraft = Partial<Omit<Claim, 'id' | 'shortId' | 'createdAt' | '
|
|
41
|
+
export type ClaimDraft = Partial<Omit<Claim, 'id' | 'shortId' | 'createdAt' | 'intercomId' | 'status' | 'attachments'>> & {
|
|
42
42
|
/**
|
|
43
43
|
* The draft ID.
|
|
44
44
|
*/
|
package/dist/types.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,eAAe,EAAE,wBAAoB;AAEnD,MAAM,MAAM,UAAU,GAAG;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC;;OAEG;IACH,yBAAyB,EAAE,MAAM,CAAC;IAElC;;OAEG;IACH,iBAAiB,EAAE,KAAK,MAAM,EAAE,EAAE,CAAC;CACpC,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG,IAAI,CAC7C,oBAAoB,EACpB,mBAAmB,CACpB,GAAG;IACF;;;OAGG;IACH,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,qBAAqB,EAAE,GAAG,CAAC;IAC3B,cAAc,EAAE,GAAG,CAAC;IACpB,0BAA0B,EAAE,GAAG,CAAC;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,GAAG,CAAC;IACf,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;IAC3B,MAAM,EAAE,eAAe,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,OAAO,CAC9B,IAAI,CACF,KAAK,
|
|
1
|
+
{"version":3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E,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,IAAI,CACnC,KAAK,EACL,IAAI,GAAG,SAAS,GAAG,WAAW,GAAG,WAAW,GAAG,YAAY,GAAG,QAAQ,CACvE,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC;;OAEG;IACH,MAAM,EAAE,KAAK,EAAE,CAAC;IAEhB;;;OAGG;IACH,oBAAoB,EAAE,oBAAoB,CAAC;IAE3C;;OAEG;IACH,MAAM,EAAE,UAAU,EAAE,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,IAAI,EAAE,kBAAkB,CAAC;IACzB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,gCAAgC,GAAG;IAC7C,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC"}
|
package/dist/types.d.mts
CHANGED
|
@@ -38,7 +38,7 @@ export type Claim = {
|
|
|
38
38
|
updatedAt: string;
|
|
39
39
|
intercomId?: string;
|
|
40
40
|
};
|
|
41
|
-
export type ClaimDraft = Partial<Omit<Claim, 'id' | 'shortId' | 'createdAt' | '
|
|
41
|
+
export type ClaimDraft = Partial<Omit<Claim, 'id' | 'shortId' | 'createdAt' | 'intercomId' | 'status' | 'attachments'>> & {
|
|
42
42
|
/**
|
|
43
43
|
* The draft ID.
|
|
44
44
|
*/
|
package/dist/types.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,eAAe,EAAE,w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E,CAAC;IAEhB;;;OAGG;IACH,oBAAoB,EAAE,oBAAoB,CAAC;IAE3C;;OAEG;IACH,MAAM,EAAE,UAAU,EAAE,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,IAAI,EAAE,kBAAkB,CAAC;IACzB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,gCAAgC,GAAG;IAC7C,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC"}
|
package/dist/types.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport type { ClaimStatusEnum } from './constants';\n\nexport type Attachment = {\n publicUrl: string;\n contentType: string;\n originalname: string;\n};\n\nexport type ClaimsConfigurations = {\n /**\n * The number of days the claim is valid for submission.\n */\n validSubmissionWindowDays: number;\n\n /**\n * List of supported chain IDs in hexadecimal format.\n */\n supportedNetworks: `0x${string}`[];\n};\n\nexport type ClaimsConfigurationsResponse = Omit<\n ClaimsConfigurations,\n 'supportedNetworks'\n> & {\n /**\n * List of supported chain IDs.\n * Claims API response for `supportedNetworks` field (in decimal format).\n */\n networks: number[];\n};\n\nexport type Claim = {\n id: string;\n shortId: string;\n chainId: string;\n email: string;\n impactedWalletAddress: Hex;\n impactedTxHash: Hex;\n reimbursementWalletAddress: Hex;\n description: string;\n signature: Hex;\n attachments?: Attachment[];\n status: ClaimStatusEnum;\n createdAt: string;\n updatedAt: string;\n intercomId?: string;\n};\n\nexport type ClaimDraft = Partial<\n Omit<\n Claim,\n
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport type { ClaimStatusEnum } from './constants';\n\nexport type Attachment = {\n publicUrl: string;\n contentType: string;\n originalname: string;\n};\n\nexport type ClaimsConfigurations = {\n /**\n * The number of days the claim is valid for submission.\n */\n validSubmissionWindowDays: number;\n\n /**\n * List of supported chain IDs in hexadecimal format.\n */\n supportedNetworks: `0x${string}`[];\n};\n\nexport type ClaimsConfigurationsResponse = Omit<\n ClaimsConfigurations,\n 'supportedNetworks'\n> & {\n /**\n * List of supported chain IDs.\n * Claims API response for `supportedNetworks` field (in decimal format).\n */\n networks: number[];\n};\n\nexport type Claim = {\n id: string;\n shortId: string;\n chainId: string;\n email: string;\n impactedWalletAddress: Hex;\n impactedTxHash: Hex;\n reimbursementWalletAddress: Hex;\n description: string;\n signature: Hex;\n attachments?: Attachment[];\n status: ClaimStatusEnum;\n createdAt: string;\n updatedAt: string;\n intercomId?: string;\n};\n\nexport type ClaimDraft = Partial<\n Omit<\n Claim,\n 'id' | 'shortId' | 'createdAt' | 'intercomId' | 'status' | 'attachments'\n >\n> & {\n /**\n * The draft ID.\n */\n draftId: string;\n};\n\nexport type CreateClaimRequest = Omit<\n Claim,\n 'id' | 'shortId' | 'createdAt' | 'updatedAt' | 'intercomId' | 'status'\n>;\n\nexport type ClaimsControllerState = {\n /**\n * List of claims.\n */\n claims: Claim[];\n\n /**\n * The claims configurations.\n * This is used to store the claims configurations fetched from the backend.\n */\n claimsConfigurations: ClaimsConfigurations;\n\n /**\n * List of claim drafts before submission.\n */\n drafts: ClaimDraft[];\n};\n\nexport type SubmitClaimConfig = {\n /**\n * The sanitized and validated data to be submitted.\n */\n data: CreateClaimRequest;\n /**\n * The headers to be used in the request.\n */\n headers: Record<string, string>;\n /**\n * The HTTP method to submit.\n */\n method: 'POST';\n /**\n * The URL to submit the claim to.\n */\n url: string;\n};\n\nexport type GenerateSignatureMessageResponse = {\n message: string;\n nonce: string;\n};\n"]}
|
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSentryError = exports.getErrorFromResponse = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Get an error from a response.
|
|
6
|
+
*
|
|
7
|
+
* @param response - The response to get an error from.
|
|
8
|
+
* @returns An error.
|
|
9
|
+
*/
|
|
10
|
+
async function getErrorFromResponse(response) {
|
|
11
|
+
const contentType = response.headers?.get('content-type');
|
|
12
|
+
const statusCode = response.status;
|
|
13
|
+
try {
|
|
14
|
+
if (contentType?.includes('application/json')) {
|
|
15
|
+
const json = await response.json();
|
|
16
|
+
const errorMessage = json?.error ?? json?.message ?? 'Unknown error';
|
|
17
|
+
const networkError = `error: ${errorMessage}, statusCode: ${statusCode}`;
|
|
18
|
+
return new Error(networkError);
|
|
19
|
+
}
|
|
20
|
+
else if (contentType?.includes('text/plain')) {
|
|
21
|
+
const text = await response.text();
|
|
22
|
+
const networkError = `error: ${text}, statusCode: ${statusCode}`;
|
|
23
|
+
return new Error(networkError);
|
|
24
|
+
}
|
|
25
|
+
const error = 'data' in response && typeof response.data === 'string'
|
|
26
|
+
? response.data
|
|
27
|
+
: 'Unknown error';
|
|
28
|
+
const networkError = `error: ${error}, statusCode: ${statusCode}`;
|
|
29
|
+
return new Error(networkError);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return new Error(`HTTP ${statusCode} error`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.getErrorFromResponse = getErrorFromResponse;
|
|
36
|
+
/**
|
|
37
|
+
* Creates an error instance with a readable message and the root cause.
|
|
38
|
+
*
|
|
39
|
+
* @param message - The error message to create a Sentry error from.
|
|
40
|
+
* @param cause - The inner error to create a Sentry error from.
|
|
41
|
+
* @returns A Sentry error.
|
|
42
|
+
*/
|
|
43
|
+
function createSentryError(message, cause) {
|
|
44
|
+
const sentryError = new Error(message);
|
|
45
|
+
sentryError.cause = cause;
|
|
46
|
+
return sentryError;
|
|
47
|
+
}
|
|
48
|
+
exports.createSentryError = createSentryError;
|
|
49
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACI,KAAK,UAAU,oBAAoB,CAAC,QAAkB;IAC3D,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;IAC1D,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC;IACnC,IAAI,CAAC;QACH,IAAI,WAAW,EAAE,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC;YAC9C,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,MAAM,YAAY,GAAG,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,OAAO,IAAI,eAAe,CAAC;YACrE,MAAM,YAAY,GAAG,UAAU,YAAY,iBAAiB,UAAU,EAAE,CAAC;YACzE,OAAO,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;QACjC,CAAC;aAAM,IAAI,WAAW,EAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,MAAM,YAAY,GAAG,UAAU,IAAI,iBAAiB,UAAU,EAAE,CAAC;YACjE,OAAO,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;QACjC,CAAC;QAED,MAAM,KAAK,GACT,MAAM,IAAI,QAAQ,IAAI,OAAO,QAAQ,CAAC,IAAI,KAAK,QAAQ;YACrD,CAAC,CAAC,QAAQ,CAAC,IAAI;YACf,CAAC,CAAC,eAAe,CAAC;QACtB,MAAM,YAAY,GAAG,UAAU,KAAK,iBAAiB,UAAU,EAAE,CAAC;QAClE,OAAO,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;IACjC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,KAAK,CAAC,QAAQ,UAAU,QAAQ,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAxBD,oDAwBC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAAC,OAAe,EAAE,KAAY;IAC7D,MAAM,WAAW,GAAG,IAAI,KAAK,CAAC,OAAO,CAEpC,CAAC;IACF,WAAW,CAAC,KAAK,GAAG,KAAK,CAAC;IAE1B,OAAO,WAAW,CAAC;AACrB,CAAC;AAPD,8CAOC","sourcesContent":["/**\n * Get an error from a response.\n *\n * @param response - The response to get an error from.\n * @returns An error.\n */\nexport async function getErrorFromResponse(response: Response): Promise<Error> {\n const contentType = response.headers?.get('content-type');\n const statusCode = response.status;\n try {\n if (contentType?.includes('application/json')) {\n const json = await response.json();\n const errorMessage = json?.error ?? json?.message ?? 'Unknown error';\n const networkError = `error: ${errorMessage}, statusCode: ${statusCode}`;\n return new Error(networkError);\n } else if (contentType?.includes('text/plain')) {\n const text = await response.text();\n const networkError = `error: ${text}, statusCode: ${statusCode}`;\n return new Error(networkError);\n }\n\n const error =\n 'data' in response && typeof response.data === 'string'\n ? response.data\n : 'Unknown error';\n const networkError = `error: ${error}, statusCode: ${statusCode}`;\n return new Error(networkError);\n } catch {\n return new Error(`HTTP ${statusCode} error`);\n }\n}\n\n/**\n * Creates an error instance with a readable message and the root cause.\n *\n * @param message - The error message to create a Sentry error from.\n * @param cause - The inner error to create a Sentry error from.\n * @returns A Sentry error.\n */\nexport function createSentryError(message: string, cause: Error): Error {\n const sentryError = new Error(message) as Error & {\n cause: Error;\n };\n sentryError.cause = cause;\n\n return sentryError;\n}\n"]}
|
package/dist/utils.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get an error from a response.
|
|
3
|
+
*
|
|
4
|
+
* @param response - The response to get an error from.
|
|
5
|
+
* @returns An error.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getErrorFromResponse(response: Response): Promise<Error>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an error instance with a readable message and the root cause.
|
|
10
|
+
*
|
|
11
|
+
* @param message - The error message to create a Sentry error from.
|
|
12
|
+
* @param cause - The inner error to create a Sentry error from.
|
|
13
|
+
* @returns A Sentry error.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createSentryError(message: string, cause: Error): Error;
|
|
16
|
+
//# sourceMappingURL=utils.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAwB7E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CAOtE"}
|
package/dist/utils.d.mts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get an error from a response.
|
|
3
|
+
*
|
|
4
|
+
* @param response - The response to get an error from.
|
|
5
|
+
* @returns An error.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getErrorFromResponse(response: Response): Promise<Error>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an error instance with a readable message and the root cause.
|
|
10
|
+
*
|
|
11
|
+
* @param message - The error message to create a Sentry error from.
|
|
12
|
+
* @param cause - The inner error to create a Sentry error from.
|
|
13
|
+
* @returns A Sentry error.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createSentryError(message: string, cause: Error): Error;
|
|
16
|
+
//# sourceMappingURL=utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAwB7E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CAOtE"}
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get an error from a response.
|
|
3
|
+
*
|
|
4
|
+
* @param response - The response to get an error from.
|
|
5
|
+
* @returns An error.
|
|
6
|
+
*/
|
|
7
|
+
export async function getErrorFromResponse(response) {
|
|
8
|
+
const contentType = response.headers?.get('content-type');
|
|
9
|
+
const statusCode = response.status;
|
|
10
|
+
try {
|
|
11
|
+
if (contentType?.includes('application/json')) {
|
|
12
|
+
const json = await response.json();
|
|
13
|
+
const errorMessage = json?.error ?? json?.message ?? 'Unknown error';
|
|
14
|
+
const networkError = `error: ${errorMessage}, statusCode: ${statusCode}`;
|
|
15
|
+
return new Error(networkError);
|
|
16
|
+
}
|
|
17
|
+
else if (contentType?.includes('text/plain')) {
|
|
18
|
+
const text = await response.text();
|
|
19
|
+
const networkError = `error: ${text}, statusCode: ${statusCode}`;
|
|
20
|
+
return new Error(networkError);
|
|
21
|
+
}
|
|
22
|
+
const error = 'data' in response && typeof response.data === 'string'
|
|
23
|
+
? response.data
|
|
24
|
+
: 'Unknown error';
|
|
25
|
+
const networkError = `error: ${error}, statusCode: ${statusCode}`;
|
|
26
|
+
return new Error(networkError);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return new Error(`HTTP ${statusCode} error`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates an error instance with a readable message and the root cause.
|
|
34
|
+
*
|
|
35
|
+
* @param message - The error message to create a Sentry error from.
|
|
36
|
+
* @param cause - The inner error to create a Sentry error from.
|
|
37
|
+
* @returns A Sentry error.
|
|
38
|
+
*/
|
|
39
|
+
export function createSentryError(message, cause) {
|
|
40
|
+
const sentryError = new Error(message);
|
|
41
|
+
sentryError.cause = cause;
|
|
42
|
+
return sentryError;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,QAAkB;IAC3D,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;IAC1D,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC;IACnC,IAAI,CAAC;QACH,IAAI,WAAW,EAAE,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC;YAC9C,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,MAAM,YAAY,GAAG,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,OAAO,IAAI,eAAe,CAAC;YACrE,MAAM,YAAY,GAAG,UAAU,YAAY,iBAAiB,UAAU,EAAE,CAAC;YACzE,OAAO,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;QACjC,CAAC;aAAM,IAAI,WAAW,EAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,MAAM,YAAY,GAAG,UAAU,IAAI,iBAAiB,UAAU,EAAE,CAAC;YACjE,OAAO,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;QACjC,CAAC;QAED,MAAM,KAAK,GACT,MAAM,IAAI,QAAQ,IAAI,OAAO,QAAQ,CAAC,IAAI,KAAK,QAAQ;YACrD,CAAC,CAAC,QAAQ,CAAC,IAAI;YACf,CAAC,CAAC,eAAe,CAAC;QACtB,MAAM,YAAY,GAAG,UAAU,KAAK,iBAAiB,UAAU,EAAE,CAAC;QAClE,OAAO,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;IACjC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,KAAK,CAAC,QAAQ,UAAU,QAAQ,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAe,EAAE,KAAY;IAC7D,MAAM,WAAW,GAAG,IAAI,KAAK,CAAC,OAAO,CAEpC,CAAC;IACF,WAAW,CAAC,KAAK,GAAG,KAAK,CAAC;IAE1B,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["/**\n * Get an error from a response.\n *\n * @param response - The response to get an error from.\n * @returns An error.\n */\nexport async function getErrorFromResponse(response: Response): Promise<Error> {\n const contentType = response.headers?.get('content-type');\n const statusCode = response.status;\n try {\n if (contentType?.includes('application/json')) {\n const json = await response.json();\n const errorMessage = json?.error ?? json?.message ?? 'Unknown error';\n const networkError = `error: ${errorMessage}, statusCode: ${statusCode}`;\n return new Error(networkError);\n } else if (contentType?.includes('text/plain')) {\n const text = await response.text();\n const networkError = `error: ${text}, statusCode: ${statusCode}`;\n return new Error(networkError);\n }\n\n const error =\n 'data' in response && typeof response.data === 'string'\n ? response.data\n : 'Unknown error';\n const networkError = `error: ${error}, statusCode: ${statusCode}`;\n return new Error(networkError);\n } catch {\n return new Error(`HTTP ${statusCode} error`);\n }\n}\n\n/**\n * Creates an error instance with a readable message and the root cause.\n *\n * @param message - The error message to create a Sentry error from.\n * @param cause - The inner error to create a Sentry error from.\n * @returns A Sentry error.\n */\nexport function createSentryError(message: string, cause: Error): Error {\n const sentryError = new Error(message) as Error & {\n cause: Error;\n };\n sentryError.cause = cause;\n\n return sentryError;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/claims-controller",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Controller handling shield subscription claims logic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -49,9 +49,9 @@
|
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@metamask/base-controller": "^9.0.0",
|
|
52
|
-
"@metamask/controller-utils": "^11.
|
|
52
|
+
"@metamask/controller-utils": "^11.17.0",
|
|
53
53
|
"@metamask/messenger": "^0.3.0",
|
|
54
|
-
"@metamask/utils": "^11.
|
|
54
|
+
"@metamask/utils": "^11.9.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@metamask/auto-changelog": "^3.4.4",
|