@novasamatech/product-sdk 0.5.4-8 → 0.5.4

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 CHANGED
@@ -9,6 +9,7 @@ Core features:
9
9
  - Generic injectWeb3 provider similar to [polkadot-js extension](https://polkadot.js.org/extension/)
10
10
  - Chat module integration
11
11
  - Statement store integration
12
+ - Accounts provider for product accounts and signing
12
13
  - Redirect [PAPI](https://papi.how/) requests to host application
13
14
  - Receive additional information from host application - supported chains, theme, etc.
14
15
 
@@ -72,7 +73,7 @@ function createPapiClient(): PolkadotClient {
72
73
  }
73
74
  ```
74
75
 
75
- ### Subscribing connection status
76
+ ### Subscribing host connection status
76
77
 
77
78
  ```ts
78
79
  import { metaProvider } from '@novasamatech/product-sdk';
@@ -85,32 +86,50 @@ const unsubscribe = metaProvider.subscribeConnectionStatus((status) => {
85
86
  ### Chat Integration
86
87
 
87
88
  ```ts
88
- import { createChat } from '@novasamatech/product-sdk';
89
+ import { createProductChatManager } from '@novasamatech/product-sdk';
89
90
 
90
- // Create chat instance
91
- const chat = createChat();
91
+ // Create manager instance
92
+ const chat = createProductChatManager();
92
93
 
93
- // Register your dapp as a chat contact
94
- const registrationStatus = await chat.register({
94
+ // Register your product as a chat contact
95
+ const roomRegistrationStatus = await chat.registerRoom({
96
+ roomId: 'my-product-room',
97
+ name: 'My Product',
98
+ icon: 'https://example.com/icon.png'
99
+ });
100
+
101
+ // Register your product as a chat bot
102
+ const botRegistrationStatus = await chat.registerBot({
103
+ botId: 'my-product-bot',
95
104
  name: 'My Product',
96
105
  icon: 'https://example.com/icon.png'
97
106
  });
98
107
 
99
108
  // Send a message
100
- const { messageId } = await chat.sendMessage({
109
+ const { messageId } = await chat.sendMessage('my-product-room', {
101
110
  tag: 'Text',
102
111
  value: 'Hello dear user!'
103
112
  });
104
113
 
105
- // Subscribe to chat actions (incoming messages, etc.)
114
+ // Subscribing to chat actions (incoming messages, etc.)
106
115
  const subscriber = chat.subscribeAction((action) => {
107
- if (action.tag === 'MessagePosted') {
116
+ console.log('Room:', action.roomId);
117
+ console.log('Sender:', action.peer);
118
+
119
+ const payload = action.payload;
120
+
121
+ if (payload.tag === 'MessagePosted') {
108
122
  console.log('Received message:', action.value);
109
123
  }
110
- if (action.tag === 'ActionTriggered') {
124
+ if (payload.tag === 'ActionTriggered') {
111
125
  console.log('User triggered action:', action.value)
112
126
  }
113
127
  });
128
+
129
+ // Subscribing to chat room list updates
130
+ const chatListSubscriber = chat.subscribeChatList((rooms) => {
131
+ console.log('Chat rooms updated:', rooms);
132
+ });
114
133
  ```
115
134
 
116
135
  **Note:** Messages sent before registration will be queued and sent automatically after successful registration.
@@ -163,3 +182,53 @@ await statementStore.submit(signedStatement);
163
182
  subscription.unsubscribe();
164
183
  ```
165
184
 
185
+ ### Accounts Provider
186
+
187
+ The Accounts Provider allows you to access product accounts and create signers for signing transactions.
188
+
189
+ ```ts
190
+ import { createAccountsProvider } from '@novasamatech/product-sdk';
191
+ import type { ProductAccount } from '@novasamatech/product-sdk';
192
+
193
+ // Create accounts provider instance
194
+ const accountsProvider = createAccountsProvider();
195
+
196
+ // Get a product account by DotNS identifier and derivation index
197
+ const accountResult = await accountsProvider.getProductAccount('product.dot', 0);
198
+
199
+ if (accountResult.isOk()) {
200
+ const account: ProductAccount = accountResult.value;
201
+ console.log('Public key:', account.publicKey);
202
+ }
203
+
204
+ // Get account alias
205
+ const aliasResult = await accountsProvider.getProductAccountAlias('product.dot', 0);
206
+
207
+ if (aliasResult.isOk()) {
208
+ console.log('Alias:', aliasResult.value);
209
+ }
210
+
211
+ // Get non-product accounts (external wallets)
212
+ const nonProductAccountsResult = await accountsProvider.getNonProductAccounts();
213
+
214
+ if (nonProductAccountsResult.isOk()) {
215
+ console.log('Non-product accounts:', nonProductAccountsResult.value);
216
+ }
217
+
218
+ // Create a signer for a product account (for use with PAPI)
219
+ const account: ProductAccount = {
220
+ dotNsIdentifier: 'product.dot',
221
+ derivationIndex: 0,
222
+ publicKey: new Uint8Array([/* ... */])
223
+ };
224
+ const signer = accountsProvider.getProductAccountSigner(account);
225
+
226
+ // Create a signer for a non-product account
227
+ const nonProductSigner = accountsProvider.getNonProductAccountSigner(account);
228
+
229
+ // PAPI transaction signing example
230
+
231
+ const productAccountSignedTx = await tx.signAndSubmit(signer);
232
+ const nonProductAccountSignedTx = await tx.signAndSubmit(nonProductSigner);
233
+ ```
234
+
package/dist/chat.d.ts CHANGED
@@ -1,16 +1,20 @@
1
- import type { ChatMessageContent as ChatMessageContentCodec, ChatRoom as ChatRoomCodec, ChatRoomRegistrationResult as ChatRoomRegistrationResultCodec, CodecType, ReceivedChatAction as ReceivedChatActionCodec, Transport } from '@novasamatech/host-api';
1
+ import type { ChatBotRegistrationStatus as ChatBotRegistrationStatusCodec, ChatMessageContent as ChatMessageContentCodec, ChatRoom as ChatRoomCodec, ChatRoomRegistrationStatus as ChatRoomRegistrationStatusCodec, CodecType, ReceivedChatAction as ReceivedChatActionCodec, Transport } from '@novasamatech/host-api';
2
2
  export type ChatMessageContent = CodecType<typeof ChatMessageContentCodec>;
3
3
  export type ReceivedChatAction = CodecType<typeof ReceivedChatActionCodec>;
4
- export type ChatRoomRegistrationResult = CodecType<typeof ChatRoomRegistrationResultCodec>;
4
+ export type ChatRoomRegistrationResult = CodecType<typeof ChatRoomRegistrationStatusCodec>;
5
+ export type ChatBotRegistrationResult = CodecType<typeof ChatBotRegistrationStatusCodec>;
5
6
  export type ChatRoom = CodecType<typeof ChatRoomCodec>;
6
- export declare const createChat: (transport?: Transport) => {
7
- register(params: {
7
+ export declare const createProductChatManager: (transport?: Transport) => {
8
+ registerRoom(params: {
8
9
  roomId: string;
9
10
  name: string;
10
11
  icon: string;
11
- }): Promise<{
12
- status: "New" | "Exists";
13
- }>;
12
+ }): Promise<"New" | "Exists">;
13
+ registerBot(params: {
14
+ botId: string;
15
+ name: string;
16
+ icon: string;
17
+ }): Promise<"New" | "Exists">;
14
18
  sendMessage(roomId: string, payload: ChatMessageContent): Promise<{
15
19
  messageId: string;
16
20
  }>;
package/dist/chat.js CHANGED
@@ -1,54 +1,61 @@
1
1
  import { createHostApi, enumValue } from '@novasamatech/host-api';
2
- import { promiseWithResolvers } from './helpers.js';
3
2
  import { sandboxTransport } from './sandboxTransport.js';
4
- promiseWithResolvers();
5
- export const createChat = (transport = sandboxTransport) => {
3
+ export const createProductChatManager = (transport = sandboxTransport) => {
6
4
  const hostApi = createHostApi(transport);
7
- let registrationStatus = null;
8
- const messageQueue = [];
5
+ const roomRegistrationStatus = {};
6
+ const botRegistrationStatus = {};
9
7
  const chat = {
10
- async register(params) {
11
- if (registrationStatus) {
12
- return registrationStatus;
8
+ async registerRoom(params) {
9
+ const existingRegistration = roomRegistrationStatus[params.roomId];
10
+ if (existingRegistration) {
11
+ return existingRegistration;
13
12
  }
14
13
  const result = await hostApi.chatCreateRoom(enumValue('v1', params));
15
14
  return result.match(payload => {
16
- if (payload.tag === 'v1') {
17
- registrationStatus = payload.value;
18
- if (messageQueue.length > 0) {
19
- messageQueue.forEach(({ roomId, content, resolve, reject }) => {
20
- chat.sendMessage(roomId, content).then(resolve, reject);
21
- });
22
- messageQueue.length = 0;
15
+ switch (payload.tag) {
16
+ case 'v1': {
17
+ roomRegistrationStatus[params.roomId] = payload.value.status;
18
+ return payload.value.status;
23
19
  }
24
- return registrationStatus;
20
+ default:
21
+ throw new Error(`Unknown message version ${payload.tag}`);
25
22
  }
26
- else {
27
- throw new Error(`Unknown message version ${payload.tag}`);
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}`);
28
41
  }
29
42
  }, err => {
30
43
  throw err.value;
31
44
  });
32
45
  },
33
46
  async sendMessage(roomId, payload) {
34
- if (registrationStatus) {
35
- const result = await hostApi.chatPostMessage(enumValue('v1', { roomId, payload }));
36
- return result.match(payload => {
37
- if (payload.tag === 'v1') {
47
+ const result = await hostApi.chatPostMessage(enumValue('v1', { roomId, payload }));
48
+ return result.match(payload => {
49
+ switch (payload.tag) {
50
+ case 'v1': {
38
51
  return { messageId: payload.value.messageId };
39
52
  }
40
- else {
53
+ default:
41
54
  throw new Error(`Unknown message version ${payload.tag}`);
42
- }
43
- }, err => {
44
- throw err.value;
45
- });
46
- }
47
- else {
48
- const { promise, resolve, reject } = promiseWithResolvers();
49
- messageQueue.push({ roomId, content: payload, resolve, reject });
50
- return promise;
51
- }
55
+ }
56
+ }, err => {
57
+ throw err.value;
58
+ });
52
59
  },
53
60
  subscribeChatList(callback) {
54
61
  return hostApi.chatListSubscribe(enumValue('v1', undefined), action => {
@@ -59,8 +66,12 @@ export const createChat = (transport = sandboxTransport) => {
59
66
  },
60
67
  subscribeAction(callback) {
61
68
  return hostApi.chatActionSubscribe(enumValue('v1', undefined), action => {
62
- if (action.tag === 'v1') {
63
- callback(action.value);
69
+ switch (action.tag) {
70
+ case 'v1':
71
+ callback(action.value);
72
+ break;
73
+ default:
74
+ console.error(`Unknown message version ${action.tag}`);
64
75
  }
65
76
  });
66
77
  },
package/dist/index.d.ts CHANGED
@@ -4,8 +4,8 @@ export { hostApi } from './hostApi.js';
4
4
  export { createMetaProvider, metaProvider } from './metaProvider.js';
5
5
  export { createNonProductExtensionEnableFactory, injectSpektrExtension } from './injectWeb3.js';
6
6
  export { createPapiProvider } from './papiProvider.js';
7
- export type { ChatMessageContent, ReceivedChatAction } from './chat.js';
8
- export { createChat } from './chat.js';
7
+ export type { ChatBotRegistrationResult, ChatMessageContent, ChatRoom, ChatRoomRegistrationResult, ReceivedChatAction, } from './chat.js';
8
+ export { createProductChatManager } from './chat.js';
9
9
  export type { ProductAccountId, SignedStatement, Statement, Topic } from './statementStore.js';
10
10
  export { createStatementStore } from './statementStore.js';
11
11
  export type { ProductAccount } from './accounts.js';
package/dist/index.js CHANGED
@@ -4,6 +4,6 @@ export { hostApi } from './hostApi.js';
4
4
  export { createMetaProvider, metaProvider } from './metaProvider.js';
5
5
  export { createNonProductExtensionEnableFactory, injectSpektrExtension } from './injectWeb3.js';
6
6
  export { createPapiProvider } from './papiProvider.js';
7
- export { createChat } from './chat.js';
7
+ export { createProductChatManager } from './chat.js';
8
8
  export { createStatementStore } from './statementStore.js';
9
9
  export { createAccountsProvider } from './accounts.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@novasamatech/product-sdk",
3
3
  "type": "module",
4
- "version": "0.5.4-8",
4
+ "version": "0.5.4",
5
5
  "description": "Polkadot product SDK: integrate and run your product inside Polkadot browser.",
6
6
  "license": "Apache-2.0",
7
7
  "repository": {
@@ -27,10 +27,10 @@
27
27
  ],
28
28
  "dependencies": {
29
29
  "@polkadot/extension-inject": "^0.62.6",
30
- "@polkadot-api/substrate-bindings": "^0.16.5",
30
+ "@polkadot-api/substrate-bindings": "^0.17.0",
31
31
  "@polkadot-api/json-rpc-provider": "^0.0.4",
32
32
  "@polkadot-api/json-rpc-provider-proxy": "^0.2.7",
33
- "@novasamatech/host-api": "0.5.4-8"
33
+ "@novasamatech/host-api": "0.5.4"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"