@tari-project/metamask-signer 0.5.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/LICENSE +29 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +172 -0
- package/dist/utils.d.ts +38 -0
- package/dist/utils.js +55 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, The Tari Developer Community
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { TariSigner } from "@tari-project/tari-signer";
|
|
2
|
+
import { SubmitTransactionRequest, TransactionResult, SubmitTransactionResponse, VaultBalances, TemplateDefinition, Substate, ListSubstatesResponse, Account } from "@tari-project/tari-signer";
|
|
3
|
+
import { MetaMaskInpageProvider } from "@metamask/providers";
|
|
4
|
+
import { Snap } from "./utils";
|
|
5
|
+
import { ListAccountNftRequest, ListAccountNftResponse, SubstateType } from "@tari-project/typescript-bindings";
|
|
6
|
+
export declare const MetamaskNotInstalled = "METAMASK_NOT_INSTALLED";
|
|
7
|
+
export declare const MetamaskIsNotFlask = "METAMASK_IS_NOT_FLASK";
|
|
8
|
+
export declare const TariSnapNotInstalled = "TARI_SNAP_NOT_INSTALLED";
|
|
9
|
+
export declare class MetamaskTariSigner implements TariSigner {
|
|
10
|
+
signerName: string;
|
|
11
|
+
snapId: string;
|
|
12
|
+
snapVersion: string | undefined;
|
|
13
|
+
metamask: MetaMaskInpageProvider;
|
|
14
|
+
snap?: Snap;
|
|
15
|
+
metamaskConnected: boolean;
|
|
16
|
+
constructor(snapId: string, metamask: MetaMaskInpageProvider);
|
|
17
|
+
connect(): Promise<void>;
|
|
18
|
+
isConnected(): boolean;
|
|
19
|
+
createFreeTestCoins(account_id: number): Promise<Account>;
|
|
20
|
+
getAccount(): Promise<Account>;
|
|
21
|
+
getSubstate(substate_address: string): Promise<Substate>;
|
|
22
|
+
listSubstates(filter_by_template: string | null, filter_by_type: SubstateType | null, limit: number | null, offset: number | null): Promise<ListSubstatesResponse>;
|
|
23
|
+
submitTransaction(req: SubmitTransactionRequest): Promise<SubmitTransactionResponse>;
|
|
24
|
+
getTransactionResult(transactionId: string): Promise<TransactionResult>;
|
|
25
|
+
getPublicKey(_branch: string, index: number): Promise<string>;
|
|
26
|
+
getConfidentialVaultBalances(viewKeyId: number, vaultId: string, min?: number | null, max?: number | null): Promise<VaultBalances>;
|
|
27
|
+
getTemplateDefinition(template_address: string): Promise<TemplateDefinition>;
|
|
28
|
+
private metamaskRequest;
|
|
29
|
+
getNftsList(req: ListAccountNftRequest): Promise<ListAccountNftResponse>;
|
|
30
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { TransactionStatus, } from "@tari-project/tari-signer";
|
|
2
|
+
import { connectSnap, getSnap, isFlask } from "./utils";
|
|
3
|
+
export const MetamaskNotInstalled = "METAMASK_NOT_INSTALLED";
|
|
4
|
+
export const MetamaskIsNotFlask = "METAMASK_IS_NOT_FLASK";
|
|
5
|
+
export const TariSnapNotInstalled = "TARI_SNAP_NOT_INSTALLED";
|
|
6
|
+
export class MetamaskTariSigner {
|
|
7
|
+
signerName = "Metamask";
|
|
8
|
+
snapId;
|
|
9
|
+
snapVersion;
|
|
10
|
+
metamask;
|
|
11
|
+
snap;
|
|
12
|
+
metamaskConnected;
|
|
13
|
+
constructor(snapId, metamask) {
|
|
14
|
+
this.snapId = snapId;
|
|
15
|
+
this.snapVersion = undefined;
|
|
16
|
+
this.metamask = metamask;
|
|
17
|
+
this.metamaskConnected = false;
|
|
18
|
+
}
|
|
19
|
+
async connect() {
|
|
20
|
+
// check that the metamask provider is valid
|
|
21
|
+
if (!this.metamask || !this.metamask.isMetaMask) {
|
|
22
|
+
throw MetamaskNotInstalled;
|
|
23
|
+
}
|
|
24
|
+
// check that flask is installed
|
|
25
|
+
if (!isFlask(this.metamask)) {
|
|
26
|
+
throw MetamaskIsNotFlask;
|
|
27
|
+
}
|
|
28
|
+
// connect to the tari snap
|
|
29
|
+
// this will request MetaMask the installation of the tari snap if it's not already installed
|
|
30
|
+
await connectSnap(this.metamask, { [this.snapId]: { version: this.snapVersion } });
|
|
31
|
+
// store the tari snap reference
|
|
32
|
+
const snap = await getSnap(this.metamask, this.snapId);
|
|
33
|
+
if (!snap) {
|
|
34
|
+
// this should olny happen if the user didn't accept the tari snap in the previous step
|
|
35
|
+
throw TariSnapNotInstalled;
|
|
36
|
+
}
|
|
37
|
+
this.snap = snap;
|
|
38
|
+
this.metamaskConnected = true;
|
|
39
|
+
}
|
|
40
|
+
isConnected() {
|
|
41
|
+
return this.metamaskConnected;
|
|
42
|
+
}
|
|
43
|
+
async createFreeTestCoins(account_id) {
|
|
44
|
+
const res = (await this.metamaskRequest("getFreeTestCoins", {
|
|
45
|
+
amount: 1000000,
|
|
46
|
+
account_id,
|
|
47
|
+
fee: 2000,
|
|
48
|
+
}));
|
|
49
|
+
return {
|
|
50
|
+
account_id,
|
|
51
|
+
address: res.address,
|
|
52
|
+
public_key: res.public_key,
|
|
53
|
+
resources: [],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async getAccount() {
|
|
57
|
+
return (await this.metamaskRequest("getAccountData", { account_id: 0 }));
|
|
58
|
+
}
|
|
59
|
+
async getSubstate(substate_address) {
|
|
60
|
+
const { substate, address: substate_id, version, } = await this.metamaskRequest("getSubstate", { substate_address });
|
|
61
|
+
return { value: substate, address: { substate_id, version } };
|
|
62
|
+
}
|
|
63
|
+
async listSubstates(filter_by_template, filter_by_type, limit, offset) {
|
|
64
|
+
const res = (await this.metamaskRequest("listSubstates", {
|
|
65
|
+
filter_by_template,
|
|
66
|
+
filter_by_type,
|
|
67
|
+
limit,
|
|
68
|
+
offset,
|
|
69
|
+
}));
|
|
70
|
+
return res;
|
|
71
|
+
}
|
|
72
|
+
async submitTransaction(req) {
|
|
73
|
+
const params = {
|
|
74
|
+
instructions: req.instructions,
|
|
75
|
+
fee_instructions: req.fee_instructions,
|
|
76
|
+
input_refs: req.input_refs,
|
|
77
|
+
required_substates: req.required_substates || [],
|
|
78
|
+
is_dry_run: req.is_dry_run,
|
|
79
|
+
};
|
|
80
|
+
const resp = await this.metamaskRequest("sendTransaction", params);
|
|
81
|
+
if (!resp) {
|
|
82
|
+
throw new Error("Failed to submit transaction to metamask snap: empty response");
|
|
83
|
+
}
|
|
84
|
+
if (resp.error) {
|
|
85
|
+
throw new Error(`Failed to submit transaction to metamask snap: ${resp.error}`);
|
|
86
|
+
}
|
|
87
|
+
return { transaction_id: resp.transaction_id };
|
|
88
|
+
}
|
|
89
|
+
async getTransactionResult(transactionId) {
|
|
90
|
+
// This request returns the response from the indexer get_transaction_result request
|
|
91
|
+
const resp = await this.metamaskRequest("getTransactionResult", { transaction_id: transactionId });
|
|
92
|
+
if (!resp) {
|
|
93
|
+
throw new Error("Failed to get transaction result from metamask snap: empty response");
|
|
94
|
+
}
|
|
95
|
+
if (resp.result === "Pending") {
|
|
96
|
+
return {
|
|
97
|
+
transaction_id: transactionId,
|
|
98
|
+
status: TransactionStatus.Pending,
|
|
99
|
+
result: null,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (!resp?.result?.Finalized) {
|
|
103
|
+
throw new Error("Transaction result was not pending nor finalized");
|
|
104
|
+
}
|
|
105
|
+
const newStatus = convertToStatus(resp.result.Finalized);
|
|
106
|
+
return {
|
|
107
|
+
transaction_id: transactionId,
|
|
108
|
+
status: newStatus,
|
|
109
|
+
result: resp.result.Finalized.execution_result.finalize,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async getPublicKey(_branch, index) {
|
|
113
|
+
const resp = await this.metamaskRequest("getPublicKey", { index });
|
|
114
|
+
if (!resp) {
|
|
115
|
+
throw new Error("Failed to get public key from metamask snap: empty response");
|
|
116
|
+
}
|
|
117
|
+
return resp.public_key;
|
|
118
|
+
}
|
|
119
|
+
async getConfidentialVaultBalances(viewKeyId, vaultId, min = null, max = null) {
|
|
120
|
+
const res = (await this.metamaskRequest("getConfidentialVaultBalances", {
|
|
121
|
+
view_key_id: viewKeyId,
|
|
122
|
+
vault_id: vaultId,
|
|
123
|
+
minimum_expected_value: min,
|
|
124
|
+
maximum_expected_value: max,
|
|
125
|
+
}));
|
|
126
|
+
return { balances: res };
|
|
127
|
+
}
|
|
128
|
+
getTemplateDefinition(template_address) {
|
|
129
|
+
return this.metamaskRequest("getTemplateDefinition", { template_address }).then((resp) => {
|
|
130
|
+
if (!resp) {
|
|
131
|
+
throw new Error("Template not found");
|
|
132
|
+
}
|
|
133
|
+
return resp.definition;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
async metamaskRequest(method, params) {
|
|
137
|
+
console.log("Metamask request:", method, params);
|
|
138
|
+
const resp = await this.metamask.request({
|
|
139
|
+
method: "wallet_invokeSnap",
|
|
140
|
+
params: {
|
|
141
|
+
snapId: this.snapId,
|
|
142
|
+
request: {
|
|
143
|
+
method,
|
|
144
|
+
params,
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
console.log("Metamask response:", resp);
|
|
149
|
+
if (!resp) {
|
|
150
|
+
throw new Error("Metamask request failed: empty response");
|
|
151
|
+
}
|
|
152
|
+
return resp;
|
|
153
|
+
}
|
|
154
|
+
async getNftsList(req) {
|
|
155
|
+
const resp = (await this.metamaskRequest("getNftsList", req));
|
|
156
|
+
return resp;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function convertToStatus(result) {
|
|
160
|
+
// Ref: https://github.com/tari-project/tari-dan/blob/bb0b31139b770aacd7bb49af865543aa4a9e2de4/dan_layer/wallet/sdk/src/apis/transaction.rs
|
|
161
|
+
if (result.final_decision !== "Commit") {
|
|
162
|
+
return TransactionStatus.Rejected;
|
|
163
|
+
}
|
|
164
|
+
// if (!result?.result?.Finalized) {
|
|
165
|
+
// throw new Error("Transaction result was finalized but no result was returned");
|
|
166
|
+
// }
|
|
167
|
+
//
|
|
168
|
+
// if (result.finalize.AcceptFeeRejectRest) {
|
|
169
|
+
// return TransactionStatus.OnlyFeeAccepted;
|
|
170
|
+
// }
|
|
171
|
+
return TransactionStatus.Accepted;
|
|
172
|
+
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { MetaMaskInpageProvider } from '@metamask/providers';
|
|
2
|
+
export type GetSnapsResponse = Record<string, Snap>;
|
|
3
|
+
export type Snap = {
|
|
4
|
+
permissionName: string;
|
|
5
|
+
id: string;
|
|
6
|
+
version: string;
|
|
7
|
+
initialPermissions: Record<string, unknown>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Get the installed snaps in MetaMask.
|
|
11
|
+
*
|
|
12
|
+
* @param provider - The MetaMask inpage provider.
|
|
13
|
+
* @returns The snaps installed in MetaMask.
|
|
14
|
+
*/
|
|
15
|
+
export declare const getSnaps: (provider: MetaMaskInpageProvider) => Promise<GetSnapsResponse>;
|
|
16
|
+
/**
|
|
17
|
+
* Connect a snap to MetaMask.
|
|
18
|
+
*
|
|
19
|
+
* @param snapId - The ID of the snap.
|
|
20
|
+
* @param params - The params to pass with the snap to connect.
|
|
21
|
+
*/
|
|
22
|
+
export declare const connectSnap: (provider: MetaMaskInpageProvider, snaps: Record<string, {
|
|
23
|
+
version?: string;
|
|
24
|
+
}>) => Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Get the snap from MetaMask.
|
|
27
|
+
*
|
|
28
|
+
* @param version - The version of the snap to install (optional).
|
|
29
|
+
* @returns The snap object returned by the extension.
|
|
30
|
+
*/
|
|
31
|
+
export declare const getSnap: (provider: MetaMaskInpageProvider, snapId: string, version?: string) => Promise<Snap | undefined>;
|
|
32
|
+
export declare const isLocalSnap: (snapId: string) => boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Detect if the wallet injecting the ethereum object is MetaMask Flask.
|
|
35
|
+
*
|
|
36
|
+
* @returns True if the MetaMask version is Flask, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export declare const isFlask: (provider: MetaMaskInpageProvider) => Promise<boolean>;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the installed snaps in MetaMask.
|
|
3
|
+
*
|
|
4
|
+
* @param provider - The MetaMask inpage provider.
|
|
5
|
+
* @returns The snaps installed in MetaMask.
|
|
6
|
+
*/
|
|
7
|
+
export const getSnaps = async (provider) => (await provider.request({
|
|
8
|
+
method: 'wallet_getSnaps',
|
|
9
|
+
}));
|
|
10
|
+
/**
|
|
11
|
+
* Connect a snap to MetaMask.
|
|
12
|
+
*
|
|
13
|
+
* @param snapId - The ID of the snap.
|
|
14
|
+
* @param params - The params to pass with the snap to connect.
|
|
15
|
+
*/
|
|
16
|
+
export const connectSnap = async (provider, snaps) => {
|
|
17
|
+
await provider.request({
|
|
18
|
+
method: 'wallet_requestSnaps',
|
|
19
|
+
params: snaps,
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Get the snap from MetaMask.
|
|
24
|
+
*
|
|
25
|
+
* @param version - The version of the snap to install (optional).
|
|
26
|
+
* @returns The snap object returned by the extension.
|
|
27
|
+
*/
|
|
28
|
+
export const getSnap = async (provider, snapId, version) => {
|
|
29
|
+
try {
|
|
30
|
+
const snaps = await getSnaps(provider);
|
|
31
|
+
return Object.values(snaps).find((snap) => snap.id === snapId && (!version || snap.version === version));
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.log('Failed to obtain installed snap', e);
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
export const isLocalSnap = (snapId) => snapId.startsWith('local:');
|
|
39
|
+
/**
|
|
40
|
+
* Detect if the wallet injecting the ethereum object is MetaMask Flask.
|
|
41
|
+
*
|
|
42
|
+
* @returns True if the MetaMask version is Flask, false otherwise.
|
|
43
|
+
*/
|
|
44
|
+
export const isFlask = async (provider) => {
|
|
45
|
+
try {
|
|
46
|
+
const clientVersion = await provider.request({
|
|
47
|
+
method: 'web3_clientVersion',
|
|
48
|
+
});
|
|
49
|
+
const isFlaskDetected = clientVersion?.includes('flask');
|
|
50
|
+
return Boolean(provider && isFlaskDetected);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tari-project/metamask-signer",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@metamask/providers": "^18.2.0",
|
|
14
|
+
"@tari-project/typescript-bindings": "^1.5.1",
|
|
15
|
+
"@tari-project/tari-signer": "^0.5.0",
|
|
16
|
+
"@tari-project/tarijs-types": "^0.5.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^22.13.1",
|
|
20
|
+
"typescript": "^5.0.4"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"/dist"
|
|
24
|
+
],
|
|
25
|
+
"main": "dist/index.js",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -b"
|
|
29
|
+
}
|
|
30
|
+
}
|