@metamask/claims-controller 0.1.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 +25 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/ClaimsController.cjs +108 -0
- package/dist/ClaimsController.cjs.map +1 -0
- package/dist/ClaimsController.d.cts +48 -0
- package/dist/ClaimsController.d.cts.map +1 -0
- package/dist/ClaimsController.d.mts +48 -0
- package/dist/ClaimsController.d.mts.map +1 -0
- package/dist/ClaimsController.mjs +103 -0
- package/dist/ClaimsController.mjs.map +1 -0
- package/dist/ClaimsService.cjs +115 -0
- package/dist/ClaimsService.cjs.map +1 -0
- package/dist/ClaimsService.d.cts +74 -0
- package/dist/ClaimsService.d.cts.map +1 -0
- package/dist/ClaimsService.d.mts +74 -0
- package/dist/ClaimsService.d.mts.map +1 -0
- package/dist/ClaimsService.mjs +111 -0
- package/dist/ClaimsService.mjs.map +1 -0
- package/dist/constants.cjs +50 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +33 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +33 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +47 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +54 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +54 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +76 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Added new `@metamask/claims-controller` package to handle shield subscription claims logics. ([#7072](https://github.com/MetaMask/core/pull/7072))
|
|
15
|
+
- Implementation of `ClaimsController`. ([#7072](https://github.com/MetaMask/core/pull/7072))
|
|
16
|
+
- `getSubmitClaimConfig`: Generate configurations required for the claim submission.
|
|
17
|
+
- `generateClaimSignature`: Generate signature for the claim submission.
|
|
18
|
+
- Implementation of Data-Service, `ClaimsService`. ([#7072](https://github.com/MetaMask/core/pull/7072))
|
|
19
|
+
- `getClaims`: fetch list of users' claims from the backend.
|
|
20
|
+
- `getClaimById`: fetch single claim by id.
|
|
21
|
+
- `generateMessageForClaimSignature`: generate message to sign for the claim signature.
|
|
22
|
+
- `verifyClaimSignature`: verify claim signature produced by user.
|
|
23
|
+
|
|
24
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/claims-controller@0.1.0...HEAD
|
|
25
|
+
[0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/claims-controller@0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# `@metamask/claims-controller`
|
|
2
|
+
|
|
3
|
+
Controller handling shield subscription claims logic
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/claims-controller`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/claims-controller`
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _ClaimsController_instances, _ClaimsController_validateSubmitClaimRequest;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ClaimsController = exports.getDefaultClaimsControllerState = void 0;
|
|
10
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
11
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
12
|
+
const utils_1 = require("@metamask/utils");
|
|
13
|
+
const constants_1 = require("./constants.cjs");
|
|
14
|
+
const ClaimsControllerStateMetadata = {
|
|
15
|
+
claims: {
|
|
16
|
+
includeInStateLogs: false,
|
|
17
|
+
persist: true,
|
|
18
|
+
includeInDebugSnapshot: false,
|
|
19
|
+
usedInUi: true,
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Get an initial default state for the controller.
|
|
24
|
+
*
|
|
25
|
+
* @returns The initial default controller state.
|
|
26
|
+
*/
|
|
27
|
+
function getDefaultClaimsControllerState() {
|
|
28
|
+
return {
|
|
29
|
+
claims: [],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.getDefaultClaimsControllerState = getDefaultClaimsControllerState;
|
|
33
|
+
class ClaimsController extends base_controller_1.BaseController {
|
|
34
|
+
constructor({ messenger, state }) {
|
|
35
|
+
super({
|
|
36
|
+
messenger,
|
|
37
|
+
metadata: ClaimsControllerStateMetadata,
|
|
38
|
+
name: constants_1.CONTROLLER_NAME,
|
|
39
|
+
state: { ...getDefaultClaimsControllerState(), ...state },
|
|
40
|
+
});
|
|
41
|
+
_ClaimsController_instances.add(this);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get required config for submitting a claim.
|
|
45
|
+
*
|
|
46
|
+
* @param claim - The claim request to get the required config for.
|
|
47
|
+
* @returns The required config for submitting the claim.
|
|
48
|
+
*/
|
|
49
|
+
async getSubmitClaimConfig(claim) {
|
|
50
|
+
// Validate the claim before submitting it.
|
|
51
|
+
__classPrivateFieldGet(this, _ClaimsController_instances, "m", _ClaimsController_validateSubmitClaimRequest).call(this, claim);
|
|
52
|
+
const headers = await this.messenger.call(`${constants_1.SERVICE_NAME}:getRequestHeaders`, constants_1.HttpContentTypeHeader.MULTIPART_FORM_DATA);
|
|
53
|
+
const baseUrl = this.messenger.call(`${constants_1.SERVICE_NAME}:getClaimsApiUrl`);
|
|
54
|
+
const url = `${baseUrl}/claims`;
|
|
55
|
+
return {
|
|
56
|
+
data: claim,
|
|
57
|
+
headers,
|
|
58
|
+
method: 'POST',
|
|
59
|
+
url,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Generate a signature for a claim.
|
|
64
|
+
*
|
|
65
|
+
* @param chainId - The chain id of the claim.
|
|
66
|
+
* @param walletAddress - The impacted wallet address of the claim.
|
|
67
|
+
* @returns The signature for the claim.
|
|
68
|
+
*/
|
|
69
|
+
async generateClaimSignature(chainId, walletAddress) {
|
|
70
|
+
// generate the message to be signed
|
|
71
|
+
const { message } = await this.messenger.call(`${constants_1.SERVICE_NAME}:generateMessageForClaimSignature`, chainId, walletAddress);
|
|
72
|
+
// generate and parse the SIWE message
|
|
73
|
+
const messageBytes = (0, utils_1.stringToBytes)(message);
|
|
74
|
+
const messageHex = (0, utils_1.bytesToHex)(messageBytes);
|
|
75
|
+
const siwe = (0, controller_utils_1.detectSIWE)({ data: messageHex });
|
|
76
|
+
if (!siwe.isSIWEMessage) {
|
|
77
|
+
throw new Error(constants_1.ClaimsControllerErrorMessages.INVALID_SIGNATURE_MESSAGE);
|
|
78
|
+
}
|
|
79
|
+
// sign the message
|
|
80
|
+
const signature = await this.messenger.call('KeyringController:signPersonalMessage', {
|
|
81
|
+
data: message,
|
|
82
|
+
from: walletAddress,
|
|
83
|
+
siwe,
|
|
84
|
+
});
|
|
85
|
+
return signature;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get the list of claims for the current user.
|
|
89
|
+
*
|
|
90
|
+
* @returns The list of claims for the current user.
|
|
91
|
+
*/
|
|
92
|
+
async getClaims() {
|
|
93
|
+
const claims = await this.messenger.call(`${constants_1.SERVICE_NAME}:getClaims`);
|
|
94
|
+
this.update((state) => {
|
|
95
|
+
state.claims = claims;
|
|
96
|
+
});
|
|
97
|
+
return claims;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.ClaimsController = ClaimsController;
|
|
101
|
+
_ClaimsController_instances = new WeakSet(), _ClaimsController_validateSubmitClaimRequest = function _ClaimsController_validateSubmitClaimRequest(claim) {
|
|
102
|
+
const { claims: existingClaims } = this.state;
|
|
103
|
+
const isClaimAlreadySubmitted = existingClaims.some((existingClaim) => existingClaim.impactedTxHash === claim.impactedTxHash);
|
|
104
|
+
if (isClaimAlreadySubmitted) {
|
|
105
|
+
throw new Error(constants_1.ClaimsControllerErrorMessages.CLAIM_ALREADY_SUBMITTED);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
//# sourceMappingURL=ClaimsController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClaimsController.cjs","sourceRoot":"","sources":["../src/ClaimsController.ts"],"names":[],"mappings":";;;;;;;;;AAKA,+DAA2D;AAC3D,iEAAwD;AAGxD,2CAA4D;AAS5D,+CAKqB;AAwCrB,MAAM,6BAA6B,GAAyC;IAC1E,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,MAAM,EAAE,EAAE;KACX,CAAC;AACJ,CAAC;AAJD,0EAIC;AAED,MAAa,gBAAiB,SAAQ,gCAIrC;IACC,YAAY,EAAE,SAAS,EAAE,KAAK,EAA2B;QACvD,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,6BAA6B;YACvC,IAAI,EAAE,2BAAe;YAC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AC,SAAS,CAAC,IAAI,CACzC,uCAAuC,EACvC;YACE,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,aAAa;YACnB,IAAI;SACL,CACF,CAAC;QAEF,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,wBAAY,YAAY,CAAC,CAAC;QACtE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;QACxB,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;CAgBF;AA3GD,4CA2GC;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 } 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 ClaimsServiceGenerateMessageForClaimSignatureAction,\n ClaimsServiceGetClaimByIdAction,\n ClaimsServiceGetClaimsAction,\n ClaimsServiceGetClaimsApiUrlAction,\n ClaimsServiceGetRequestHeadersAction,\n} from './ClaimsService';\nimport {\n ClaimsControllerErrorMessages,\n CONTROLLER_NAME,\n HttpContentTypeHeader,\n SERVICE_NAME,\n} from './constants';\nimport type {\n Claim,\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 | 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};\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 claims: [],\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 * 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 HttpContentTypeHeader.MULTIPART_FORM_DATA,\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 * 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"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import type { KeyringControllerSignPersonalMessageAction } from "@metamask/keyring-controller";
|
|
4
|
+
import type { Messenger } from "@metamask/messenger";
|
|
5
|
+
import type { ClaimsServiceGenerateMessageForClaimSignatureAction, ClaimsServiceGetClaimByIdAction, ClaimsServiceGetClaimsAction, ClaimsServiceGetClaimsApiUrlAction, ClaimsServiceGetRequestHeadersAction } from "./ClaimsService.cjs";
|
|
6
|
+
import { CONTROLLER_NAME } from "./constants.cjs";
|
|
7
|
+
import type { Claim, ClaimsControllerState, CreateClaimRequest, SubmitClaimConfig } from "./types.cjs";
|
|
8
|
+
export type ClaimsControllerGetStateAction = ControllerGetStateAction<typeof CONTROLLER_NAME, ClaimsControllerState>;
|
|
9
|
+
export type ClaimsControllerActions = ClaimsControllerGetStateAction;
|
|
10
|
+
export type AllowedActions = ClaimsServiceGetClaimsAction | ClaimsServiceGetClaimByIdAction | ClaimsServiceGetRequestHeadersAction | ClaimsServiceGetClaimsApiUrlAction | ClaimsServiceGenerateMessageForClaimSignatureAction | ClaimsServiceGetClaimsAction | KeyringControllerSignPersonalMessageAction;
|
|
11
|
+
export type ClaimsControllerStateChangeEvent = ControllerStateChangeEvent<typeof CONTROLLER_NAME, ClaimsControllerState>;
|
|
12
|
+
export type ClaimsControllerMessenger = Messenger<typeof CONTROLLER_NAME, ClaimsControllerActions | AllowedActions, ClaimsControllerStateChangeEvent>;
|
|
13
|
+
export type ClaimsControllerOptions = {
|
|
14
|
+
messenger: ClaimsControllerMessenger;
|
|
15
|
+
state?: Partial<ClaimsControllerState>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Get an initial default state for the controller.
|
|
19
|
+
*
|
|
20
|
+
* @returns The initial default controller state.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getDefaultClaimsControllerState(): ClaimsControllerState;
|
|
23
|
+
export declare class ClaimsController extends BaseController<typeof CONTROLLER_NAME, ClaimsControllerState, ClaimsControllerMessenger> {
|
|
24
|
+
#private;
|
|
25
|
+
constructor({ messenger, state }: ClaimsControllerOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Get required config for submitting a claim.
|
|
28
|
+
*
|
|
29
|
+
* @param claim - The claim request to get the required config for.
|
|
30
|
+
* @returns The required config for submitting the claim.
|
|
31
|
+
*/
|
|
32
|
+
getSubmitClaimConfig(claim: CreateClaimRequest): Promise<SubmitClaimConfig>;
|
|
33
|
+
/**
|
|
34
|
+
* Generate a signature for a claim.
|
|
35
|
+
*
|
|
36
|
+
* @param chainId - The chain id of the claim.
|
|
37
|
+
* @param walletAddress - The impacted wallet address of the claim.
|
|
38
|
+
* @returns The signature for the claim.
|
|
39
|
+
*/
|
|
40
|
+
generateClaimSignature(chainId: number, walletAddress: `0x${string}`): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Get the list of claims for the current user.
|
|
43
|
+
*
|
|
44
|
+
* @returns The list of claims for the current user.
|
|
45
|
+
*/
|
|
46
|
+
getClaims(): Promise<Claim[]>;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=ClaimsController.d.cts.map
|
|
@@ -0,0 +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,mDAAmD,EACnD,+BAA+B,EAC/B,4BAA4B,EAC5B,kCAAkC,EAClC,oCAAoC,EACrC,4BAAwB;AACzB,OAAO,EAEL,eAAe,EAG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GetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import type { KeyringControllerSignPersonalMessageAction } from "@metamask/keyring-controller";
|
|
4
|
+
import type { Messenger } from "@metamask/messenger";
|
|
5
|
+
import type { ClaimsServiceGenerateMessageForClaimSignatureAction, ClaimsServiceGetClaimByIdAction, ClaimsServiceGetClaimsAction, ClaimsServiceGetClaimsApiUrlAction, ClaimsServiceGetRequestHeadersAction } from "./ClaimsService.mjs";
|
|
6
|
+
import { CONTROLLER_NAME } from "./constants.mjs";
|
|
7
|
+
import type { Claim, ClaimsControllerState, CreateClaimRequest, SubmitClaimConfig } from "./types.mjs";
|
|
8
|
+
export type ClaimsControllerGetStateAction = ControllerGetStateAction<typeof CONTROLLER_NAME, ClaimsControllerState>;
|
|
9
|
+
export type ClaimsControllerActions = ClaimsControllerGetStateAction;
|
|
10
|
+
export type AllowedActions = ClaimsServiceGetClaimsAction | ClaimsServiceGetClaimByIdAction | ClaimsServiceGetRequestHeadersAction | ClaimsServiceGetClaimsApiUrlAction | ClaimsServiceGenerateMessageForClaimSignatureAction | ClaimsServiceGetClaimsAction | KeyringControllerSignPersonalMessageAction;
|
|
11
|
+
export type ClaimsControllerStateChangeEvent = ControllerStateChangeEvent<typeof CONTROLLER_NAME, ClaimsControllerState>;
|
|
12
|
+
export type ClaimsControllerMessenger = Messenger<typeof CONTROLLER_NAME, ClaimsControllerActions | AllowedActions, ClaimsControllerStateChangeEvent>;
|
|
13
|
+
export type ClaimsControllerOptions = {
|
|
14
|
+
messenger: ClaimsControllerMessenger;
|
|
15
|
+
state?: Partial<ClaimsControllerState>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Get an initial default state for the controller.
|
|
19
|
+
*
|
|
20
|
+
* @returns The initial default controller state.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getDefaultClaimsControllerState(): ClaimsControllerState;
|
|
23
|
+
export declare class ClaimsController extends BaseController<typeof CONTROLLER_NAME, ClaimsControllerState, ClaimsControllerMessenger> {
|
|
24
|
+
#private;
|
|
25
|
+
constructor({ messenger, state }: ClaimsControllerOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Get required config for submitting a claim.
|
|
28
|
+
*
|
|
29
|
+
* @param claim - The claim request to get the required config for.
|
|
30
|
+
* @returns The required config for submitting the claim.
|
|
31
|
+
*/
|
|
32
|
+
getSubmitClaimConfig(claim: CreateClaimRequest): Promise<SubmitClaimConfig>;
|
|
33
|
+
/**
|
|
34
|
+
* Generate a signature for a claim.
|
|
35
|
+
*
|
|
36
|
+
* @param chainId - The chain id of the claim.
|
|
37
|
+
* @param walletAddress - The impacted wallet address of the claim.
|
|
38
|
+
* @returns The signature for the claim.
|
|
39
|
+
*/
|
|
40
|
+
generateClaimSignature(chainId: number, walletAddress: `0x${string}`): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Get the list of claims for the current user.
|
|
43
|
+
*
|
|
44
|
+
* @returns The list of claims for the current user.
|
|
45
|
+
*/
|
|
46
|
+
getClaims(): Promise<Claim[]>;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=ClaimsController.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClaimsController.d.mts","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,mDAAmD,EACnD,+BAA+B,EAC/B,4BAA4B,EAC5B,kCAAkC,EAClC,oCAAoC,EACrC,4BAAwB;AACzB,OAAO,EAEL,eAAe,EAGhB,wBAAoB;AACrB,OAAO,KAAK,EACV,KAAK,EACL,qBAAqB,EACr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
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _ClaimsController_instances, _ClaimsController_validateSubmitClaimRequest;
|
|
7
|
+
import { BaseController } from "@metamask/base-controller";
|
|
8
|
+
import { detectSIWE } from "@metamask/controller-utils";
|
|
9
|
+
import { bytesToHex, stringToBytes } from "@metamask/utils";
|
|
10
|
+
import { ClaimsControllerErrorMessages, CONTROLLER_NAME, HttpContentTypeHeader, SERVICE_NAME } from "./constants.mjs";
|
|
11
|
+
const ClaimsControllerStateMetadata = {
|
|
12
|
+
claims: {
|
|
13
|
+
includeInStateLogs: false,
|
|
14
|
+
persist: true,
|
|
15
|
+
includeInDebugSnapshot: false,
|
|
16
|
+
usedInUi: true,
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Get an initial default state for the controller.
|
|
21
|
+
*
|
|
22
|
+
* @returns The initial default controller state.
|
|
23
|
+
*/
|
|
24
|
+
export function getDefaultClaimsControllerState() {
|
|
25
|
+
return {
|
|
26
|
+
claims: [],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export class ClaimsController extends BaseController {
|
|
30
|
+
constructor({ messenger, state }) {
|
|
31
|
+
super({
|
|
32
|
+
messenger,
|
|
33
|
+
metadata: ClaimsControllerStateMetadata,
|
|
34
|
+
name: CONTROLLER_NAME,
|
|
35
|
+
state: { ...getDefaultClaimsControllerState(), ...state },
|
|
36
|
+
});
|
|
37
|
+
_ClaimsController_instances.add(this);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get required config for submitting a claim.
|
|
41
|
+
*
|
|
42
|
+
* @param claim - The claim request to get the required config for.
|
|
43
|
+
* @returns The required config for submitting the claim.
|
|
44
|
+
*/
|
|
45
|
+
async getSubmitClaimConfig(claim) {
|
|
46
|
+
// Validate the claim before submitting it.
|
|
47
|
+
__classPrivateFieldGet(this, _ClaimsController_instances, "m", _ClaimsController_validateSubmitClaimRequest).call(this, claim);
|
|
48
|
+
const headers = await this.messenger.call(`${SERVICE_NAME}:getRequestHeaders`, HttpContentTypeHeader.MULTIPART_FORM_DATA);
|
|
49
|
+
const baseUrl = this.messenger.call(`${SERVICE_NAME}:getClaimsApiUrl`);
|
|
50
|
+
const url = `${baseUrl}/claims`;
|
|
51
|
+
return {
|
|
52
|
+
data: claim,
|
|
53
|
+
headers,
|
|
54
|
+
method: 'POST',
|
|
55
|
+
url,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Generate a signature for a claim.
|
|
60
|
+
*
|
|
61
|
+
* @param chainId - The chain id of the claim.
|
|
62
|
+
* @param walletAddress - The impacted wallet address of the claim.
|
|
63
|
+
* @returns The signature for the claim.
|
|
64
|
+
*/
|
|
65
|
+
async generateClaimSignature(chainId, walletAddress) {
|
|
66
|
+
// generate the message to be signed
|
|
67
|
+
const { message } = await this.messenger.call(`${SERVICE_NAME}:generateMessageForClaimSignature`, chainId, walletAddress);
|
|
68
|
+
// generate and parse the SIWE message
|
|
69
|
+
const messageBytes = stringToBytes(message);
|
|
70
|
+
const messageHex = bytesToHex(messageBytes);
|
|
71
|
+
const siwe = detectSIWE({ data: messageHex });
|
|
72
|
+
if (!siwe.isSIWEMessage) {
|
|
73
|
+
throw new Error(ClaimsControllerErrorMessages.INVALID_SIGNATURE_MESSAGE);
|
|
74
|
+
}
|
|
75
|
+
// sign the message
|
|
76
|
+
const signature = await this.messenger.call('KeyringController:signPersonalMessage', {
|
|
77
|
+
data: message,
|
|
78
|
+
from: walletAddress,
|
|
79
|
+
siwe,
|
|
80
|
+
});
|
|
81
|
+
return signature;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get the list of claims for the current user.
|
|
85
|
+
*
|
|
86
|
+
* @returns The list of claims for the current user.
|
|
87
|
+
*/
|
|
88
|
+
async getClaims() {
|
|
89
|
+
const claims = await this.messenger.call(`${SERVICE_NAME}:getClaims`);
|
|
90
|
+
this.update((state) => {
|
|
91
|
+
state.claims = claims;
|
|
92
|
+
});
|
|
93
|
+
return claims;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
_ClaimsController_instances = new WeakSet(), _ClaimsController_validateSubmitClaimRequest = function _ClaimsController_validateSubmitClaimRequest(claim) {
|
|
97
|
+
const { claims: existingClaims } = this.state;
|
|
98
|
+
const isClaimAlreadySubmitted = existingClaims.some((existingClaim) => existingClaim.impactedTxHash === claim.impactedTxHash);
|
|
99
|
+
if (isClaimAlreadySubmitted) {
|
|
100
|
+
throw new Error(ClaimsControllerErrorMessages.CLAIM_ALREADY_SUBMITTED);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
//# sourceMappingURL=ClaimsController.mjs.map
|
|
@@ -0,0 +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,mCAAmC;AAGxD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,wBAAwB;AAS5D,OAAO,EACL,6BAA6B,EAC7B,eAAe,EACf,qBAAqB,EACrB,YAAY,EACb,wBAAoB;AAwCrB,MAAM,6BAA6B,GAAyC;IAC1E,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,MAAM,EAAE,EAAE;KACX,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,gBAAiB,SAAQ,cA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AAC,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;QAC3E,CAAC;QAED,mBAAmB;QACnB,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACzC,uCAAuC,EACvC;YACE,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,aAAa;YACnB,IAAI;SACL,CACF,CAAC;QAEF,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,YAAY,YAAY,CAAC,CAAC;QACtE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;QACxB,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,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 } 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 ClaimsServiceGenerateMessageForClaimSignatureAction,\n ClaimsServiceGetClaimByIdAction,\n ClaimsServiceGetClaimsAction,\n ClaimsServiceGetClaimsApiUrlAction,\n ClaimsServiceGetRequestHeadersAction,\n} from './ClaimsService';\nimport {\n ClaimsControllerErrorMessages,\n CONTROLLER_NAME,\n HttpContentTypeHeader,\n SERVICE_NAME,\n} from './constants';\nimport type {\n Claim,\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 | 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};\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 claims: [],\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 * 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 HttpContentTypeHeader.MULTIPART_FORM_DATA,\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 * 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"]}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _ClaimsService_env, _ClaimsService_fetch, _ClaimsService_messenger;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ClaimsService = void 0;
|
|
16
|
+
const constants_1 = require("./constants.cjs");
|
|
17
|
+
class ClaimsService {
|
|
18
|
+
constructor({ env, messenger, fetchFunction }) {
|
|
19
|
+
this.name = constants_1.SERVICE_NAME; // required for Modular Initialization
|
|
20
|
+
_ClaimsService_env.set(this, void 0);
|
|
21
|
+
_ClaimsService_fetch.set(this, void 0);
|
|
22
|
+
_ClaimsService_messenger.set(this, void 0);
|
|
23
|
+
__classPrivateFieldSet(this, _ClaimsService_env, env, "f");
|
|
24
|
+
__classPrivateFieldSet(this, _ClaimsService_messenger, messenger, "f");
|
|
25
|
+
__classPrivateFieldSet(this, _ClaimsService_fetch, fetchFunction, "f");
|
|
26
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").registerActionHandler(`${constants_1.SERVICE_NAME}:getClaims`, this.getClaims.bind(this));
|
|
27
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").registerActionHandler(`${constants_1.SERVICE_NAME}:getClaimById`, this.getClaimById.bind(this));
|
|
28
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").registerActionHandler(`${constants_1.SERVICE_NAME}:getRequestHeaders`, this.getRequestHeaders.bind(this));
|
|
29
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").registerActionHandler(`${constants_1.SERVICE_NAME}:getClaimsApiUrl`, this.getClaimsApiUrl.bind(this));
|
|
30
|
+
__classPrivateFieldGet(this, _ClaimsService_messenger, "f").registerActionHandler(`${constants_1.SERVICE_NAME}:generateMessageForClaimSignature`, this.generateMessageForClaimSignature.bind(this));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the claims for the current user.
|
|
34
|
+
*
|
|
35
|
+
* @returns The claims for the current user.
|
|
36
|
+
*/
|
|
37
|
+
async getClaims() {
|
|
38
|
+
const headers = await this.getRequestHeaders();
|
|
39
|
+
const url = `${this.getClaimsApiUrl()}/claims`;
|
|
40
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
41
|
+
headers,
|
|
42
|
+
});
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
throw new Error(constants_1.ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIMS);
|
|
45
|
+
}
|
|
46
|
+
const claims = await response.json();
|
|
47
|
+
return claims;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get the claim by id.
|
|
51
|
+
*
|
|
52
|
+
* @param id - The id of the claim to get.
|
|
53
|
+
* @returns The claim by id.
|
|
54
|
+
*/
|
|
55
|
+
async getClaimById(id) {
|
|
56
|
+
const headers = await this.getRequestHeaders();
|
|
57
|
+
const url = `${this.getClaimsApiUrl()}/claims/byId/${id}`;
|
|
58
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
59
|
+
headers,
|
|
60
|
+
});
|
|
61
|
+
if (!response.ok) {
|
|
62
|
+
throw new Error(constants_1.ClaimsServiceErrorMessages.FAILED_TO_GET_CLAIM_BY_ID);
|
|
63
|
+
}
|
|
64
|
+
const claim = await response.json();
|
|
65
|
+
return claim;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Generate a message to be signed by the user for the claim request.
|
|
69
|
+
*
|
|
70
|
+
* @param chainId - The chain id of the claim.
|
|
71
|
+
* @param walletAddress - The impacted wallet address of the claim.
|
|
72
|
+
* @returns The message for the claim signature.
|
|
73
|
+
*/
|
|
74
|
+
async generateMessageForClaimSignature(chainId, walletAddress) {
|
|
75
|
+
const headers = await this.getRequestHeaders();
|
|
76
|
+
const url = `${this.getClaimsApiUrl()}/signature/generateMessage`;
|
|
77
|
+
const response = await __classPrivateFieldGet(this, _ClaimsService_fetch, "f").call(this, url, {
|
|
78
|
+
headers,
|
|
79
|
+
method: 'POST',
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
chainId,
|
|
82
|
+
walletAddress,
|
|
83
|
+
}),
|
|
84
|
+
});
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
throw new Error(constants_1.ClaimsServiceErrorMessages.SIGNATURE_MESSAGE_GENERATION_FAILED);
|
|
87
|
+
}
|
|
88
|
+
const message = await response.json();
|
|
89
|
+
return message;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create the headers for the current request.
|
|
93
|
+
*
|
|
94
|
+
* @param contentType - The content type of the request. Defaults to 'application/json'.
|
|
95
|
+
* @returns The headers for the current request.
|
|
96
|
+
*/
|
|
97
|
+
async getRequestHeaders(contentType = constants_1.HttpContentTypeHeader.APPLICATION_JSON) {
|
|
98
|
+
const bearerToken = await __classPrivateFieldGet(this, _ClaimsService_messenger, "f").call('AuthenticationController:getBearerToken');
|
|
99
|
+
return {
|
|
100
|
+
Authorization: `Bearer ${bearerToken}`,
|
|
101
|
+
'Content-Type': contentType,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get the URL for the claims API for the current environment.
|
|
106
|
+
*
|
|
107
|
+
* @returns The URL for the claims API for the current environment.
|
|
108
|
+
*/
|
|
109
|
+
getClaimsApiUrl() {
|
|
110
|
+
return `${constants_1.CLAIMS_API_URL[__classPrivateFieldGet(this, _ClaimsService_env, "f")]}`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.ClaimsService = ClaimsService;
|
|
114
|
+
_ClaimsService_env = new WeakMap(), _ClaimsService_fetch = new WeakMap(), _ClaimsService_messenger = new WeakMap();
|
|
115
|
+
//# sourceMappingURL=ClaimsService.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClaimsService.cjs","sourceRoot":"","sources":["../src/ClaimsService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,+CAMqB;AAmDrB,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,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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I,0BAAK,CAAC,EAAE,CAAC;IACxC,CAAC;CACF;AAvID,sCAuIC","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,\n ClaimsServiceErrorMessages,\n type Env,\n HttpContentTypeHeader,\n SERVICE_NAME,\n} from './constants';\nimport type { Claim, GenerateSignatureMessageResponse } from './types';\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 | 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}: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 * 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 headers,\n method: 'POST',\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 * @param contentType - The content type of the request. Defaults to 'application/json'.\n * @returns The headers for the current request.\n */\n async getRequestHeaders(\n contentType: HttpContentTypeHeader = HttpContentTypeHeader.APPLICATION_JSON,\n ): Promise<Record<string, string>> {\n const bearerToken = await this.#messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n return {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': contentType,\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[this.#env]}`;\n }\n}\n"]}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Messenger } from "@metamask/messenger";
|
|
2
|
+
import type { AuthenticationController } from "@metamask/profile-sync-controller";
|
|
3
|
+
import type { Hex } from "@metamask/utils";
|
|
4
|
+
import { type Env, HttpContentTypeHeader, SERVICE_NAME } from "./constants.cjs";
|
|
5
|
+
import type { Claim, GenerateSignatureMessageResponse } from "./types.cjs";
|
|
6
|
+
export type ClaimsServiceGetClaimsAction = {
|
|
7
|
+
type: `${typeof SERVICE_NAME}:getClaims`;
|
|
8
|
+
handler: ClaimsService['getClaims'];
|
|
9
|
+
};
|
|
10
|
+
export type ClaimsServiceGetClaimByIdAction = {
|
|
11
|
+
type: `${typeof SERVICE_NAME}:getClaimById`;
|
|
12
|
+
handler: ClaimsService['getClaimById'];
|
|
13
|
+
};
|
|
14
|
+
export type ClaimsServiceGetRequestHeadersAction = {
|
|
15
|
+
type: `${typeof SERVICE_NAME}:getRequestHeaders`;
|
|
16
|
+
handler: ClaimsService['getRequestHeaders'];
|
|
17
|
+
};
|
|
18
|
+
export type ClaimsServiceGetClaimsApiUrlAction = {
|
|
19
|
+
type: `${typeof SERVICE_NAME}:getClaimsApiUrl`;
|
|
20
|
+
handler: ClaimsService['getClaimsApiUrl'];
|
|
21
|
+
};
|
|
22
|
+
export type ClaimsServiceGenerateMessageForClaimSignatureAction = {
|
|
23
|
+
type: `${typeof SERVICE_NAME}:generateMessageForClaimSignature`;
|
|
24
|
+
handler: ClaimsService['generateMessageForClaimSignature'];
|
|
25
|
+
};
|
|
26
|
+
export type ClaimsServiceActions = ClaimsServiceGetClaimsAction | ClaimsServiceGetClaimByIdAction | ClaimsServiceGetRequestHeadersAction | ClaimsServiceGetClaimsApiUrlAction | ClaimsServiceGenerateMessageForClaimSignatureAction;
|
|
27
|
+
export type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken;
|
|
28
|
+
export type ClaimsServiceEvents = never;
|
|
29
|
+
export type ClaimsServiceMessenger = Messenger<typeof SERVICE_NAME, ClaimsServiceActions | AllowedActions>;
|
|
30
|
+
export type ClaimsServiceConfig = {
|
|
31
|
+
env: Env;
|
|
32
|
+
messenger: ClaimsServiceMessenger;
|
|
33
|
+
fetchFunction: typeof fetch;
|
|
34
|
+
};
|
|
35
|
+
export declare class ClaimsService {
|
|
36
|
+
#private;
|
|
37
|
+
readonly name = "ClaimsService";
|
|
38
|
+
constructor({ env, messenger, fetchFunction }: ClaimsServiceConfig);
|
|
39
|
+
/**
|
|
40
|
+
* Get the claims for the current user.
|
|
41
|
+
*
|
|
42
|
+
* @returns The claims for the current user.
|
|
43
|
+
*/
|
|
44
|
+
getClaims(): Promise<Claim[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Get the claim by id.
|
|
47
|
+
*
|
|
48
|
+
* @param id - The id of the claim to get.
|
|
49
|
+
* @returns The claim by id.
|
|
50
|
+
*/
|
|
51
|
+
getClaimById(id: string): Promise<Claim>;
|
|
52
|
+
/**
|
|
53
|
+
* Generate a message to be signed by the user for the claim request.
|
|
54
|
+
*
|
|
55
|
+
* @param chainId - The chain id of the claim.
|
|
56
|
+
* @param walletAddress - The impacted wallet address of the claim.
|
|
57
|
+
* @returns The message for the claim signature.
|
|
58
|
+
*/
|
|
59
|
+
generateMessageForClaimSignature(chainId: number, walletAddress: Hex): Promise<GenerateSignatureMessageResponse>;
|
|
60
|
+
/**
|
|
61
|
+
* Create the headers for the current request.
|
|
62
|
+
*
|
|
63
|
+
* @param contentType - The content type of the request. Defaults to 'application/json'.
|
|
64
|
+
* @returns The headers for the current request.
|
|
65
|
+
*/
|
|
66
|
+
getRequestHeaders(contentType?: HttpContentTypeHeader): Promise<Record<string, string>>;
|
|
67
|
+
/**
|
|
68
|
+
* Get the URL for the claims API for the current environment.
|
|
69
|
+
*
|
|
70
|
+
* @returns The URL for the claims API for the current environment.
|
|
71
|
+
*/
|
|
72
|
+
getClaimsApiUrl(): string;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=ClaimsService.d.cts.map
|
|
@@ -0,0 +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,KAAK,GAAG,EACR,qBAAqB,EACrB,YAAY,EACb,wBAAoB;AACrB,OAAO,KAAK,EAAE,KAAK,EAAE,gCAAgC,EAAE,oBAAgB;AAEvE,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,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