@novasamatech/host-api-wrapper 0.7.9-5
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/README.md +429 -0
- package/dist/accounts.d.ts +93 -0
- package/dist/accounts.js +263 -0
- package/dist/chat.d.ts +33 -0
- package/dist/chat.js +125 -0
- package/dist/constants.d.ts +11 -0
- package/dist/constants.js +12 -0
- package/dist/deriveEntropy.d.ts +3 -0
- package/dist/deriveEntropy.js +8 -0
- package/dist/helpers.d.ts +9 -0
- package/dist/helpers.js +19 -0
- package/dist/hostApi.d.ts +1 -0
- package/dist/hostApi.js +3 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +16 -0
- package/dist/injectWeb3.d.ts +90 -0
- package/dist/injectWeb3.js +157 -0
- package/dist/localStorage.d.ts +18 -0
- package/dist/localStorage.js +44 -0
- package/dist/metaProvider.d.ts +7 -0
- package/dist/metaProvider.js +24 -0
- package/dist/notification.d.ts +15 -0
- package/dist/notification.js +20 -0
- package/dist/papiProvider.d.ts +7 -0
- package/dist/papiProvider.js +349 -0
- package/dist/payments.d.ts +35 -0
- package/dist/payments.js +49 -0
- package/dist/permission.d.ts +24 -0
- package/dist/permission.js +28 -0
- package/dist/preimage.d.ts +9 -0
- package/dist/preimage.js +24 -0
- package/dist/sandboxTransport.d.ts +9 -0
- package/dist/sandboxTransport.js +109 -0
- package/dist/statementStore.d.ts +44 -0
- package/dist/statementStore.js +41 -0
- package/dist/theme.d.ts +6 -0
- package/dist/theme.js +18 -0
- package/package.json +38 -0
package/dist/accounts.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { CreateProofErr, GetUserIdErr, LoginErr, RequestCredentialsErr, RingLocation, SigningPayload, SigningPayloadWithoutAccount, SigningRawPayload, SigningRawPayloadWithoutAccount, assertEnumVariant, createHostApi, enumValue, fromHex, isEnumVariant, toHex, } from '@novasamatech/host-api';
|
|
2
|
+
import { decAnyMetadata, unifyMetadata } from '@polkadot-api/substrate-bindings';
|
|
3
|
+
import { err, ok } from 'neverthrow';
|
|
4
|
+
import { getPolkadotSignerFromPjs } from 'polkadot-api/pjs-signer';
|
|
5
|
+
import { sandboxTransport } from './sandboxTransport.js';
|
|
6
|
+
const UNSUPPORTED_VERSION_ERROR = 'Unsupported message version';
|
|
7
|
+
export const createAccountsProvider = (transport = sandboxTransport) => {
|
|
8
|
+
const hostApi = createHostApi(transport);
|
|
9
|
+
return {
|
|
10
|
+
getUserId() {
|
|
11
|
+
return hostApi
|
|
12
|
+
.getUserId(enumValue('v1', undefined))
|
|
13
|
+
.mapErr(e => e.value)
|
|
14
|
+
.andThen(response => {
|
|
15
|
+
if (isEnumVariant(response, 'v1')) {
|
|
16
|
+
return ok(response.value);
|
|
17
|
+
}
|
|
18
|
+
// @ts-expect-error response.tag is never here
|
|
19
|
+
return err(new GetUserIdErr.Unknown({ reason: `Unsupported response version ${response.tag}` }));
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
requestLogin(reason) {
|
|
23
|
+
return hostApi
|
|
24
|
+
.requestLogin(enumValue('v1', reason))
|
|
25
|
+
.mapErr(e => e.value)
|
|
26
|
+
.andThen(response => {
|
|
27
|
+
if (isEnumVariant(response, 'v1')) {
|
|
28
|
+
return ok(response.value);
|
|
29
|
+
}
|
|
30
|
+
// @ts-expect-error response.tag is never here
|
|
31
|
+
return err(new LoginErr.Unknown({ reason: `Unsupported response version ${response.tag}` }));
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
getProductAccount(dotNsIdentifier, derivationIndex = 0) {
|
|
35
|
+
return hostApi
|
|
36
|
+
.accountGet(enumValue('v1', [dotNsIdentifier, derivationIndex]))
|
|
37
|
+
.mapErr(e => e.value)
|
|
38
|
+
.andThen(response => {
|
|
39
|
+
if (isEnumVariant(response, 'v1')) {
|
|
40
|
+
return ok({
|
|
41
|
+
publicKey: response.value.publicKey,
|
|
42
|
+
dotNsIdentifier,
|
|
43
|
+
derivationIndex,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// @ts-expect-error response.tag is never here
|
|
47
|
+
return err(new RequestCredentialsErr.Unknown({ reason: `Unsupported response version ${response.tag}` }));
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
getProductAccountAlias(dotNsIdentifier, derivationIndex = 0) {
|
|
51
|
+
return hostApi
|
|
52
|
+
.accountGetAlias(enumValue('v1', [dotNsIdentifier, derivationIndex]))
|
|
53
|
+
.mapErr(e => e.value)
|
|
54
|
+
.andThen(response => {
|
|
55
|
+
if (isEnumVariant(response, 'v1')) {
|
|
56
|
+
return ok(response.value);
|
|
57
|
+
}
|
|
58
|
+
// @ts-expect-error response.tag is never here
|
|
59
|
+
return err(new RequestCredentialsErr.Unknown({ reason: `Unsupported response version ${response.tag}` }));
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
getLegacyAccounts() {
|
|
63
|
+
return hostApi
|
|
64
|
+
.getLegacyAccounts(enumValue('v1', undefined))
|
|
65
|
+
.mapErr(e => e.value)
|
|
66
|
+
.andThen(response => {
|
|
67
|
+
if (isEnumVariant(response, 'v1')) {
|
|
68
|
+
return ok(response.value);
|
|
69
|
+
}
|
|
70
|
+
// @ts-expect-error response.tag is never here
|
|
71
|
+
return err(new RequestCredentialsErr.Unknown({ reason: `Unsupported response version ${response.tag}` }));
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
createRingVRFProof(dotNsIdentifier, derivationIndex = 0, location, message) {
|
|
75
|
+
return hostApi
|
|
76
|
+
.accountCreateProof(enumValue('v1', [[dotNsIdentifier, derivationIndex], location, message]))
|
|
77
|
+
.mapErr(e => e.value)
|
|
78
|
+
.andThen(response => {
|
|
79
|
+
if (isEnumVariant(response, 'v1')) {
|
|
80
|
+
return ok(response.value);
|
|
81
|
+
}
|
|
82
|
+
// @ts-expect-error response.tag is never here
|
|
83
|
+
return err(new CreateProofErr.Unknown({ reason: `Unsupported response version ${response.tag}` }));
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
/**
|
|
87
|
+
* Builds a `PolkadotSigner` that delegates to the host via `host_create_transaction`.
|
|
88
|
+
*
|
|
89
|
+
* The factory is async because `PolkadotSigner.publicKey` must be a synchronous
|
|
90
|
+
* `Uint8Array` on the returned object — it is fetched up front via `host_account_get`.
|
|
91
|
+
*/
|
|
92
|
+
getProductAccountSigner(account, signerType = 'signPayload') {
|
|
93
|
+
const hostApi = createHostApi(transport);
|
|
94
|
+
const productAccountId = [account.dotNsIdentifier, account.derivationIndex];
|
|
95
|
+
/**
|
|
96
|
+
* @deprecated added for backward compatibility
|
|
97
|
+
*/
|
|
98
|
+
if (signerType === 'signPayload') {
|
|
99
|
+
return getPolkadotSignerFromPjs(toHex(account.publicKey), async (payload) => {
|
|
100
|
+
const codecPayload = {
|
|
101
|
+
account: [account.dotNsIdentifier, account.derivationIndex],
|
|
102
|
+
payload: buildSigningPayloadFields(payload),
|
|
103
|
+
};
|
|
104
|
+
const response = await hostApi.signPayload(enumValue('v1', codecPayload));
|
|
105
|
+
return response.match(response => {
|
|
106
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
107
|
+
return {
|
|
108
|
+
id: 0,
|
|
109
|
+
signature: response.value.signature,
|
|
110
|
+
signedTransaction: response.value.signedTransaction,
|
|
111
|
+
};
|
|
112
|
+
}, err => {
|
|
113
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
114
|
+
throw err.value;
|
|
115
|
+
});
|
|
116
|
+
}, async (raw) => {
|
|
117
|
+
const payload = {
|
|
118
|
+
account: [account.dotNsIdentifier, account.derivationIndex],
|
|
119
|
+
payload: raw.type === 'bytes'
|
|
120
|
+
? {
|
|
121
|
+
tag: 'Bytes',
|
|
122
|
+
value: fromHex(asHex(raw.data)),
|
|
123
|
+
}
|
|
124
|
+
: {
|
|
125
|
+
tag: 'Payload',
|
|
126
|
+
value: raw.data,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
const response = await hostApi.signRaw(enumValue('v1', payload));
|
|
130
|
+
return response.match(response => {
|
|
131
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
132
|
+
return {
|
|
133
|
+
id: 0,
|
|
134
|
+
signature: response.value.signature,
|
|
135
|
+
signedTransaction: response.value.signedTransaction,
|
|
136
|
+
};
|
|
137
|
+
}, err => {
|
|
138
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
139
|
+
throw err.value;
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
publicKey: account.publicKey,
|
|
145
|
+
async signTx(callData, signedExtensions, metadata) {
|
|
146
|
+
const decMeta = unifyMetadata(decAnyMetadata(metadata));
|
|
147
|
+
const { version: versions } = decMeta.extrinsic;
|
|
148
|
+
const latestVersion = versions.reduce((acc, v) => Math.max(acc, v), 0);
|
|
149
|
+
const txExtVersion = latestVersion === 4 ? 0 : latestVersion;
|
|
150
|
+
const checkGenesis = signedExtensions['CheckGenesis'];
|
|
151
|
+
if (!checkGenesis) {
|
|
152
|
+
throw new Error("Can't find genesis hash on transaction");
|
|
153
|
+
}
|
|
154
|
+
const txPayload = {
|
|
155
|
+
signer: productAccountId,
|
|
156
|
+
genesisHash: checkGenesis.additionalSigned,
|
|
157
|
+
callData,
|
|
158
|
+
extensions: Object.values(signedExtensions).map(({ identifier, value, additionalSigned }) => ({
|
|
159
|
+
id: identifier,
|
|
160
|
+
extra: value,
|
|
161
|
+
additionalSigned: additionalSigned,
|
|
162
|
+
})),
|
|
163
|
+
txExtVersion,
|
|
164
|
+
};
|
|
165
|
+
const response = await hostApi.createTransaction(enumValue('v1', txPayload));
|
|
166
|
+
return response.match(response => {
|
|
167
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
168
|
+
return response.value;
|
|
169
|
+
}, err => {
|
|
170
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
171
|
+
throw err.value;
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
async signBytes(data) {
|
|
175
|
+
const response = await hostApi.signRaw(enumValue('v1', {
|
|
176
|
+
account: productAccountId,
|
|
177
|
+
payload: { tag: 'Bytes', value: data },
|
|
178
|
+
}));
|
|
179
|
+
return response.match(response => {
|
|
180
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
181
|
+
return fromHex(response.value.signature);
|
|
182
|
+
}, err => {
|
|
183
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
184
|
+
throw err.value;
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
subscribeAccountConnectionStatus(callback) {
|
|
190
|
+
const subscriber = hostApi.accountConnectionStatusSubscribe(enumValue('v1', undefined), status => {
|
|
191
|
+
if (status.tag === 'v1') {
|
|
192
|
+
callback(status.value);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
unsubscribe: subscriber.unsubscribe,
|
|
197
|
+
onInterrupt: cb => subscriber.onInterrupt(v => cb(v.value)),
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
getLegacyAccountSigner(account) {
|
|
201
|
+
return getPolkadotSignerFromPjs(toHex(account.publicKey), async (payload) => {
|
|
202
|
+
const codecPayload = {
|
|
203
|
+
signer: payload.address,
|
|
204
|
+
payload: buildSigningPayloadFields(payload),
|
|
205
|
+
};
|
|
206
|
+
const response = await hostApi.signPayloadWithLegacyAccount(enumValue('v1', codecPayload));
|
|
207
|
+
return response.match(response => {
|
|
208
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
209
|
+
return {
|
|
210
|
+
id: 0,
|
|
211
|
+
signature: response.value.signature,
|
|
212
|
+
signedTransaction: response.value.signedTransaction,
|
|
213
|
+
};
|
|
214
|
+
}, err => {
|
|
215
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
216
|
+
throw err.value;
|
|
217
|
+
});
|
|
218
|
+
}, async (raw) => {
|
|
219
|
+
const payload = {
|
|
220
|
+
signer: raw.address,
|
|
221
|
+
payload: { tag: 'Bytes', value: fromHex(asHex(raw.data)) },
|
|
222
|
+
};
|
|
223
|
+
const response = await hostApi.signRawWithLegacyAccount(enumValue('v1', payload));
|
|
224
|
+
return response.match(response => {
|
|
225
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
226
|
+
return {
|
|
227
|
+
id: 0,
|
|
228
|
+
signature: response.value.signature,
|
|
229
|
+
signedTransaction: response.value.signedTransaction,
|
|
230
|
+
};
|
|
231
|
+
}, err => {
|
|
232
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
233
|
+
throw err.value;
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
export const accounts = createAccountsProvider();
|
|
240
|
+
function asHex(v) {
|
|
241
|
+
if (v.startsWith('0x'))
|
|
242
|
+
return v;
|
|
243
|
+
return `0x${v}`;
|
|
244
|
+
}
|
|
245
|
+
function buildSigningPayloadFields(payload) {
|
|
246
|
+
return {
|
|
247
|
+
blockHash: asHex(payload.blockHash),
|
|
248
|
+
blockNumber: asHex(payload.blockNumber),
|
|
249
|
+
era: asHex(payload.era),
|
|
250
|
+
genesisHash: asHex(payload.genesisHash),
|
|
251
|
+
nonce: asHex(payload.nonce),
|
|
252
|
+
method: asHex(payload.method),
|
|
253
|
+
specVersion: asHex(payload.specVersion),
|
|
254
|
+
transactionVersion: asHex(payload.transactionVersion),
|
|
255
|
+
metadataHash: payload.metadataHash ? asHex(payload.metadataHash) : undefined,
|
|
256
|
+
tip: asHex(payload.tip),
|
|
257
|
+
assetId: payload.assetId !== undefined ? payload.assetId : undefined,
|
|
258
|
+
mode: payload.mode,
|
|
259
|
+
withSignedTransaction: payload.withSignedTransaction,
|
|
260
|
+
signedExtensions: payload.signedExtensions,
|
|
261
|
+
version: payload.version,
|
|
262
|
+
};
|
|
263
|
+
}
|
package/dist/chat.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ChatBotRegistrationStatus as ChatBotRegistrationStatusCodec, ChatMessageContent as ChatMessageContentCodec, ChatRoom as ChatRoomCodec, ChatRoomRegistrationStatus as ChatRoomRegistrationStatusCodec, CodecType, ReceivedChatAction as ReceivedChatActionCodec, Subscription, Transport } from '@novasamatech/host-api';
|
|
2
|
+
import { CustomRendererNode } from '@novasamatech/host-api';
|
|
3
|
+
export type ChatMessageContent = CodecType<typeof ChatMessageContentCodec>;
|
|
4
|
+
export type ChatReceivedAction = CodecType<typeof ReceivedChatActionCodec>;
|
|
5
|
+
export type ChatRoomRegistrationResult = CodecType<typeof ChatRoomRegistrationStatusCodec>;
|
|
6
|
+
export type ChatBotRegistrationResult = CodecType<typeof ChatBotRegistrationStatusCodec>;
|
|
7
|
+
export type ChatRoom = CodecType<typeof ChatRoomCodec>;
|
|
8
|
+
export type ChatCustomMessageRenderer = (params: ChatCustomMessageRendererParams, render: (node: CodecType<typeof CustomRendererNode>) => void) => VoidFunction;
|
|
9
|
+
export type ChatCustomMessageRendererParams<T = Uint8Array> = {
|
|
10
|
+
messageId: string;
|
|
11
|
+
messageType: string;
|
|
12
|
+
payload: T;
|
|
13
|
+
subscribeActions(callback: (actionId: string, payload: Uint8Array | undefined) => void): VoidFunction;
|
|
14
|
+
};
|
|
15
|
+
export declare const createProductChatManager: (transport?: Transport) => {
|
|
16
|
+
registerRoom(params: {
|
|
17
|
+
roomId: string;
|
|
18
|
+
name: string;
|
|
19
|
+
icon: string;
|
|
20
|
+
}): Promise<"New" | "Exists">;
|
|
21
|
+
registerBot(params: {
|
|
22
|
+
botId: string;
|
|
23
|
+
name: string;
|
|
24
|
+
icon: string;
|
|
25
|
+
}): Promise<"New" | "Exists">;
|
|
26
|
+
sendMessage(roomId: string, payload: ChatMessageContent): Promise<{
|
|
27
|
+
messageId: string;
|
|
28
|
+
}>;
|
|
29
|
+
subscribeChatList(callback: (rooms: ChatRoom[]) => void): Subscription<void>;
|
|
30
|
+
subscribeAction(callback: (action: ChatReceivedAction) => void): Subscription<void>;
|
|
31
|
+
onCustomMessageRenderingRequest(callback: ChatCustomMessageRenderer): VoidFunction;
|
|
32
|
+
};
|
|
33
|
+
export declare function matchChatCustomRenderers(map: Record<string, ChatCustomMessageRenderer>): ChatCustomMessageRenderer;
|
package/dist/chat.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { CustomRendererNode, createHostApi, enumValue } from '@novasamatech/host-api';
|
|
2
|
+
import { sandboxTransport } from './sandboxTransport.js';
|
|
3
|
+
export const createProductChatManager = (transport = sandboxTransport) => {
|
|
4
|
+
const hostApi = createHostApi(transport);
|
|
5
|
+
const roomRegistrationStatus = {};
|
|
6
|
+
const botRegistrationStatus = {};
|
|
7
|
+
const chat = {
|
|
8
|
+
async registerRoom(params) {
|
|
9
|
+
const existingRegistration = roomRegistrationStatus[params.roomId];
|
|
10
|
+
if (existingRegistration) {
|
|
11
|
+
return existingRegistration;
|
|
12
|
+
}
|
|
13
|
+
const result = await hostApi.chatCreateRoom(enumValue('v1', params));
|
|
14
|
+
return result.match(payload => {
|
|
15
|
+
switch (payload.tag) {
|
|
16
|
+
case 'v1': {
|
|
17
|
+
roomRegistrationStatus[params.roomId] = payload.value.status;
|
|
18
|
+
return payload.value.status;
|
|
19
|
+
}
|
|
20
|
+
default:
|
|
21
|
+
throw new Error(`Unknown message version ${payload.tag}`);
|
|
22
|
+
}
|
|
23
|
+
}, err => {
|
|
24
|
+
throw err.value;
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
async registerBot(params) {
|
|
28
|
+
const existingRegistration = botRegistrationStatus[params.botId];
|
|
29
|
+
if (existingRegistration) {
|
|
30
|
+
return existingRegistration;
|
|
31
|
+
}
|
|
32
|
+
const result = await hostApi.chatRegisterBot(enumValue('v1', params));
|
|
33
|
+
return result.match(payload => {
|
|
34
|
+
switch (payload.tag) {
|
|
35
|
+
case 'v1': {
|
|
36
|
+
botRegistrationStatus[params.botId] = payload.value.status;
|
|
37
|
+
return payload.value.status;
|
|
38
|
+
}
|
|
39
|
+
default:
|
|
40
|
+
throw new Error(`Unknown message version ${payload.tag}`);
|
|
41
|
+
}
|
|
42
|
+
}, err => {
|
|
43
|
+
throw err.value;
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
async sendMessage(roomId, payload) {
|
|
47
|
+
const result = await hostApi.chatPostMessage(enumValue('v1', { roomId, payload }));
|
|
48
|
+
return result.match(payload => {
|
|
49
|
+
switch (payload.tag) {
|
|
50
|
+
case 'v1': {
|
|
51
|
+
return { messageId: payload.value.messageId };
|
|
52
|
+
}
|
|
53
|
+
default:
|
|
54
|
+
throw new Error(`Unknown message version ${payload.tag}`);
|
|
55
|
+
}
|
|
56
|
+
}, err => {
|
|
57
|
+
throw err.value;
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
subscribeChatList(callback) {
|
|
61
|
+
const subscriber = hostApi.chatListSubscribe(enumValue('v1', undefined), action => {
|
|
62
|
+
if (action.tag === 'v1') {
|
|
63
|
+
callback(action.value);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
unsubscribe: subscriber.unsubscribe,
|
|
68
|
+
onInterrupt: cb => subscriber.onInterrupt(v => cb(v.value)),
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
subscribeAction(callback) {
|
|
72
|
+
const subscriber = hostApi.chatActionSubscribe(enumValue('v1', undefined), action => {
|
|
73
|
+
switch (action.tag) {
|
|
74
|
+
case 'v1':
|
|
75
|
+
callback(action.value);
|
|
76
|
+
break;
|
|
77
|
+
default:
|
|
78
|
+
console.error(`Unknown message version ${action.tag}`);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
unsubscribe: subscriber.unsubscribe,
|
|
83
|
+
onInterrupt: cb => subscriber.onInterrupt(v => cb(v.value)),
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
onCustomMessageRenderingRequest(callback) {
|
|
87
|
+
return transport.handleSubscription('product_chat_custom_message_render_subscribe', (params, send, interrupt) => {
|
|
88
|
+
if (params.tag !== 'v1') {
|
|
89
|
+
// unsupported version
|
|
90
|
+
interrupt(enumValue('v1', undefined));
|
|
91
|
+
return () => {
|
|
92
|
+
/* empty */
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const { messageId, messageType, payload } = params.value;
|
|
96
|
+
return callback({
|
|
97
|
+
messageId,
|
|
98
|
+
messageType,
|
|
99
|
+
payload,
|
|
100
|
+
subscribeActions(callback) {
|
|
101
|
+
const actionsSubscription = hostApi.chatActionSubscribe(enumValue('v1', undefined), action => {
|
|
102
|
+
if (action.tag === 'v1' &&
|
|
103
|
+
action.value.payload.tag === 'ActionTriggered' &&
|
|
104
|
+
action.value.payload.value.messageId === messageId) {
|
|
105
|
+
callback(action.value.payload.value.actionId, action.value.payload.value.payload);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return actionsSubscription.unsubscribe;
|
|
109
|
+
},
|
|
110
|
+
}, node => send(enumValue('v1', node)));
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
return chat;
|
|
115
|
+
};
|
|
116
|
+
export function matchChatCustomRenderers(map) {
|
|
117
|
+
return (params, render) => {
|
|
118
|
+
const { messageType } = params;
|
|
119
|
+
const renderer = map[messageType];
|
|
120
|
+
if (!renderer) {
|
|
121
|
+
throw new Error(`Renderer for message type ${messageType} is not defined`);
|
|
122
|
+
}
|
|
123
|
+
return renderer(params, render);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const Version: string;
|
|
2
|
+
export declare const SpektrExtensionName = "spektr";
|
|
3
|
+
export declare const WellKnownChain: {
|
|
4
|
+
readonly polkadotRelay: "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3";
|
|
5
|
+
readonly polkadotAssetHub: "0x68d56f15f85d3136970ec16946040bc1752654e906147f7e43e9d539d7c3de2f";
|
|
6
|
+
readonly kusamaRelay: "0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe";
|
|
7
|
+
readonly kusamaAssetHub: "0x48239ef607d7928874027a43a67689209727dfb3d3dc5e5b03a39bdc2eda771a";
|
|
8
|
+
readonly westendRelay: "0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e";
|
|
9
|
+
readonly westendAssetHub: "0x67f9723393ef76214df0118c34bbbd3dbebc8ed46a10973a8c969d48fe7598c9";
|
|
10
|
+
readonly rococo: "0x6408de7737c59c238890533af25896a2c20608d8b380bb01029acb392781063e";
|
|
11
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
2
|
+
export const Version = pkg.version;
|
|
3
|
+
export const SpektrExtensionName = 'spektr';
|
|
4
|
+
export const WellKnownChain = {
|
|
5
|
+
polkadotRelay: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3',
|
|
6
|
+
polkadotAssetHub: '0x68d56f15f85d3136970ec16946040bc1752654e906147f7e43e9d539d7c3de2f',
|
|
7
|
+
kusamaRelay: '0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe',
|
|
8
|
+
kusamaAssetHub: '0x48239ef607d7928874027a43a67689209727dfb3d3dc5e5b03a39bdc2eda771a',
|
|
9
|
+
westendRelay: '0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e',
|
|
10
|
+
westendAssetHub: '0x67f9723393ef76214df0118c34bbbd3dbebc8ed46a10973a8c969d48fe7598c9',
|
|
11
|
+
rococo: '0x6408de7737c59c238890533af25896a2c20608d8b380bb01029acb392781063e',
|
|
12
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ResultAsync } from 'neverthrow';
|
|
2
|
+
export declare function unwrapVersionedResult<OK, KO, V extends string>(version: V, result: ResultAsync<{
|
|
3
|
+
tag: V;
|
|
4
|
+
value: OK;
|
|
5
|
+
}, {
|
|
6
|
+
tag: V;
|
|
7
|
+
value: KO;
|
|
8
|
+
}>): ResultAsync<OK, Error | KO>;
|
|
9
|
+
export declare function resultToPromise<T>(result: ResultAsync<T, unknown>): Promise<T>;
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { err, ok } from 'neverthrow';
|
|
2
|
+
export function unwrapVersionedResult(version, result) {
|
|
3
|
+
return result
|
|
4
|
+
.orElse(payload => {
|
|
5
|
+
if (payload.tag !== version) {
|
|
6
|
+
return err(new Error(`Unsupported result version ${payload.tag}`));
|
|
7
|
+
}
|
|
8
|
+
return err(payload.value);
|
|
9
|
+
})
|
|
10
|
+
.andThen(payload => {
|
|
11
|
+
if (payload.tag !== version) {
|
|
12
|
+
return err(new Error(`Unsupported result version ${payload.tag}`));
|
|
13
|
+
}
|
|
14
|
+
return ok(payload.value);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export function resultToPromise(result) {
|
|
18
|
+
return new Promise((resolve, reject) => result.match(resolve, reject));
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const hostApi: import("@novasamatech/host-api").HostApi;
|
package/dist/hostApi.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export { SpektrExtensionName, WellKnownChain } from './constants.js';
|
|
2
|
+
export { sandboxProvider, sandboxTransport } from './sandboxTransport.js';
|
|
3
|
+
export { hostApi } from './hostApi.js';
|
|
4
|
+
export { createMetaProvider, metaProvider } from './metaProvider.js';
|
|
5
|
+
export { createLegacyExtensionEnableFactory, injectSpektrExtension } from './injectWeb3.js';
|
|
6
|
+
export { createPapiProvider } from './papiProvider.js';
|
|
7
|
+
export type { ChatBotRegistrationResult, ChatCustomMessageRenderer, ChatCustomMessageRendererParams, ChatMessageContent, ChatReceivedAction, ChatRoom, ChatRoomRegistrationResult, } from './chat.js';
|
|
8
|
+
export { createProductChatManager, matchChatCustomRenderers } from './chat.js';
|
|
9
|
+
export type { ProductAccountId, SignedStatement, Statement, StatementTopicFilter, StatementsPage, Topic, } from './statementStore.js';
|
|
10
|
+
export { createStatementStore } from './statementStore.js';
|
|
11
|
+
export type { AccountConnectionStatus, LegacyAccount, ProductAccount } from './accounts.js';
|
|
12
|
+
export { accounts, createAccountsProvider } from './accounts.js';
|
|
13
|
+
export type { ThemeMode } from './theme.js';
|
|
14
|
+
export { createThemeProvider } from './theme.js';
|
|
15
|
+
export { createLocalStorage, hostLocalStorage } from './localStorage.js';
|
|
16
|
+
export type { NotificationId, PushNotificationInput } from './notification.js';
|
|
17
|
+
export { createNotificationManager, notificationManager } from './notification.js';
|
|
18
|
+
export { createPreimageManager, preimageManager } from './preimage.js';
|
|
19
|
+
export type { PaymentBalance, PaymentStatus, TopUpSource } from './payments.js';
|
|
20
|
+
export { createPaymentManager, paymentManager } from './payments.js';
|
|
21
|
+
export { deriveEntropy } from './deriveEntropy.js';
|
|
22
|
+
export type { DevicePermissionKind, RemotePermissionItem } from './permission.js';
|
|
23
|
+
export { requestDevicePermission, requestPermission } from './permission.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { SpektrExtensionName, WellKnownChain } from './constants.js';
|
|
2
|
+
export { sandboxProvider, sandboxTransport } from './sandboxTransport.js';
|
|
3
|
+
export { hostApi } from './hostApi.js';
|
|
4
|
+
export { createMetaProvider, metaProvider } from './metaProvider.js';
|
|
5
|
+
export { createLegacyExtensionEnableFactory, injectSpektrExtension } from './injectWeb3.js';
|
|
6
|
+
export { createPapiProvider } from './papiProvider.js';
|
|
7
|
+
export { createProductChatManager, matchChatCustomRenderers } from './chat.js';
|
|
8
|
+
export { createStatementStore } from './statementStore.js';
|
|
9
|
+
export { accounts, createAccountsProvider } from './accounts.js';
|
|
10
|
+
export { createThemeProvider } from './theme.js';
|
|
11
|
+
export { createLocalStorage, hostLocalStorage } from './localStorage.js';
|
|
12
|
+
export { createNotificationManager, notificationManager } from './notification.js';
|
|
13
|
+
export { createPreimageManager, preimageManager } from './preimage.js';
|
|
14
|
+
export { createPaymentManager, paymentManager } from './payments.js';
|
|
15
|
+
export { deriveEntropy } from './deriveEntropy.js';
|
|
16
|
+
export { requestDevicePermission, requestPermission } from './permission.js';
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { HexString, Transport } from '@novasamatech/host-api';
|
|
2
|
+
import type { InjectedAccounts } from '@polkadot/extension-inject/types';
|
|
3
|
+
import type { SignerPayloadJSON, SignerPayloadRaw, SignerResult } from '@polkadot/types/types/extrinsic';
|
|
4
|
+
/**
|
|
5
|
+
* expected interface derived from specification
|
|
6
|
+
*/
|
|
7
|
+
export interface TxPayloadV1 {
|
|
8
|
+
/** Payload version. MUST be 1. */
|
|
9
|
+
version: 1;
|
|
10
|
+
/**
|
|
11
|
+
* Signer selection hint. Allows the implementer to identify which private-key / scheme to use.
|
|
12
|
+
* - Use a wallet-defined handle (e.g., address/SS58, account-name, etc). This identifier
|
|
13
|
+
* was previously made available to the consumer.
|
|
14
|
+
* - Set `null` to let the implementer pick the signer (or if the signer is implied).
|
|
15
|
+
*/
|
|
16
|
+
signer: string | null;
|
|
17
|
+
/**
|
|
18
|
+
* SCALE-encoded Call (module indicator + function indicator + params).
|
|
19
|
+
*/
|
|
20
|
+
callData: HexString;
|
|
21
|
+
/**
|
|
22
|
+
* Transaction extensions supplied by the caller (order irrelevant).
|
|
23
|
+
* The consumer SHOULD provide every extension that is relevant to them.
|
|
24
|
+
* The implementer MAY infer missing ones.
|
|
25
|
+
*/
|
|
26
|
+
extensions: Array<{
|
|
27
|
+
/** Identifier as defined in metadata (e.g., "CheckSpecVersion", "ChargeAssetTxPayment"). */
|
|
28
|
+
id: string;
|
|
29
|
+
/**
|
|
30
|
+
* Explicit "extra" to sign (goes into the extrinsic body).
|
|
31
|
+
* SCALE-encoded per the extension's "extra" type as defined in the metadata.
|
|
32
|
+
*/
|
|
33
|
+
extra: HexString;
|
|
34
|
+
/**
|
|
35
|
+
* "Implicit" data to sign (known by the chain, not included into the extrinsic body).
|
|
36
|
+
* SCALE-encoded per the extension's "additionalSigned" type as defined in the metadata.
|
|
37
|
+
*/
|
|
38
|
+
additionalSigned: HexString;
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* Transaction Extension Version.
|
|
42
|
+
* - For Extrinsic V4 MUST be 0.
|
|
43
|
+
* - For Extrinsic V5, set to any version supported by the runtime.
|
|
44
|
+
* The implementer:
|
|
45
|
+
* - MUST use this field to determine the required extensions for creating the extrinsic.
|
|
46
|
+
* - MAY use this field to infer missing extensions that the implementer could know how to handle.
|
|
47
|
+
*/
|
|
48
|
+
txExtVersion: number;
|
|
49
|
+
/**
|
|
50
|
+
* Context needed for decoding, display, and (optionally) inferring certain extensions.
|
|
51
|
+
*/
|
|
52
|
+
context: {
|
|
53
|
+
/**
|
|
54
|
+
* RuntimeMetadataPrefixed blob (SCALE), starting with ASCII "meta" magic (`0x6d657461`),
|
|
55
|
+
* then a metadata version (V14+). For V5+ versioned extensions, MUST provide V16+.
|
|
56
|
+
*/
|
|
57
|
+
metadata: HexString;
|
|
58
|
+
/**
|
|
59
|
+
* Native token display info (used by some implementers), also needed to compute
|
|
60
|
+
* the `CheckMetadataHash` value.
|
|
61
|
+
*/
|
|
62
|
+
tokenSymbol: string;
|
|
63
|
+
tokenDecimals: number;
|
|
64
|
+
/**
|
|
65
|
+
* Highest known block number to aid mortality UX.
|
|
66
|
+
*/
|
|
67
|
+
bestBlockHeight: number;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
interface Signer {
|
|
71
|
+
/**
|
|
72
|
+
* @description signs an extrinsic payload from a serialized form
|
|
73
|
+
*/
|
|
74
|
+
signPayload?: (payload: SignerPayloadJSON) => Promise<SignerResult>;
|
|
75
|
+
/**
|
|
76
|
+
* @description signs a raw payload, only the bytes data as supplied
|
|
77
|
+
*/
|
|
78
|
+
signRaw?: (raw: SignerPayloadRaw) => Promise<SignerResult>;
|
|
79
|
+
/**
|
|
80
|
+
* @description signs a transaction according to https://github.com/polkadot-js/api/issues/6213
|
|
81
|
+
*/
|
|
82
|
+
createTransaction?: (payload: TxPayloadV1) => Promise<HexString>;
|
|
83
|
+
}
|
|
84
|
+
interface Injected {
|
|
85
|
+
accounts: InjectedAccounts;
|
|
86
|
+
signer: Signer;
|
|
87
|
+
}
|
|
88
|
+
export declare function createLegacyExtensionEnableFactory(transport: Transport): Promise<(() => Promise<Injected>) | null>;
|
|
89
|
+
export declare function injectSpektrExtension(transport?: Transport | null): Promise<boolean>;
|
|
90
|
+
export {};
|