@tari-project/tarijs 0.1.20
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/README.md +46 -0
- package/dist/index.js +1063 -0
- package/dist/index.js.map +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/index.d.ts +111 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/helpers/helpers.d.ts +11 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/AccessRule.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Account.d.ts +7 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Amount.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Arg.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ArgDef.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/AuthHook.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Block.d.ts +31 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/BucketId.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Claims.d.ts +7 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Command.d.ts +13 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Committee.d.ts +3 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/CommitteeInfo.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/CommitteeShardInfo.d.ts +10 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ComponentAccessRules.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ComponentAddress.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ComponentBody.d.ts +3 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ComponentHeader.d.ts +13 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ComponentKey.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ConfidentialClaim.d.ts +8 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ConfidentialOutput.d.ts +8 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ConfidentialOutputStatement.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ConfidentialStatement.d.ts +8 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ConfidentialTransferInputSelection.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ConfidentialWithdrawProof.d.ts +7 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Decision.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ElgamalVerifiableBalance.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/EntityId.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Epoch.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Event.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Evidence.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ExecuteResult.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ExecutedTransaction.d.ts +21 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FeeBreakdown.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FeeClaim.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FeeClaimAddress.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FeeCostBreakdown.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FeeReceipt.d.ts +7 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FeeSource.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FinalizeResult.d.ts +13 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ForeignProposal.d.ts +10 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ForeignProposalState.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/FunctionDef.d.ts +8 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/IndexedValue.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/IndexedWellKnownTypes.d.ts +18 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Instruction.d.ts +40 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/InstructionResult.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/JrpcPermission.d.ts +14 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/JrpcPermissions.d.ts +2 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/LeaderFee.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/LockFlag.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/LogEntry.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/LogLevel.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Metadata.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NetworkCommitteeInfo.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NodeHeight.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungible.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungibleAddress.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungibleAddressContents.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungibleContainer.d.ts +2 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungibleId.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungibleIndex.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungibleIndexAddress.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/NonFungibleToken.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Ordering.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/OwnerRule.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/PeerAddress.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ProofId.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/QuorumCertificate.d.ts +15 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/QuorumDecision.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/RejectReason.d.ts +11 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/RequireRule.d.ts +8 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Resource.d.ts +16 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ResourceAccessRules.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ResourceAddress.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ResourceContainer.d.ts +25 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ResourceType.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/RestrictedAccessRule.d.ts +8 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/RuleRequirement.d.ts +12 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Shard.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ShardEvidence.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Substate.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateAddress.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateDestroyed.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateDiff.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateId.d.ts +24 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateLockFlag.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateRecord.d.ts +19 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateRequirement.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateType.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/SubstateValue.d.ts +25 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TemplateDef.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TemplateDefV1.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Transaction.d.ts +15 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionAtom.d.ts +10 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionPoolRecord.d.ts +16 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionPoolStage.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionReceipt.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionReceiptAddress.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionResult.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionSignature.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/TransactionStatus.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Type.d.ts +9 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/UnclaimedConfidentialOutput.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/UnsignedTransaction.d.ts +10 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ValidatorSignature.d.ts +7 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/Vault.d.ts +4 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/VaultId.d.ts +1 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/VersionedSubstateId.d.ts +5 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/VersionedSubstateIdLockIntent.d.ts +6 -0
- package/dist/node_modules/@tari-project/typescript-bindings/src/types/ViewableBalanceProof.d.ts +10 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/providers/index.d.ts +16 -0
- package/dist/src/providers/metamask/index.d.ts +30 -0
- package/dist/src/providers/metamask/utils.d.ts +38 -0
- package/dist/src/providers/tari_universe/index.d.ts +3 -0
- package/dist/src/providers/tari_universe/provider.d.ts +23 -0
- package/dist/src/providers/tari_universe/types.d.ts +30 -0
- package/dist/src/providers/types.d.ts +65 -0
- package/dist/src/providers/wallet_daemon/index.d.ts +3 -0
- package/dist/src/providers/wallet_daemon/provider.d.ts +35 -0
- package/dist/src/providers/wallet_daemon/tari_permissions.d.ts +216 -0
- package/dist/src/providers/wallet_daemon/webrtc.d.ts +20 -0
- package/dist/src/providers/wallet_daemon/webrtc_transport.d.ts +10 -0
- package/dist/src/utils.d.ts +2 -0
- package/package.json +30 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1063 @@
|
|
|
1
|
+
export { MetaMaskInpageProvider } from '@metamask/providers';
|
|
2
|
+
import { WalletDaemonClient, stringToSubstateId, substateIdToString } from '@tari-project/wallet_jrpc_client';
|
|
3
|
+
|
|
4
|
+
function toHexString(byteArray) {
|
|
5
|
+
if (Array.isArray(byteArray)) {
|
|
6
|
+
return Array.from(byteArray, function (byte) {
|
|
7
|
+
return ('0' + (byte & 0xff).toString(16)).slice(-2);
|
|
8
|
+
}).join('');
|
|
9
|
+
}
|
|
10
|
+
if (byteArray === undefined) {
|
|
11
|
+
return 'undefined';
|
|
12
|
+
}
|
|
13
|
+
// object might be a tagged object
|
|
14
|
+
if (byteArray['@@TAGGED@@'] !== undefined) {
|
|
15
|
+
return toHexString(byteArray['@@TAGGED@@'][1]);
|
|
16
|
+
}
|
|
17
|
+
return 'Unsupported type';
|
|
18
|
+
}
|
|
19
|
+
function fromHexString(hexString) {
|
|
20
|
+
let res = [];
|
|
21
|
+
for (let i = 0; i < hexString.length; i += 2) {
|
|
22
|
+
res.push(Number('0x' + hexString.substring(i, i + 2)));
|
|
23
|
+
}
|
|
24
|
+
return res;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var utils = /*#__PURE__*/Object.freeze({
|
|
28
|
+
__proto__: null,
|
|
29
|
+
fromHexString: fromHexString,
|
|
30
|
+
toHexString: toHexString
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
var TransactionStatus;
|
|
34
|
+
(function (TransactionStatus) {
|
|
35
|
+
TransactionStatus[TransactionStatus["New"] = 0] = "New";
|
|
36
|
+
TransactionStatus[TransactionStatus["DryRun"] = 1] = "DryRun";
|
|
37
|
+
TransactionStatus[TransactionStatus["Pending"] = 2] = "Pending";
|
|
38
|
+
TransactionStatus[TransactionStatus["Accepted"] = 3] = "Accepted";
|
|
39
|
+
TransactionStatus[TransactionStatus["Rejected"] = 4] = "Rejected";
|
|
40
|
+
TransactionStatus[TransactionStatus["InvalidTransaction"] = 5] = "InvalidTransaction";
|
|
41
|
+
TransactionStatus[TransactionStatus["OnlyFeeAccepted"] = 6] = "OnlyFeeAccepted";
|
|
42
|
+
})(TransactionStatus || (TransactionStatus = {}));
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get the installed snaps in MetaMask.
|
|
46
|
+
*
|
|
47
|
+
* @param provider - The MetaMask inpage provider.
|
|
48
|
+
* @returns The snaps installed in MetaMask.
|
|
49
|
+
*/
|
|
50
|
+
const getSnaps = async (provider) => (await provider.request({
|
|
51
|
+
method: 'wallet_getSnaps',
|
|
52
|
+
}));
|
|
53
|
+
/**
|
|
54
|
+
* Connect a snap to MetaMask.
|
|
55
|
+
*
|
|
56
|
+
* @param snapId - The ID of the snap.
|
|
57
|
+
* @param params - The params to pass with the snap to connect.
|
|
58
|
+
*/
|
|
59
|
+
const connectSnap = async (provider, snaps) => {
|
|
60
|
+
await provider.request({
|
|
61
|
+
method: 'wallet_requestSnaps',
|
|
62
|
+
params: snaps,
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Get the snap from MetaMask.
|
|
67
|
+
*
|
|
68
|
+
* @param version - The version of the snap to install (optional).
|
|
69
|
+
* @returns The snap object returned by the extension.
|
|
70
|
+
*/
|
|
71
|
+
const getSnap = async (provider, snapId, version) => {
|
|
72
|
+
try {
|
|
73
|
+
const snaps = await getSnaps(provider);
|
|
74
|
+
return Object.values(snaps).find((snap) => snap.id === snapId && (!version || snap.version === version));
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
console.log('Failed to obtain installed snap', e);
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Detect if the wallet injecting the ethereum object is MetaMask Flask.
|
|
83
|
+
*
|
|
84
|
+
* @returns True if the MetaMask version is Flask, false otherwise.
|
|
85
|
+
*/
|
|
86
|
+
const isFlask = async (provider) => {
|
|
87
|
+
try {
|
|
88
|
+
const clientVersion = await provider.request({
|
|
89
|
+
method: 'web3_clientVersion',
|
|
90
|
+
});
|
|
91
|
+
const isFlaskDetected = clientVersion?.includes('flask');
|
|
92
|
+
return Boolean(provider && isFlaskDetected);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const MetamaskNotInstalled = 'METAMASK_NOT_INSTALLED';
|
|
100
|
+
const MetamaskIsNotFlask = 'METAMASK_IS_NOT_FLASK';
|
|
101
|
+
const TariSnapNotInstalled = 'TARI_SNAP_NOT_INSTALLED';
|
|
102
|
+
class MetamaskTariProvider {
|
|
103
|
+
providerName = "Metamask";
|
|
104
|
+
snapId;
|
|
105
|
+
snapVersion;
|
|
106
|
+
metamask;
|
|
107
|
+
snap;
|
|
108
|
+
metamaskConnected;
|
|
109
|
+
constructor(snapId, metamask) {
|
|
110
|
+
this.snapId = snapId;
|
|
111
|
+
this.snapVersion = undefined;
|
|
112
|
+
this.metamask = metamask;
|
|
113
|
+
this.metamaskConnected = false;
|
|
114
|
+
}
|
|
115
|
+
async connect() {
|
|
116
|
+
// check that the metamask provider is valid
|
|
117
|
+
if (!this.metamask || !this.metamask.isMetaMask) {
|
|
118
|
+
throw MetamaskNotInstalled;
|
|
119
|
+
}
|
|
120
|
+
// check that flask is installed
|
|
121
|
+
if (!isFlask(this.metamask)) {
|
|
122
|
+
throw MetamaskIsNotFlask;
|
|
123
|
+
}
|
|
124
|
+
// connect to the tari snap
|
|
125
|
+
// this will request MetaMask the installation of the tari snap if it's not already installed
|
|
126
|
+
await connectSnap(this.metamask, { [this.snapId]: { version: this.snapVersion } });
|
|
127
|
+
// store the tari snap reference
|
|
128
|
+
const snap = await getSnap(this.metamask, this.snapId);
|
|
129
|
+
if (!snap) {
|
|
130
|
+
// this should olny happen if the user didn't accept the tari snap in the previous step
|
|
131
|
+
throw TariSnapNotInstalled;
|
|
132
|
+
}
|
|
133
|
+
this.snap = snap;
|
|
134
|
+
this.metamaskConnected = true;
|
|
135
|
+
}
|
|
136
|
+
isConnected() {
|
|
137
|
+
return this.metamaskConnected;
|
|
138
|
+
}
|
|
139
|
+
async createFreeTestCoins(account_id) {
|
|
140
|
+
const res = await this.metamaskRequest('getFreeTestCoins', {
|
|
141
|
+
amount: 1000000,
|
|
142
|
+
account_id,
|
|
143
|
+
fee: 2000
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
account_id,
|
|
147
|
+
address: res.address,
|
|
148
|
+
public_key: res.public_key,
|
|
149
|
+
resources: []
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
async getAccount() {
|
|
153
|
+
return await this.metamaskRequest('getAccountData', { account_id: 0 });
|
|
154
|
+
}
|
|
155
|
+
async getSubstate(substate_address) {
|
|
156
|
+
const { substate, address: substate_id, version } = await this.metamaskRequest('getSubstate', { substate_address });
|
|
157
|
+
return { value: substate, address: { substate_id, version } };
|
|
158
|
+
}
|
|
159
|
+
async listSubstates(filter_by_template, filter_by_type, limit, offset) {
|
|
160
|
+
const res = await this.metamaskRequest('listSubstates', {
|
|
161
|
+
filter_by_template,
|
|
162
|
+
filter_by_type,
|
|
163
|
+
limit,
|
|
164
|
+
offset,
|
|
165
|
+
});
|
|
166
|
+
return res;
|
|
167
|
+
}
|
|
168
|
+
async submitTransaction(req) {
|
|
169
|
+
const params = {
|
|
170
|
+
instructions: req.instructions,
|
|
171
|
+
fee_instructions: req.fee_instructions,
|
|
172
|
+
input_refs: req.input_refs,
|
|
173
|
+
required_substates: req.required_substates || [],
|
|
174
|
+
is_dry_run: req.is_dry_run,
|
|
175
|
+
};
|
|
176
|
+
const resp = await this.metamaskRequest('sendTransaction', params);
|
|
177
|
+
if (!resp) {
|
|
178
|
+
throw new Error("Failed to submit transaction to metamask snap: empty response");
|
|
179
|
+
}
|
|
180
|
+
if (resp.error) {
|
|
181
|
+
throw new Error(`Failed to submit transaction to metamask snap: ${resp.error}`);
|
|
182
|
+
}
|
|
183
|
+
return { transaction_id: resp.transaction_id };
|
|
184
|
+
}
|
|
185
|
+
async getTransactionResult(transactionId) {
|
|
186
|
+
// This request returns the response from the indexer get_transaction_result request
|
|
187
|
+
const resp = await this.metamaskRequest('getTransactionResult', { transaction_id: transactionId });
|
|
188
|
+
if (!resp) {
|
|
189
|
+
throw new Error("Failed to get transaction result from metamask snap: empty response");
|
|
190
|
+
}
|
|
191
|
+
if (resp.result === "Pending") {
|
|
192
|
+
return {
|
|
193
|
+
transaction_id: transactionId,
|
|
194
|
+
status: TransactionStatus.Pending,
|
|
195
|
+
result: null
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
if (!resp?.result?.Finalized) {
|
|
199
|
+
throw new Error("Transaction result was not pending nor finalized");
|
|
200
|
+
}
|
|
201
|
+
const newStatus = convertToStatus(resp.result.Finalized);
|
|
202
|
+
return {
|
|
203
|
+
transaction_id: transactionId,
|
|
204
|
+
status: newStatus,
|
|
205
|
+
result: resp.result.Finalized.execution_result.finalize
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
async getPublicKey(_branch, index) {
|
|
209
|
+
const resp = await this.metamaskRequest('getPublicKey', { index });
|
|
210
|
+
if (!resp) {
|
|
211
|
+
throw new Error("Failed to get public key from metamask snap: empty response");
|
|
212
|
+
}
|
|
213
|
+
return resp.public_key;
|
|
214
|
+
}
|
|
215
|
+
async getConfidentialVaultBalances(viewKeyId, vaultId, min = null, max = null) {
|
|
216
|
+
const res = await this.metamaskRequest('getConfidentialVaultBalances', {
|
|
217
|
+
view_key_id: viewKeyId,
|
|
218
|
+
vault_id: vaultId,
|
|
219
|
+
minimum_expected_value: min,
|
|
220
|
+
maximum_expected_value: max,
|
|
221
|
+
});
|
|
222
|
+
return { balances: res };
|
|
223
|
+
}
|
|
224
|
+
getTemplateDefinition(template_address) {
|
|
225
|
+
return this.metamaskRequest('getTemplateDefinition', { template_address })
|
|
226
|
+
.then(resp => {
|
|
227
|
+
if (!resp) {
|
|
228
|
+
throw new Error("Template not found");
|
|
229
|
+
}
|
|
230
|
+
return resp.definition;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
async metamaskRequest(method, params) {
|
|
234
|
+
console.log("Metamask request:", method, params);
|
|
235
|
+
const resp = await this.metamask.request({
|
|
236
|
+
method: 'wallet_invokeSnap',
|
|
237
|
+
params: {
|
|
238
|
+
snapId: this.snapId,
|
|
239
|
+
request: {
|
|
240
|
+
method,
|
|
241
|
+
params
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
console.log("Metamask response:", resp);
|
|
246
|
+
if (!resp) {
|
|
247
|
+
throw new Error("Metamask request failed: empty response");
|
|
248
|
+
}
|
|
249
|
+
return resp;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function convertToStatus(result) {
|
|
253
|
+
// Ref: https://github.com/tari-project/tari-dan/blob/bb0b31139b770aacd7bb49af865543aa4a9e2de4/dan_layer/wallet/sdk/src/apis/transaction.rs
|
|
254
|
+
if (result.final_decision !== "Commit") {
|
|
255
|
+
return TransactionStatus.Rejected;
|
|
256
|
+
}
|
|
257
|
+
// if (!result?.result?.Finalized) {
|
|
258
|
+
// throw new Error("Transaction result was finalized but no result was returned");
|
|
259
|
+
// }
|
|
260
|
+
//
|
|
261
|
+
// if (result.finalize.AcceptFeeRejectRest) {
|
|
262
|
+
// return TransactionStatus.OnlyFeeAccepted;
|
|
263
|
+
// }
|
|
264
|
+
return TransactionStatus.Accepted;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
class SignaligServer {
|
|
268
|
+
_token;
|
|
269
|
+
_server_url;
|
|
270
|
+
constructor(server_url) {
|
|
271
|
+
console.log(server_url);
|
|
272
|
+
if (server_url !== undefined) {
|
|
273
|
+
this._server_url = server_url;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
this._server_url = "http://localhost:9100";
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
async initToken(permissions) {
|
|
280
|
+
this._token = await this.authLogin(permissions);
|
|
281
|
+
}
|
|
282
|
+
get token() {
|
|
283
|
+
return this._token;
|
|
284
|
+
}
|
|
285
|
+
async jsonRpc(method, token, params) {
|
|
286
|
+
console.log('jsonRpc', method, token, params);
|
|
287
|
+
let id = 0;
|
|
288
|
+
id += 1;
|
|
289
|
+
let address = this._server_url;
|
|
290
|
+
let headers = { 'Content-Type': 'application/json' };
|
|
291
|
+
if (token) {
|
|
292
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
293
|
+
}
|
|
294
|
+
let response = await fetch(address, {
|
|
295
|
+
method: 'POST',
|
|
296
|
+
body: JSON.stringify({
|
|
297
|
+
method: method,
|
|
298
|
+
jsonrpc: '2.0',
|
|
299
|
+
id: id,
|
|
300
|
+
params: params || {},
|
|
301
|
+
}),
|
|
302
|
+
headers: headers
|
|
303
|
+
});
|
|
304
|
+
let json = await response.json();
|
|
305
|
+
if (json.error) {
|
|
306
|
+
throw json.error;
|
|
307
|
+
}
|
|
308
|
+
return json.result;
|
|
309
|
+
}
|
|
310
|
+
async authLogin(permissions) {
|
|
311
|
+
return await this.jsonRpc("auth.login", undefined, permissions);
|
|
312
|
+
}
|
|
313
|
+
async storeIceCandidate(ice_candidate) {
|
|
314
|
+
return await this.jsonRpc("add.offer_ice_candidate", this._token, ice_candidate);
|
|
315
|
+
}
|
|
316
|
+
async storeOffer(offer) {
|
|
317
|
+
return await this.jsonRpc("add.offer", this._token, offer.sdp);
|
|
318
|
+
}
|
|
319
|
+
async getAnswer() {
|
|
320
|
+
return await this.jsonRpc("get.answer", this._token);
|
|
321
|
+
}
|
|
322
|
+
async getIceCandidates() {
|
|
323
|
+
return await this.jsonRpc("get.answer_ice_candidates", this._token);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
class TariConnection {
|
|
327
|
+
_peerConnection;
|
|
328
|
+
_dataChannel;
|
|
329
|
+
_signalingServer;
|
|
330
|
+
_callbacks;
|
|
331
|
+
_offer;
|
|
332
|
+
_walletToken;
|
|
333
|
+
// This is public so that user can directly set the onopen callback that will be called once the data channel is open.
|
|
334
|
+
onopen;
|
|
335
|
+
onConnection;
|
|
336
|
+
constructor(signalig_server_url, config) {
|
|
337
|
+
this._peerConnection = new RTCPeerConnection(config || this.config());
|
|
338
|
+
this._dataChannel = this._peerConnection.createDataChannel("tari-data");
|
|
339
|
+
this._signalingServer = new SignaligServer(signalig_server_url);
|
|
340
|
+
this._callbacks = {};
|
|
341
|
+
}
|
|
342
|
+
get token() {
|
|
343
|
+
if (this._walletToken) {
|
|
344
|
+
return this._walletToken;
|
|
345
|
+
}
|
|
346
|
+
return this._signalingServer.token;
|
|
347
|
+
}
|
|
348
|
+
async init(permissions, onConnection) {
|
|
349
|
+
this.onConnection = onConnection;
|
|
350
|
+
await this._signalingServer.initToken(permissions);
|
|
351
|
+
// Setup our receiving end
|
|
352
|
+
this._dataChannel.onmessage = (message) => {
|
|
353
|
+
let response = JSON.parse(message.data);
|
|
354
|
+
console.log('response', response);
|
|
355
|
+
if (!this._callbacks[response.id]) {
|
|
356
|
+
console.error("No callback found for id", response.id);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
// The response should contain id, to identify the Promise.resolve, that is waiting for this result
|
|
360
|
+
let [resolve, reject] = this._callbacks[response.id];
|
|
361
|
+
delete this._callbacks[response.id];
|
|
362
|
+
if (response.payload?.error) {
|
|
363
|
+
reject(new Error(response.payload.error));
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
resolve(response.payload);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
this._dataChannel.onopen = () => {
|
|
370
|
+
// This should be removed before the release, but it's good for debugging.
|
|
371
|
+
console.log("Data channel is open!");
|
|
372
|
+
this.sendMessage({ id: 0, jsonrpc: "2.0", method: "get.token", params: {} }, this._signalingServer.token)
|
|
373
|
+
.then((walletToken) => {
|
|
374
|
+
if (typeof walletToken !== 'string') {
|
|
375
|
+
throw Error('Received invalid JWT from wallet daemon');
|
|
376
|
+
}
|
|
377
|
+
console.log("Wallet JWT received: ", walletToken);
|
|
378
|
+
this._walletToken = walletToken;
|
|
379
|
+
if (this.onConnection) {
|
|
380
|
+
this.onConnection(this);
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
};
|
|
384
|
+
this._peerConnection.onicecandidate = (event) => {
|
|
385
|
+
console.log('event', event);
|
|
386
|
+
if (event?.candidate) {
|
|
387
|
+
console.log("ICE ", event.candidate);
|
|
388
|
+
console.log("ICE ", typeof event.candidate);
|
|
389
|
+
// Store the ice candidates, so the other end can add them
|
|
390
|
+
this._signalingServer.storeIceCandidate(event.candidate).then((resp) => {
|
|
391
|
+
// This should be removed before the release, but it's good for debugging.
|
|
392
|
+
console.log("Candidate stored", resp);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
// Create offer
|
|
397
|
+
this._offer = await this._peerConnection.createOffer();
|
|
398
|
+
// Set the offer as our local sdp, at this point it will start getting the ice candidates
|
|
399
|
+
this._peerConnection.setLocalDescription(this._offer);
|
|
400
|
+
// Store the offer so the other end can set it as a remote sdp
|
|
401
|
+
this._signalingServer.storeOffer(this._offer).then((resp) => {
|
|
402
|
+
// This should be removed before the release, but it's good for debugging.
|
|
403
|
+
console.log("Offer stored", resp);
|
|
404
|
+
});
|
|
405
|
+
await this.signalingServerPolling();
|
|
406
|
+
}
|
|
407
|
+
async setAnswer() {
|
|
408
|
+
// This is called once the other end got the offer and ices and created and store an answer and its ice candidates
|
|
409
|
+
// We get its answer sdp
|
|
410
|
+
let sdp = await this._signalingServer.getAnswer();
|
|
411
|
+
// And its ice candidates
|
|
412
|
+
let iceCandidates = await this._signalingServer.getIceCandidates();
|
|
413
|
+
// For us the answer is remote sdp
|
|
414
|
+
let answer = new RTCSessionDescription({ sdp, type: "answer" });
|
|
415
|
+
this._peerConnection.setRemoteDescription(answer);
|
|
416
|
+
// We add all the ice candidates to connect, the other end is doing the same with our ice candidates
|
|
417
|
+
for (const iceCandidate of iceCandidates) {
|
|
418
|
+
this._peerConnection.addIceCandidate(iceCandidate);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
async signalingServerPolling() {
|
|
422
|
+
// no need to keep retrying if we are already connected to the wallet
|
|
423
|
+
if (this._peerConnection.connectionState === "connected") {
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
try {
|
|
427
|
+
await this.setAnswer();
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
// we don't need to do anything on error, as the execution will be retried later
|
|
431
|
+
console.error(error);
|
|
432
|
+
}
|
|
433
|
+
// try again later
|
|
434
|
+
setTimeout(async () => {
|
|
435
|
+
await this.signalingServerPolling();
|
|
436
|
+
}, 2000);
|
|
437
|
+
}
|
|
438
|
+
isConnected() {
|
|
439
|
+
return this._dataChannel.readyState === "open";
|
|
440
|
+
}
|
|
441
|
+
// If the last parameter has timeout property e.g. {timeout:1000}, it set the timeout for this call.
|
|
442
|
+
async sendMessage(request, token, timeout_secs = null) {
|
|
443
|
+
if (!this.isConnected) {
|
|
444
|
+
throw new Error("WALLET_DAEMON_NOT_CONNECTED");
|
|
445
|
+
}
|
|
446
|
+
// This should be removed before the release, but it's good for debugging.
|
|
447
|
+
console.log(request, 'timeout', timeout_secs);
|
|
448
|
+
return new Promise((resolve, reject) => {
|
|
449
|
+
// We store the resolve callback for this request,
|
|
450
|
+
// so once the data channel receives a response we know where to return the data
|
|
451
|
+
this._callbacks[request.id] = [resolve, reject];
|
|
452
|
+
if (timeout_secs) {
|
|
453
|
+
// If the user set a timeout which set it here so the promise will be rejected if not fulfilled in time.
|
|
454
|
+
setTimeout(() => {
|
|
455
|
+
delete this._callbacks[request.id];
|
|
456
|
+
reject(new Error("Timeout"));
|
|
457
|
+
}, timeout_secs * 1000);
|
|
458
|
+
}
|
|
459
|
+
// Make the actual call to the wallet daemon
|
|
460
|
+
this._dataChannel.send(JSON.stringify({ token, ...request }));
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
// This is our default config, use can set their own stun/turn server in the constructor.
|
|
464
|
+
config() {
|
|
465
|
+
return { iceServers: [{ urls: "stun:stun.l.google.com:19302" }] };
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
class Hash {
|
|
470
|
+
value;
|
|
471
|
+
constructor(value) {
|
|
472
|
+
this.value = value;
|
|
473
|
+
}
|
|
474
|
+
toJSON() {
|
|
475
|
+
return this.value;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
var TAG;
|
|
479
|
+
(function (TAG) {
|
|
480
|
+
TAG[TAG["ComponentAddress"] = 128] = "ComponentAddress";
|
|
481
|
+
TAG[TAG["Metadata"] = 129] = "Metadata";
|
|
482
|
+
TAG[TAG["NonFungibleAddress"] = 130] = "NonFungibleAddress";
|
|
483
|
+
TAG[TAG["ResourceAddress"] = 131] = "ResourceAddress";
|
|
484
|
+
TAG[TAG["VaultId"] = 132] = "VaultId";
|
|
485
|
+
TAG[TAG["TransactionReceipt"] = 134] = "TransactionReceipt";
|
|
486
|
+
TAG[TAG["FeeClaim"] = 135] = "FeeClaim";
|
|
487
|
+
})(TAG || (TAG = {}));
|
|
488
|
+
class Tagged {
|
|
489
|
+
value;
|
|
490
|
+
tag;
|
|
491
|
+
constructor(tag, value) {
|
|
492
|
+
this.tag = tag;
|
|
493
|
+
this.value = value;
|
|
494
|
+
}
|
|
495
|
+
toJSON() {
|
|
496
|
+
return { "@@TAGGED@@": [this.tag, this.value] };
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
class ResourceAddress {
|
|
500
|
+
tagged;
|
|
501
|
+
constructor(hash) {
|
|
502
|
+
this.tagged = new Tagged(TAG.ResourceAddress, hash);
|
|
503
|
+
}
|
|
504
|
+
toJSON() {
|
|
505
|
+
return this.tagged.toJSON();
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
class UnclaimedConfidentialOutputAddress {
|
|
509
|
+
hash;
|
|
510
|
+
constructor(hash) {
|
|
511
|
+
this.hash = hash;
|
|
512
|
+
}
|
|
513
|
+
toJSON() {
|
|
514
|
+
return this.hash.toJSON();
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
class U256 {
|
|
518
|
+
value;
|
|
519
|
+
constructor(value) {
|
|
520
|
+
this.value = value;
|
|
521
|
+
}
|
|
522
|
+
toJSON() {
|
|
523
|
+
return this.value;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
class NonFungibleId {
|
|
527
|
+
value;
|
|
528
|
+
constructor(value) {
|
|
529
|
+
this.value = value;
|
|
530
|
+
}
|
|
531
|
+
toJSON() {
|
|
532
|
+
switch (typeof this.value) {
|
|
533
|
+
case 'string':
|
|
534
|
+
return { 'string': this.value };
|
|
535
|
+
case 'number':
|
|
536
|
+
return { 'Uint64': this.value };
|
|
537
|
+
}
|
|
538
|
+
return { 'U256': this.value };
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
class NonFungibleAddressContents {
|
|
542
|
+
resource_address;
|
|
543
|
+
id;
|
|
544
|
+
constructor(resource_address, id) {
|
|
545
|
+
this.resource_address = resource_address;
|
|
546
|
+
this.id = id;
|
|
547
|
+
}
|
|
548
|
+
toJSON() {
|
|
549
|
+
return { "resource_address": this.resource_address, "id": this.id };
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
class NonFungibleAddress {
|
|
553
|
+
tagged;
|
|
554
|
+
constructor(value) {
|
|
555
|
+
this.tagged = new Tagged(TAG.NonFungibleAddress, value);
|
|
556
|
+
}
|
|
557
|
+
toJSON() {
|
|
558
|
+
return this.tagged.toJSON();
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
class NonFungibleIndexAddress {
|
|
562
|
+
resource_address;
|
|
563
|
+
index;
|
|
564
|
+
constructor(resource_address, index) {
|
|
565
|
+
this.resource_address = resource_address;
|
|
566
|
+
this.index = index;
|
|
567
|
+
}
|
|
568
|
+
toJSON() {
|
|
569
|
+
return { "resource_address": this.resource_address, "index": this.index };
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
class ComponentAddress {
|
|
573
|
+
tagged;
|
|
574
|
+
constructor(hash) {
|
|
575
|
+
this.tagged = new Tagged(TAG.ComponentAddress, hash);
|
|
576
|
+
}
|
|
577
|
+
toJSON() {
|
|
578
|
+
return this.tagged.toJSON();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
class VaultId {
|
|
582
|
+
tagged;
|
|
583
|
+
constructor(hash) {
|
|
584
|
+
this.tagged = new Tagged(TAG.VaultId, hash);
|
|
585
|
+
}
|
|
586
|
+
toJSON() {
|
|
587
|
+
return this.tagged.toJSON();
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
class SubstateAddress {
|
|
591
|
+
value;
|
|
592
|
+
constructor(value) {
|
|
593
|
+
this.value = value;
|
|
594
|
+
}
|
|
595
|
+
toJSON() {
|
|
596
|
+
if (this.value instanceof ComponentAddress) {
|
|
597
|
+
return { "Component": this.value };
|
|
598
|
+
}
|
|
599
|
+
else if (this.value instanceof ResourceAddress) {
|
|
600
|
+
return { "Resource": this.value };
|
|
601
|
+
}
|
|
602
|
+
else if (this.value instanceof VaultId) {
|
|
603
|
+
return { "Vault": this.value };
|
|
604
|
+
}
|
|
605
|
+
else if (this.value instanceof UnclaimedConfidentialOutputAddress) {
|
|
606
|
+
return { "UnclaimedConfidentialOutput": this.value };
|
|
607
|
+
}
|
|
608
|
+
else if (this.value instanceof NonFungibleAddress) {
|
|
609
|
+
return { "NonFungible": this.value };
|
|
610
|
+
}
|
|
611
|
+
else if (this.value instanceof NonFungibleIndexAddress) {
|
|
612
|
+
return { "NonFungibleIndex": this.value };
|
|
613
|
+
}
|
|
614
|
+
throw "Unknown type";
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
class TariPermissionAccountBalance {
|
|
618
|
+
value;
|
|
619
|
+
constructor(value) {
|
|
620
|
+
this.value = value;
|
|
621
|
+
}
|
|
622
|
+
toJSON() {
|
|
623
|
+
console.log("stringify", this.value);
|
|
624
|
+
return { "AccountBalance": this.value };
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
class TariPermissionAccountInfo {
|
|
628
|
+
constructor() { }
|
|
629
|
+
toJSON() {
|
|
630
|
+
return "AccountInfo";
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
class TariPermissionAccountList {
|
|
634
|
+
value;
|
|
635
|
+
constructor(value) {
|
|
636
|
+
if (value === undefined) {
|
|
637
|
+
this.value = null;
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
this.value = value;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
toJSON() {
|
|
644
|
+
console.log('JSON TariPermissionAccountList', this.value);
|
|
645
|
+
if (this.value === undefined) {
|
|
646
|
+
return { "AccountList": null };
|
|
647
|
+
}
|
|
648
|
+
else {
|
|
649
|
+
return { "AccountList": this.value };
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
class TariPermissionKeyList {
|
|
654
|
+
constructor() { }
|
|
655
|
+
toJSON() {
|
|
656
|
+
return "KeyList";
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
class TariPermissionTransactionGet {
|
|
660
|
+
constructor() { }
|
|
661
|
+
toJSON() {
|
|
662
|
+
return "TransactionGet";
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
class TariPermissionTransactionSend {
|
|
666
|
+
value;
|
|
667
|
+
constructor(value) {
|
|
668
|
+
this.value = value;
|
|
669
|
+
}
|
|
670
|
+
toJSON() {
|
|
671
|
+
console.log('JSON TariPermissionTransactionSend', this.value);
|
|
672
|
+
if (this.value === undefined) {
|
|
673
|
+
return { "TransactionSend": null };
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
return { "TransactionSend": this.value };
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
class TariPermissionGetNft {
|
|
681
|
+
value0;
|
|
682
|
+
value1;
|
|
683
|
+
constructor(value0, value1) {
|
|
684
|
+
this.value0 = value0;
|
|
685
|
+
this.value1 = value1;
|
|
686
|
+
}
|
|
687
|
+
toJSON() {
|
|
688
|
+
return { "GetNft": [this.value0, this.value1] };
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
class TariPermissionNftGetOwnershipProof {
|
|
692
|
+
value;
|
|
693
|
+
constructor(value) {
|
|
694
|
+
this.value = value;
|
|
695
|
+
}
|
|
696
|
+
toJSON() {
|
|
697
|
+
return { "NftGetOwnershipProof": this.value };
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
class TariPermissionTransactionsGet {
|
|
701
|
+
constructor() { }
|
|
702
|
+
toJSON() {
|
|
703
|
+
return "TransactionGet";
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
class TariPermissionSubstatesRead {
|
|
707
|
+
constructor() { }
|
|
708
|
+
toJSON() {
|
|
709
|
+
return "SubstatesRead";
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
class TariPermissionTemplatesRead {
|
|
713
|
+
constructor() { }
|
|
714
|
+
toJSON() {
|
|
715
|
+
return "TemplatesRead";
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
// export enum TariPermission {
|
|
719
|
+
// AccountBalance = "AccountBalance",
|
|
720
|
+
// AccountInfo = "AccountInfo",
|
|
721
|
+
// AccountList = "AccountList",
|
|
722
|
+
// KeyList = "KeyList",
|
|
723
|
+
// TransactionGet = "TransactionGet",
|
|
724
|
+
// TransactionSend = "TransactionSend",
|
|
725
|
+
// GetNft = "GetNft",
|
|
726
|
+
// NftGetOwnershipProof = "NftGetOwnershipProof",
|
|
727
|
+
// TransactionsGet = "TransactionsGet",
|
|
728
|
+
// SubstatesRead = "SubstatesRead",
|
|
729
|
+
// TemplatesRead = "TemplatesRead",
|
|
730
|
+
// }
|
|
731
|
+
class TariPermissions {
|
|
732
|
+
permissions;
|
|
733
|
+
constructor() {
|
|
734
|
+
this.permissions = [];
|
|
735
|
+
}
|
|
736
|
+
addPermission(permission) {
|
|
737
|
+
this.permissions.push(permission);
|
|
738
|
+
return this;
|
|
739
|
+
}
|
|
740
|
+
addPermissions(other) {
|
|
741
|
+
this.permissions.push(...other.permissions);
|
|
742
|
+
return this;
|
|
743
|
+
}
|
|
744
|
+
toJSON() {
|
|
745
|
+
return this.permissions;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
// TariPermissionType.prototype.toString = function () {
|
|
749
|
+
// return "wtf"
|
|
750
|
+
// }
|
|
751
|
+
|
|
752
|
+
class WebRtcRpcTransport {
|
|
753
|
+
connection;
|
|
754
|
+
constructor(connection) {
|
|
755
|
+
this.connection = connection;
|
|
756
|
+
}
|
|
757
|
+
static new(connection) {
|
|
758
|
+
return new WebRtcRpcTransport(connection);
|
|
759
|
+
}
|
|
760
|
+
token() {
|
|
761
|
+
return this.connection.token;
|
|
762
|
+
}
|
|
763
|
+
isConnected() {
|
|
764
|
+
return this.connection.isConnected();
|
|
765
|
+
}
|
|
766
|
+
sendRequest(data, options) {
|
|
767
|
+
return this.connection.sendMessage(data, options.token);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
const WalletDaemonNotConnected = 'WALLET_DAEMON_NOT_CONNECTED';
|
|
772
|
+
const Unsupported = 'UNSUPPORTED';
|
|
773
|
+
class WalletDaemonTariProvider {
|
|
774
|
+
providerName = "WalletDaemon";
|
|
775
|
+
params;
|
|
776
|
+
client;
|
|
777
|
+
constructor(params, connection) {
|
|
778
|
+
this.params = params;
|
|
779
|
+
this.client = connection;
|
|
780
|
+
}
|
|
781
|
+
static async build(params) {
|
|
782
|
+
const allPermissions = new TariPermissions();
|
|
783
|
+
allPermissions.addPermissions(params.permissions);
|
|
784
|
+
allPermissions.addPermissions(params.optionalPermissions);
|
|
785
|
+
console.log({ allPermissions });
|
|
786
|
+
let connection = new TariConnection(params.signalingServerUrl, params.webRtcConfig);
|
|
787
|
+
const client = WalletDaemonClient.new(WebRtcRpcTransport.new(connection));
|
|
788
|
+
await connection.init(allPermissions, (conn) => {
|
|
789
|
+
params.onConnection?.();
|
|
790
|
+
if (conn.token) {
|
|
791
|
+
client.setToken(conn.token);
|
|
792
|
+
}
|
|
793
|
+
});
|
|
794
|
+
return new WalletDaemonTariProvider(params, client);
|
|
795
|
+
}
|
|
796
|
+
get token() {
|
|
797
|
+
return this.client.getTransport().token();
|
|
798
|
+
}
|
|
799
|
+
get tokenUrl() {
|
|
800
|
+
if (!this.token) {
|
|
801
|
+
return undefined;
|
|
802
|
+
}
|
|
803
|
+
const name = this.params.name && encodeURIComponent(this.params.name) || '';
|
|
804
|
+
const token = this.token;
|
|
805
|
+
const permissions = JSON.stringify(this.params.permissions);
|
|
806
|
+
const optionalPermissions = JSON.stringify(this.params.optionalPermissions);
|
|
807
|
+
return `tari://${name}/${token}/${permissions}/${optionalPermissions}`;
|
|
808
|
+
}
|
|
809
|
+
isConnected() {
|
|
810
|
+
return this.client.getTransport().isConnected();
|
|
811
|
+
}
|
|
812
|
+
async createFreeTestCoins() {
|
|
813
|
+
const res = await this.client.createFreeTestCoins({
|
|
814
|
+
account: { Name: "template_web" },
|
|
815
|
+
amount: 1000000,
|
|
816
|
+
max_fee: null,
|
|
817
|
+
key_id: 0
|
|
818
|
+
});
|
|
819
|
+
return {
|
|
820
|
+
account_id: res.account.key_index,
|
|
821
|
+
address: res.account.address.Component,
|
|
822
|
+
public_key: res.public_key,
|
|
823
|
+
resources: []
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
async getAccount() {
|
|
827
|
+
const { account, public_key } = await this.client.accountsGetDefault({});
|
|
828
|
+
const { balances } = await this.client.accountsGetBalances({ account: { ComponentAddress: account.address.Component }, refresh: false });
|
|
829
|
+
return {
|
|
830
|
+
account_id: account.key_index,
|
|
831
|
+
address: account.address.Component,
|
|
832
|
+
public_key,
|
|
833
|
+
// TODO: should be vaults not resources
|
|
834
|
+
resources: balances.map((b) => ({
|
|
835
|
+
type: b.resource_type,
|
|
836
|
+
resource_address: b.resource_address,
|
|
837
|
+
balance: b.balance + b.confidential_balance,
|
|
838
|
+
vault_id: ('Vault' in b.vault_address) ? b.vault_address.Vault : b.vault_address,
|
|
839
|
+
token_symbol: b.token_symbol,
|
|
840
|
+
}))
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
async getAccountBalances(componentAddress) {
|
|
844
|
+
return await this.client.accountsGetBalances({ account: { ComponentAddress: componentAddress }, refresh: true });
|
|
845
|
+
}
|
|
846
|
+
async getSubstate(substate_id) {
|
|
847
|
+
const substateId = stringToSubstateId(substate_id);
|
|
848
|
+
const { value, record } = await this.client.substatesGet({ substate_id: substateId });
|
|
849
|
+
return {
|
|
850
|
+
value,
|
|
851
|
+
address: {
|
|
852
|
+
substate_id: substateIdToString(record.substate_id),
|
|
853
|
+
version: record.version
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
async submitTransaction(req) {
|
|
858
|
+
const params = {
|
|
859
|
+
transaction: null,
|
|
860
|
+
signing_key_index: req.account_id,
|
|
861
|
+
fee_instructions: req.fee_instructions,
|
|
862
|
+
instructions: req.instructions,
|
|
863
|
+
inputs: req.required_substates.map((s) => ({
|
|
864
|
+
// TODO: Hmm The bindings want a SubstateId object, but the wallet only wants a string. Any is used to skip type checking here
|
|
865
|
+
substate_id: s.substate_id,
|
|
866
|
+
version: s.version
|
|
867
|
+
})),
|
|
868
|
+
input_refs: [],
|
|
869
|
+
override_inputs: false,
|
|
870
|
+
is_dry_run: req.is_dry_run,
|
|
871
|
+
proof_ids: [],
|
|
872
|
+
min_epoch: null,
|
|
873
|
+
max_epoch: null,
|
|
874
|
+
};
|
|
875
|
+
const res = await this.client.submitTransaction(params);
|
|
876
|
+
return { transaction_id: res.transaction_id };
|
|
877
|
+
}
|
|
878
|
+
async getTransactionResult(transactionId) {
|
|
879
|
+
const res = await this.client.getTransactionResult({ transaction_id: transactionId });
|
|
880
|
+
return {
|
|
881
|
+
transaction_id: transactionId,
|
|
882
|
+
status: convertStringToTransactionStatus(res.status),
|
|
883
|
+
result: res.result,
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
async getPublicKey(branch, index) {
|
|
887
|
+
const res = await this.client.createKey({ branch: branch, specific_index: index });
|
|
888
|
+
return res.public_key;
|
|
889
|
+
}
|
|
890
|
+
async getTemplateDefinition(template_address) {
|
|
891
|
+
let resp = await this.client.templatesGet({ template_address });
|
|
892
|
+
return resp.template_definition;
|
|
893
|
+
}
|
|
894
|
+
async getConfidentialVaultBalances(viewKeyId, vaultId, min = null, max = null) {
|
|
895
|
+
const res = await this.client.viewVaultBalance({
|
|
896
|
+
view_key_id: viewKeyId,
|
|
897
|
+
vault_id: vaultId,
|
|
898
|
+
minimum_expected_value: min,
|
|
899
|
+
maximum_expected_value: max,
|
|
900
|
+
});
|
|
901
|
+
return { balances: res.balances };
|
|
902
|
+
}
|
|
903
|
+
async listSubstates(filter_by_template, filter_by_type, limit, offset) {
|
|
904
|
+
const resp = await this.client.substatesList({
|
|
905
|
+
filter_by_template,
|
|
906
|
+
filter_by_type,
|
|
907
|
+
limit,
|
|
908
|
+
offset
|
|
909
|
+
});
|
|
910
|
+
const substates = resp.substates.map((s) => ({
|
|
911
|
+
substate_id: substateIdToString(s.substate_id),
|
|
912
|
+
module_name: s.module_name,
|
|
913
|
+
version: s.version,
|
|
914
|
+
template_address: s.template_address
|
|
915
|
+
}));
|
|
916
|
+
return { substates };
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
function convertStringToTransactionStatus(status) {
|
|
920
|
+
switch (status) {
|
|
921
|
+
case "New":
|
|
922
|
+
return TransactionStatus.New;
|
|
923
|
+
case "DryRun":
|
|
924
|
+
return TransactionStatus.DryRun;
|
|
925
|
+
case "Pending":
|
|
926
|
+
return TransactionStatus.Pending;
|
|
927
|
+
case "Accepted":
|
|
928
|
+
return TransactionStatus.Accepted;
|
|
929
|
+
case "Rejected":
|
|
930
|
+
return TransactionStatus.Rejected;
|
|
931
|
+
case "InvalidTransaction":
|
|
932
|
+
return TransactionStatus.InvalidTransaction;
|
|
933
|
+
case "OnlyFeeAccepted":
|
|
934
|
+
return TransactionStatus.OnlyFeeAccepted;
|
|
935
|
+
default:
|
|
936
|
+
throw new Error(`Unknown status: ${status}`);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
941
|
+
__proto__: null,
|
|
942
|
+
ComponentAddress: ComponentAddress,
|
|
943
|
+
Hash: Hash,
|
|
944
|
+
NonFungibleAddress: NonFungibleAddress,
|
|
945
|
+
NonFungibleAddressContents: NonFungibleAddressContents,
|
|
946
|
+
NonFungibleId: NonFungibleId,
|
|
947
|
+
NonFungibleIndexAddress: NonFungibleIndexAddress,
|
|
948
|
+
ResourceAddress: ResourceAddress,
|
|
949
|
+
SubstateAddress: SubstateAddress,
|
|
950
|
+
get TAG () { return TAG; },
|
|
951
|
+
Tagged: Tagged,
|
|
952
|
+
TariConnection: TariConnection,
|
|
953
|
+
TariPermissionAccountBalance: TariPermissionAccountBalance,
|
|
954
|
+
TariPermissionAccountInfo: TariPermissionAccountInfo,
|
|
955
|
+
TariPermissionAccountList: TariPermissionAccountList,
|
|
956
|
+
TariPermissionGetNft: TariPermissionGetNft,
|
|
957
|
+
TariPermissionKeyList: TariPermissionKeyList,
|
|
958
|
+
TariPermissionNftGetOwnershipProof: TariPermissionNftGetOwnershipProof,
|
|
959
|
+
TariPermissionSubstatesRead: TariPermissionSubstatesRead,
|
|
960
|
+
TariPermissionTemplatesRead: TariPermissionTemplatesRead,
|
|
961
|
+
TariPermissionTransactionGet: TariPermissionTransactionGet,
|
|
962
|
+
TariPermissionTransactionSend: TariPermissionTransactionSend,
|
|
963
|
+
TariPermissionTransactionsGet: TariPermissionTransactionsGet,
|
|
964
|
+
TariPermissions: TariPermissions,
|
|
965
|
+
U256: U256,
|
|
966
|
+
UnclaimedConfidentialOutputAddress: UnclaimedConfidentialOutputAddress,
|
|
967
|
+
Unsupported: Unsupported,
|
|
968
|
+
VaultId: VaultId,
|
|
969
|
+
WalletDaemonNotConnected: WalletDaemonNotConnected,
|
|
970
|
+
WalletDaemonTariProvider: WalletDaemonTariProvider
|
|
971
|
+
});
|
|
972
|
+
|
|
973
|
+
class TariUniverseProvider {
|
|
974
|
+
params;
|
|
975
|
+
providerName = "TariUniverse";
|
|
976
|
+
__id = 0;
|
|
977
|
+
constructor(params) {
|
|
978
|
+
this.params = params;
|
|
979
|
+
const filterResizeEvent = function (event) {
|
|
980
|
+
if (event.data && event.data.type === "resize") {
|
|
981
|
+
const resizeEvent = new CustomEvent("resize", {
|
|
982
|
+
detail: { width: event.data.width, height: event.data.height },
|
|
983
|
+
});
|
|
984
|
+
window.dispatchEvent(resizeEvent);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
window.addEventListener("message", (event) => filterResizeEvent(event), false);
|
|
988
|
+
}
|
|
989
|
+
async sendRequest(req) {
|
|
990
|
+
const id = ++this.__id;
|
|
991
|
+
return new Promise(function (resolve, reject) {
|
|
992
|
+
const event_ref = function (resp) {
|
|
993
|
+
if (resp.data.resultError) {
|
|
994
|
+
window.removeEventListener("message", event_ref);
|
|
995
|
+
reject(resp.data.resultError);
|
|
996
|
+
}
|
|
997
|
+
if (resp && resp.data && resp.data.id && resp.data.id == id && resp.data.type === "provider-call") {
|
|
998
|
+
window.removeEventListener("message", event_ref);
|
|
999
|
+
resolve(resp.data.result);
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
window.addEventListener("message", event_ref, false);
|
|
1003
|
+
window.parent.postMessage({ ...req, id, type: "provider-call" }, "*");
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
isConnected() {
|
|
1007
|
+
return true;
|
|
1008
|
+
}
|
|
1009
|
+
getPublicKey() {
|
|
1010
|
+
return this.sendRequest({ methodName: "getPublicKey", args: [] });
|
|
1011
|
+
}
|
|
1012
|
+
async listSubstates(filter_by_template, filter_by_type, limit, offset) {
|
|
1013
|
+
return this.sendRequest({
|
|
1014
|
+
methodName: "listSubstates",
|
|
1015
|
+
args: [filter_by_template, filter_by_type, limit, offset],
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
getConfidentialVaultBalances(viewKeyId, vaultId, min, max) {
|
|
1019
|
+
return this.sendRequest({
|
|
1020
|
+
methodName: "getConfidentialVaultBalances",
|
|
1021
|
+
args: [viewKeyId, vaultId, min, max],
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
async createFreeTestCoins() {
|
|
1025
|
+
return this.sendRequest({ methodName: "createFreeTestCoins", args: [] });
|
|
1026
|
+
}
|
|
1027
|
+
requestParentSize() {
|
|
1028
|
+
return this.sendRequest({ methodName: "requestParentSize", args: [] });
|
|
1029
|
+
}
|
|
1030
|
+
async getAccount() {
|
|
1031
|
+
return this.sendRequest({ methodName: "getAccount", args: [] });
|
|
1032
|
+
}
|
|
1033
|
+
async getAccountBalances(componentAddress) {
|
|
1034
|
+
return this.sendRequest({
|
|
1035
|
+
methodName: "getAccountBalances",
|
|
1036
|
+
args: [componentAddress],
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
async getSubstate(substate_id) {
|
|
1040
|
+
return this.sendRequest({
|
|
1041
|
+
methodName: "getSubstate",
|
|
1042
|
+
args: [substate_id],
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
async submitTransaction(req) {
|
|
1046
|
+
return this.sendRequest({
|
|
1047
|
+
methodName: "submitTransaction",
|
|
1048
|
+
args: [req],
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
async getTransactionResult(transactionId) {
|
|
1052
|
+
return this.sendRequest({
|
|
1053
|
+
methodName: "getTransactionResult",
|
|
1054
|
+
args: [transactionId],
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
async getTemplateDefinition(template_address) {
|
|
1058
|
+
return this.sendRequest({ methodName: "getTemplateDefinition", args: [template_address] });
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
export { MetamaskTariProvider, TariPermissions, TariUniverseProvider, TransactionStatus, WalletDaemonTariProvider, index as permissions, utils };
|
|
1063
|
+
//# sourceMappingURL=index.js.map
|