@trufnetwork/sdk-js 0.3.5 → 0.3.6
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 +11 -36
- package/dist/cjs/client/browserClient.cjs +1 -0
- package/dist/cjs/client/browserClient.cjs.map +2 -2
- package/dist/cjs/client/client.cjs +71 -11
- package/dist/cjs/client/client.cjs.map +2 -2
- package/dist/cjs/client/nodeClient.cjs +1 -0
- package/dist/cjs/client/nodeClient.cjs.map +2 -2
- package/dist/cjs/contracts-api/action.cjs +4 -2
- package/dist/cjs/contracts-api/action.cjs.map +2 -2
- package/dist/cjs/contracts-api/composedAction.cjs +3 -41
- package/dist/cjs/contracts-api/composedAction.cjs.map +2 -2
- package/dist/cjs/contracts-api/deployStream.cjs +0 -14
- package/dist/cjs/contracts-api/deployStream.cjs.map +2 -2
- package/dist/cjs/contracts-api/roleManagement.cjs +133 -0
- package/dist/cjs/contracts-api/roleManagement.cjs.map +7 -0
- package/dist/cjs/index.common.cjs +2 -0
- package/dist/cjs/index.common.cjs.map +2 -2
- package/dist/cjs/types/role.cjs +19 -0
- package/dist/cjs/types/role.cjs.map +7 -0
- package/dist/esm/client/browserClient.mjs +1 -0
- package/dist/esm/client/browserClient.mjs.map +2 -2
- package/dist/esm/client/client.mjs +71 -11
- package/dist/esm/client/client.mjs.map +2 -2
- package/dist/esm/client/nodeClient.mjs +1 -0
- package/dist/esm/client/nodeClient.mjs.map +2 -2
- package/dist/esm/contracts-api/action.mjs +4 -2
- package/dist/esm/contracts-api/action.mjs.map +2 -2
- package/dist/esm/contracts-api/composedAction.mjs +3 -45
- package/dist/esm/contracts-api/composedAction.mjs.map +2 -2
- package/dist/esm/contracts-api/deployStream.mjs +0 -14
- package/dist/esm/contracts-api/deployStream.mjs.map +2 -2
- package/dist/esm/contracts-api/roleManagement.mjs +112 -0
- package/dist/esm/contracts-api/roleManagement.mjs.map +7 -0
- package/dist/esm/index.common.mjs +2 -0
- package/dist/esm/index.common.mjs.map +2 -2
- package/dist/esm/types/role.mjs +1 -0
- package/dist/esm/types/role.mjs.map +7 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/client/browserClient.d.ts.map +1 -1
- package/dist/types/client/client.d.ts +39 -6
- package/dist/types/client/client.d.ts.map +1 -1
- package/dist/types/client/nodeClient.d.ts.map +1 -1
- package/dist/types/contracts-api/action.d.ts +1 -0
- package/dist/types/contracts-api/action.d.ts.map +1 -1
- package/dist/types/contracts-api/composedAction.d.ts +2 -4
- package/dist/types/contracts-api/composedAction.d.ts.map +1 -1
- package/dist/types/contracts-api/deployStream.d.ts +0 -1
- package/dist/types/contracts-api/deployStream.d.ts.map +1 -1
- package/dist/types/contracts-api/roleManagement.d.ts +41 -0
- package/dist/types/contracts-api/roleManagement.d.ts.map +1 -0
- package/dist/types/index.common.d.ts +2 -0
- package/dist/types/index.common.d.ts.map +1 -1
- package/dist/types/types/role.d.ts +33 -0
- package/dist/types/types/role.d.ts.map +1 -0
- package/package.json +3 -2
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
|
|
5
1
|
// src/contracts-api/composedAction.ts
|
|
6
2
|
import { Utils } from "@kwilteam/kwil-js";
|
|
7
3
|
import { EthereumAddress } from "../util/EthereumAddress.mjs";
|
|
@@ -12,10 +8,8 @@ var DataType = Utils.DataType;
|
|
|
12
8
|
var { Pool } = pg;
|
|
13
9
|
var ErrorStreamNotComposed = "stream is not a composed stream";
|
|
14
10
|
var ComposedAction = class _ComposedAction extends Action {
|
|
15
|
-
constructor(kwilClient, kwilSigner
|
|
11
|
+
constructor(kwilClient, kwilSigner) {
|
|
16
12
|
super(kwilClient, kwilSigner);
|
|
17
|
-
__publicField(this, "neonConnectionString");
|
|
18
|
-
this.neonConnectionString = neonConnectionString;
|
|
19
13
|
}
|
|
20
14
|
/**
|
|
21
15
|
* Returns the taxonomy of the stream
|
|
@@ -99,53 +93,17 @@ var ComposedAction = class _ComposedAction extends Action {
|
|
|
99
93
|
$start_date: DataType.Int
|
|
100
94
|
}
|
|
101
95
|
});
|
|
102
|
-
if (this.neonConnectionString) {
|
|
103
|
-
const pool = new Pool({ connectionString: this.neonConnectionString });
|
|
104
|
-
const parentProvider = taxonomy.stream.dataProvider.getAddress().toLowerCase();
|
|
105
|
-
const parentStreamId = taxonomy.stream.streamId.getId();
|
|
106
|
-
const startDateText = String(taxonomy.startDate);
|
|
107
|
-
for (const item of taxonomy.taxonomyItems) {
|
|
108
|
-
const childProvider = item.childStream.dataProvider.getAddress().toLowerCase();
|
|
109
|
-
const childStreamId = item.childStream.streamId.getId();
|
|
110
|
-
const weight = item.weight;
|
|
111
|
-
await pool.query(
|
|
112
|
-
`INSERT INTO taxonomies
|
|
113
|
-
(parent_data_provider, parent_stream_id,
|
|
114
|
-
child_data_provider, child_stream_id,
|
|
115
|
-
weight, start_date)
|
|
116
|
-
VALUES ($1, $2, $3, $4, $5, $6)
|
|
117
|
-
ON CONFLICT ON CONSTRAINT unique_parent_child DO NOTHING`,
|
|
118
|
-
[
|
|
119
|
-
parentProvider,
|
|
120
|
-
parentStreamId,
|
|
121
|
-
childProvider,
|
|
122
|
-
childStreamId,
|
|
123
|
-
weight,
|
|
124
|
-
startDateText
|
|
125
|
-
]
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
await pool.end();
|
|
129
|
-
console.log("Successfully inserted taxonomy into Explorer DB", {
|
|
130
|
-
parentStreamId,
|
|
131
|
-
childStreamId: childStreamIds,
|
|
132
|
-
weight: weights,
|
|
133
|
-
startDate: startDateText
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
96
|
return txHash;
|
|
137
97
|
}
|
|
138
98
|
/**
|
|
139
99
|
* Creates a ComposedStream from a base Stream
|
|
140
100
|
* @param stream The base stream to convert
|
|
141
|
-
* @param neonConnectionString The Neon connection string
|
|
142
101
|
* @returns A ComposedStream instance
|
|
143
102
|
*/
|
|
144
|
-
static fromStream(stream
|
|
103
|
+
static fromStream(stream) {
|
|
145
104
|
return new _ComposedAction(
|
|
146
105
|
stream["kwilClient"],
|
|
147
|
-
stream["kwilSigner"]
|
|
148
|
-
neonConnectionString
|
|
106
|
+
stream["kwilSigner"]
|
|
149
107
|
);
|
|
150
108
|
}
|
|
151
109
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/contracts-api/composedAction.ts"],
|
|
4
|
-
"sourcesContent": ["import {KwilSigner, NodeKwil, Utils, WebKwil} from \"@kwilteam/kwil-js\";\nimport { GenericResponse } from \"@kwilteam/kwil-js/dist/core/resreq\";\nimport { TxReceipt } from \"@kwilteam/kwil-js/dist/core/tx\";\nimport { DateString } from \"../types/other\";\nimport { StreamLocator } from \"../types/stream\";\nimport { EthereumAddress } from \"../util/EthereumAddress\";\nimport { StreamId } from \"../util/StreamId\";\nimport { Action } from \"./action\";\nimport DataType = Utils.DataType;\nimport pg from \"pg\";\nconst { Pool } = pg;\n\nexport const ErrorStreamNotComposed = \"stream is not a composed stream\";\n\nexport interface TaxonomySet {\n stream: StreamLocator;\n taxonomyItems: TaxonomyItem[];\n startDate: number;\n}\n\nexport interface TaxonomyItem {\n childStream: StreamLocator;\n weight: string;\n}\n\nexport interface DescribeTaxonomiesParams {\n stream: StreamLocator;\n /**\n * if true, will return the latest version of the taxonomy only\n */\n latestGroupSequence: boolean;\n}\n\nexport class ComposedAction extends Action {\n
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import {KwilSigner, NodeKwil, Utils, WebKwil} from \"@kwilteam/kwil-js\";\nimport { GenericResponse } from \"@kwilteam/kwil-js/dist/core/resreq\";\nimport { TxReceipt } from \"@kwilteam/kwil-js/dist/core/tx\";\nimport { DateString } from \"../types/other\";\nimport { StreamLocator } from \"../types/stream\";\nimport { EthereumAddress } from \"../util/EthereumAddress\";\nimport { StreamId } from \"../util/StreamId\";\nimport { Action } from \"./action\";\nimport DataType = Utils.DataType;\nimport pg from \"pg\";\nconst { Pool } = pg;\n\nexport const ErrorStreamNotComposed = \"stream is not a composed stream\";\n\nexport interface TaxonomySet {\n stream: StreamLocator;\n taxonomyItems: TaxonomyItem[];\n startDate: number;\n}\n\nexport interface TaxonomyItem {\n childStream: StreamLocator;\n weight: string;\n}\n\nexport interface DescribeTaxonomiesParams {\n stream: StreamLocator;\n /**\n * if true, will return the latest version of the taxonomy only\n */\n latestGroupSequence: boolean;\n}\n\nexport class ComposedAction extends Action {\n constructor(\n kwilClient: WebKwil | NodeKwil,\n kwilSigner: KwilSigner,\n ) {\n super(kwilClient, kwilSigner);\n }\n\n /**\n * Returns the taxonomy of the stream\n * @param params Parameters for describing taxonomies\n * @returns A promise that resolves to the taxonomy\n */\n public async describeTaxonomies(\n params: DescribeTaxonomiesParams,\n ): Promise<TaxonomySet[]> {\n type TaxonomyResult = {\n data_provider: string;\n stream_id: string;\n child_data_provider: string;\n child_stream_id: string;\n weight: string;\n created_at: number;\n group_sequence: number;\n start_date: number;\n }[];\n\n const result = await this.call<TaxonomyResult>(\n \"describe_taxonomies\",\n {\n $data_provider: params.stream.dataProvider.getAddress(),\n $stream_id: params.stream.streamId.getId(),\n $latest_group_sequence: params.latestGroupSequence,\n },\n );\n\n\n\n return result\n .mapRight((records) => {\n const taxonomyItems: Map<DateString, TaxonomyItem[]> = records.reduce(\n (acc, record) => {\n const currentArray = acc.get(record.start_date.toString()) || [];\n currentArray.push({\n childStream: {\n streamId: StreamId.fromString(record.child_stream_id).throw(),\n dataProvider: EthereumAddress.fromString(\n record.child_data_provider,\n ).throw(),\n },\n weight: record.weight,\n });\n acc.set(record.start_date.toString(), currentArray);\n return acc;\n },\n new Map<DateString, TaxonomyItem[]>(),\n );\n\n return Array.from(taxonomyItems.entries()).map(\n ([startDate, taxonomyItems]) => ({\n stream: {\n streamId: StreamId.fromString(records[0].stream_id).throw(),\n dataProvider: EthereumAddress.fromString(\n records[0].data_provider,\n ).throw(),\n },\n taxonomyItems,\n startDate: Number(startDate)\n }),\n );\n })\n .throw();\n }\n\n /**\n * Sets the taxonomy of the stream\n * @param taxonomy The taxonomy to set\n * @returns A promise that resolves to the transaction receipt\n */\n public async setTaxonomy(\n taxonomy: TaxonomySet,\n ): Promise<GenericResponse<TxReceipt>> {\n const childDataProviders: string[] = [];\n const childStreamIds: string[] = [];\n const weights: string[] = [];\n\n for (const item of taxonomy.taxonomyItems) {\n childDataProviders.push(item.childStream.dataProvider\n .getAddress());\n childStreamIds.push(item.childStream.streamId.getId());\n weights.push(item.weight.toString());\n }\n\n const txHash = await this.executeWithActionBody({\n namespace: \"main\",\n name: \"insert_taxonomy\",\n inputs: [\n {\n $data_provider: taxonomy.stream.dataProvider.getAddress(),\n $stream_id: taxonomy.stream.streamId.getId(),\n $child_data_providers: childDataProviders,\n $child_stream_ids: childStreamIds,\n $weights: weights,\n $start_date: taxonomy.startDate\n },\n ],\n types: {\n $data_provider: DataType.Text,\n $stream_id: DataType.Text,\n $child_data_providers: DataType.TextArray,\n $child_stream_ids: DataType.TextArray,\n $weights: DataType.NumericArray(36,18),\n $start_date: DataType.Int\n }});\n\n return txHash;\n }\n\n /**\n * Creates a ComposedStream from a base Stream\n * @param stream The base stream to convert\n * @returns A ComposedStream instance\n */\n public static fromStream(stream: Action): ComposedAction {\n return new ComposedAction(\n stream[\"kwilClient\"],\n stream[\"kwilSigner\"],\n );\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAA8B,aAAqB;AAKnD,SAAS,uBAAuB;AAChC,SAAS,gBAAgB;AACzB,SAAS,cAAc;AAEvB,OAAO,QAAQ;AADf,IAAO,WAAW,MAAM;AAExB,IAAM,EAAE,KAAK,IAAI;AAEV,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EACX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAa,YACX,UACqC;AACrC,UAAM,qBAA+B,CAAC;AACtC,UAAM,iBAA2B,CAAC;AAClC,UAAM,UAAoB,CAAC;AAE3B,eAAW,QAAQ,SAAS,eAAe;AACzC,yBAAmB,KAAK,KAAK,YAAY,aACpC,WAAW,CAAC;AACjB,qBAAe,KAAK,KAAK,YAAY,SAAS,MAAM,CAAC;AACrD,cAAQ,KAAK,KAAK,OAAO,SAAS,CAAC;AAAA,IACrC;AAEA,UAAM,SAAS,MAAM,KAAK,sBAAsB;AAAA,MAC5C,WAAW;AAAA,MACX,MAAM;AAAA,MACN,QAAQ;AAAA,QACN;AAAA,UACE,gBAAgB,SAAS,OAAO,aAAa,WAAW;AAAA,UACxD,YAAY,SAAS,OAAO,SAAS,MAAM;AAAA,UAC3C,uBAAuB;AAAA,UACvB,mBAAmB;AAAA,UACnB,UAAU;AAAA,UACV,aAAa,SAAS;AAAA,QACxB;AAAA,MACF;AAAA,MACA,OAAO;AAAA,QACL,gBAAgB,SAAS;AAAA,QACzB,YAAY,SAAS;AAAA,QACrB,uBAAuB,SAAS;AAAA,QAChC,mBAAmB,SAAS;AAAA,QAC5B,UAAU,SAAS,aAAa,IAAG,EAAE;AAAA,QACrC,aAAa,SAAS;AAAA,MACxB;AAAA,IAAC,CAAC;AAEN,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAc,WAAW,QAAgC;AACvD,WAAO,IAAI;AAAA,MACT,OAAO,YAAY;AAAA,MACnB,OAAO,YAAY;AAAA,IACrB;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["taxonomyItems"]
|
|
7
7
|
}
|
|
@@ -16,20 +16,6 @@ async function deployStream(input) {
|
|
|
16
16
|
input.kwilSigner,
|
|
17
17
|
input.synchronous
|
|
18
18
|
);
|
|
19
|
-
if (input.neonConnectionString) {
|
|
20
|
-
console.log("Neon connection detected, attempting to insert into DB...");
|
|
21
|
-
const signer = input.kwilSigner.signer;
|
|
22
|
-
const dataProvider = signer.address.toLowerCase();
|
|
23
|
-
const pool = new Pool({ connectionString: input.neonConnectionString });
|
|
24
|
-
await pool.query(
|
|
25
|
-
`INSERT INTO streams (data_provider, stream_id, type, stream_name, display_name, categories, owner_wallet, geography, tags)
|
|
26
|
-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
27
|
-
ON CONFLICT (data_provider, stream_id) DO NOTHING`,
|
|
28
|
-
[dataProvider, input.streamId.getId(), input.streamType, input.streamId.getName(), input.streamId.getName(), "{External}", dataProvider, "Global", "{External}"]
|
|
29
|
-
);
|
|
30
|
-
await pool.end();
|
|
31
|
-
console.log("successfully inserted into Explorer DB", input.streamId.getName());
|
|
32
|
-
}
|
|
33
19
|
return txHash;
|
|
34
20
|
} catch (error) {
|
|
35
21
|
throw new Error(`Failed to deploy stream: ${error}`);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/contracts-api/deployStream.ts"],
|
|
4
|
-
"sourcesContent": ["import {StreamType} from \"./contractValues\";\nimport {TxReceipt} from \"@kwilteam/kwil-js/dist/core/tx\";\nimport {Kwil} from \"@kwilteam/kwil-js/dist/client/kwil\";\nimport {KwilSigner} from \"@kwilteam/kwil-js\";\nimport {GenericResponse} from \"@kwilteam/kwil-js/dist/core/resreq\";\nimport {StreamId} from \"../util/StreamId\";\nimport pg from \"pg\";\nconst { Pool } = pg;\n\nexport interface DeployStreamInput {\n streamId: StreamId;\n streamType: StreamType;\n kwilClient: Kwil<any>;\n kwilSigner: KwilSigner;\n synchronous?: boolean;\n
|
|
5
|
-
"mappings": ";AAMA,OAAO,QAAQ;AACf,IAAM,EAAE,KAAK,IAAI;
|
|
4
|
+
"sourcesContent": ["import {StreamType} from \"./contractValues\";\nimport {TxReceipt} from \"@kwilteam/kwil-js/dist/core/tx\";\nimport {Kwil} from \"@kwilteam/kwil-js/dist/client/kwil\";\nimport {KwilSigner} from \"@kwilteam/kwil-js\";\nimport {GenericResponse} from \"@kwilteam/kwil-js/dist/core/resreq\";\nimport {StreamId} from \"../util/StreamId\";\nimport pg from \"pg\";\nconst { Pool } = pg;\n\nexport interface DeployStreamInput {\n streamId: StreamId;\n streamType: StreamType;\n kwilClient: Kwil<any>;\n kwilSigner: KwilSigner;\n synchronous?: boolean;\n}\n\nexport interface DeployStreamOutput {\n receipt: TxReceipt;\n}\n\n/**\n * Deploys a stream to TN.\n * @param input - The input parameters for deploying the stream.\n * @returns The transaction hash of the deployment.\n */\nexport async function deployStream(\n input: DeployStreamInput,\n): Promise<GenericResponse<TxReceipt>> {\n try {\n const txHash = await input.kwilClient.execute(\n {\n namespace: \"main\",\n inputs: [{\n $stream_id: input.streamId.getId(),\n $stream_type: input.streamType,\n }],\n name: \"create_stream\",\n description: `TN SDK - Deploying ${input.streamType} stream: ${input.streamId.getId()}`\n },\n input.kwilSigner,\n input.synchronous,\n );\n\n return txHash;\n } catch (error) {\n throw new Error(`Failed to deploy stream: ${error}`);\n }\n}"],
|
|
5
|
+
"mappings": ";AAMA,OAAO,QAAQ;AACf,IAAM,EAAE,KAAK,IAAI;AAmBjB,eAAsB,aACpB,OACqC;AACrC,MAAI;AACA,UAAM,SAAS,MAAM,MAAM,WAAW;AAAA,MACpC;AAAA,QACI,WAAW;AAAA,QACX,QAAQ,CAAC;AAAA,UACL,YAAY,MAAM,SAAS,MAAM;AAAA,UACjC,cAAc,MAAM;AAAA,QACxB,CAAC;AAAA,QACD,MAAM;AAAA,QACN,aAAa,sBAAsB,MAAM,UAAU,YAAY,MAAM,SAAS,MAAM,CAAC;AAAA,MACzF;AAAA,MACA,MAAM;AAAA,MACN,MAAM;AAAA,IACV;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,UAAM,IAAI,MAAM,4BAA4B,KAAK,EAAE;AAAA,EACrD;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// src/contracts-api/roleManagement.ts
|
|
2
|
+
import { Utils } from "@kwilteam/kwil-js";
|
|
3
|
+
import { Action } from "./action.mjs";
|
|
4
|
+
var DataType = Utils.DataType;
|
|
5
|
+
var RoleManagement = class _RoleManagement extends Action {
|
|
6
|
+
constructor(kwilClient, kwilSigner) {
|
|
7
|
+
super(kwilClient, kwilSigner);
|
|
8
|
+
}
|
|
9
|
+
static normalizeOwner(owner) {
|
|
10
|
+
return owner === "system" ? "system" : owner.getAddress().toLowerCase();
|
|
11
|
+
}
|
|
12
|
+
static normalizeWallets(wallets) {
|
|
13
|
+
return wallets.map((w) => w.getAddress().toLowerCase());
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Grants a role to the provided wallets.
|
|
17
|
+
* This calls the `grant_roles` action.
|
|
18
|
+
*/
|
|
19
|
+
async grantRole(input, synchronous = false) {
|
|
20
|
+
return this.executeWithActionBody(
|
|
21
|
+
{
|
|
22
|
+
namespace: "main",
|
|
23
|
+
name: "grant_roles",
|
|
24
|
+
inputs: [
|
|
25
|
+
{
|
|
26
|
+
$owner: _RoleManagement.normalizeOwner(input.owner),
|
|
27
|
+
$role_name: input.roleName.toLowerCase(),
|
|
28
|
+
$wallets: _RoleManagement.normalizeWallets(input.wallets)
|
|
29
|
+
}
|
|
30
|
+
],
|
|
31
|
+
types: {
|
|
32
|
+
$owner: DataType.Text,
|
|
33
|
+
$role_name: DataType.Text,
|
|
34
|
+
$wallets: DataType.TextArray
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
synchronous
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Revokes a role from the provided wallets.
|
|
42
|
+
* This calls the `revoke_roles` action.
|
|
43
|
+
*/
|
|
44
|
+
async revokeRole(input, synchronous = false) {
|
|
45
|
+
return this.executeWithActionBody(
|
|
46
|
+
{
|
|
47
|
+
namespace: "main",
|
|
48
|
+
name: "revoke_roles",
|
|
49
|
+
inputs: [
|
|
50
|
+
{
|
|
51
|
+
$owner: _RoleManagement.normalizeOwner(input.owner),
|
|
52
|
+
$role_name: input.roleName.toLowerCase(),
|
|
53
|
+
$wallets: _RoleManagement.normalizeWallets(input.wallets)
|
|
54
|
+
}
|
|
55
|
+
],
|
|
56
|
+
types: {
|
|
57
|
+
$owner: DataType.Text,
|
|
58
|
+
$role_name: DataType.Text,
|
|
59
|
+
$wallets: DataType.TextArray
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
synchronous
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if the provided wallets are members of a role.
|
|
67
|
+
* This calls the `are_members_of` VIEW action.
|
|
68
|
+
*
|
|
69
|
+
* @returns an array matching the provided wallets order with membership flags.
|
|
70
|
+
*/
|
|
71
|
+
async areMembersOf(input) {
|
|
72
|
+
const result = await this.call(
|
|
73
|
+
"are_members_of",
|
|
74
|
+
{
|
|
75
|
+
$owner: _RoleManagement.normalizeOwner(input.owner),
|
|
76
|
+
$role_name: input.roleName.toLowerCase(),
|
|
77
|
+
$wallets: _RoleManagement.normalizeWallets(input.wallets)
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
return result.throw().map((row) => ({
|
|
81
|
+
wallet: row.wallet,
|
|
82
|
+
isMember: row.is_member
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Lists the members of a role with optional pagination.
|
|
87
|
+
* This calls the `list_role_members` VIEW action.
|
|
88
|
+
*/
|
|
89
|
+
async listRoleMembers(input) {
|
|
90
|
+
const result = await this.call("list_role_members", {
|
|
91
|
+
$owner: _RoleManagement.normalizeOwner(input.owner),
|
|
92
|
+
$role_name: input.roleName.toLowerCase(),
|
|
93
|
+
...input.limit !== void 0 ? { $limit: input.limit } : {},
|
|
94
|
+
...input.offset !== void 0 ? { $offset: input.offset } : {}
|
|
95
|
+
});
|
|
96
|
+
return result.throw().map((row) => ({
|
|
97
|
+
wallet: row.wallet,
|
|
98
|
+
grantedAt: Number(row.granted_at),
|
|
99
|
+
grantedBy: row.granted_by
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Helper factory mirroring the pattern used by the other action wrappers.
|
|
104
|
+
*/
|
|
105
|
+
static fromClient(kwilClient, kwilSigner) {
|
|
106
|
+
return new _RoleManagement(kwilClient, kwilSigner);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
export {
|
|
110
|
+
RoleManagement
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=roleManagement.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/contracts-api/roleManagement.ts"],
|
|
4
|
+
"sourcesContent": ["import { KwilSigner, NodeKwil, Utils, WebKwil } from \"@kwilteam/kwil-js\";\nimport { GenericResponse } from \"@kwilteam/kwil-js/dist/core/resreq\";\nimport { TxReceipt } from \"@kwilteam/kwil-js/dist/core/tx\";\nimport { Action } from \"./action\";\nimport { AreMembersOfInput, GrantRoleInput, RevokeRoleInput, WalletMembership } from \"../types/role\";\nimport DataType = Utils.DataType;\nimport { OwnerIdentifier } from \"../types/role\";\nimport { EthereumAddress } from \"../util/EthereumAddress\";\n\n/**\n * RoleManagement provides convenient wrappers around the on-chain SQL actions\n * that implement the RBAC system (grant_roles, revoke_roles, are_members_of).\n */\nexport class RoleManagement extends Action {\n constructor(kwilClient: WebKwil | NodeKwil, kwilSigner: KwilSigner) {\n super(kwilClient, kwilSigner);\n }\n\n private static normalizeOwner(owner: OwnerIdentifier): string {\n return owner === \"system\"\n ? \"system\"\n : owner.getAddress().toLowerCase();\n }\n\n private static normalizeWallets(wallets: EthereumAddress[]): string[] {\n return wallets.map((w) => w.getAddress().toLowerCase());\n }\n\n /**\n * Grants a role to the provided wallets.\n * This calls the `grant_roles` action.\n */\n public async grantRole(\n input: GrantRoleInput,\n synchronous = false,\n ): Promise<GenericResponse<TxReceipt>> {\n return this.executeWithActionBody(\n {\n namespace: \"main\",\n name: \"grant_roles\",\n inputs: [\n {\n $owner: RoleManagement.normalizeOwner(input.owner),\n $role_name: input.roleName.toLowerCase(),\n $wallets: RoleManagement.normalizeWallets(input.wallets),\n },\n ],\n types: {\n $owner: DataType.Text,\n $role_name: DataType.Text,\n $wallets: DataType.TextArray,\n },\n },\n synchronous,\n );\n }\n\n /**\n * Revokes a role from the provided wallets.\n * This calls the `revoke_roles` action.\n */\n public async revokeRole(\n input: RevokeRoleInput,\n synchronous = false,\n ): Promise<GenericResponse<TxReceipt>> {\n return this.executeWithActionBody(\n {\n namespace: \"main\",\n name: \"revoke_roles\",\n inputs: [\n {\n $owner: RoleManagement.normalizeOwner(input.owner),\n $role_name: input.roleName.toLowerCase(),\n $wallets: RoleManagement.normalizeWallets(input.wallets),\n },\n ],\n types: {\n $owner: DataType.Text,\n $role_name: DataType.Text,\n $wallets: DataType.TextArray,\n },\n },\n synchronous,\n );\n }\n\n /**\n * Checks if the provided wallets are members of a role.\n * This calls the `are_members_of` VIEW action.\n *\n * @returns an array matching the provided wallets order with membership flags.\n */\n public async areMembersOf(\n input: AreMembersOfInput,\n ): Promise<WalletMembership[]> {\n const result = await this.call<{ wallet: string; is_member: boolean }[]>(\n \"are_members_of\",\n {\n $owner: RoleManagement.normalizeOwner(input.owner),\n $role_name: input.roleName.toLowerCase(),\n $wallets: RoleManagement.normalizeWallets(input.wallets),\n },\n );\n\n // Either.throw() will return the right value or throw with the left value (status code)\n return result.throw().map((row) => ({\n wallet: row.wallet,\n isMember: row.is_member,\n }));\n }\n\n /**\n * Lists the members of a role with optional pagination.\n * This calls the `list_role_members` VIEW action.\n */\n public async listRoleMembers(\n input: import(\"../types/role\").ListRoleMembersInput,\n ): Promise<import(\"../types/role\").RoleMember[]> {\n const result = await this.call<{\n wallet: string;\n granted_at: number;\n granted_by: string;\n }[]>(\"list_role_members\", {\n $owner: RoleManagement.normalizeOwner(input.owner),\n $role_name: input.roleName.toLowerCase(),\n ...(input.limit !== undefined ? { $limit: input.limit } : {}),\n ...(input.offset !== undefined ? { $offset: input.offset } : {}),\n });\n\n return result.throw().map((row) => ({\n wallet: row.wallet,\n grantedAt: Number(row.granted_at),\n grantedBy: row.granted_by,\n }));\n }\n\n /**\n * Helper factory mirroring the pattern used by the other action wrappers.\n */\n public static fromClient(\n kwilClient: WebKwil | NodeKwil,\n kwilSigner: KwilSigner,\n ): RoleManagement {\n return new RoleManagement(kwilClient, kwilSigner);\n }\n} "],
|
|
5
|
+
"mappings": ";AAAA,SAA+B,aAAsB;AAGrD,SAAS,cAAc;AAEvB,IAAO,WAAW,MAAM;AAQjB,IAAM,iBAAN,MAAM,wBAAuB,OAAO;AAAA,EACzC,YAAY,YAAgC,YAAwB;AAClE,UAAM,YAAY,UAAU;AAAA,EAC9B;AAAA,EAEA,OAAe,eAAe,OAAgC;AAC5D,WAAO,UAAU,WACb,WACA,MAAM,WAAW,EAAE,YAAY;AAAA,EACrC;AAAA,EAEA,OAAe,iBAAiB,SAAsC;AACpE,WAAO,QAAQ,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,YAAY,CAAC;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAa,UACX,OACA,cAAc,OACuB;AACrC,WAAO,KAAK;AAAA,MACV;AAAA,QACE,WAAW;AAAA,QACX,MAAM;AAAA,QACN,QAAQ;AAAA,UACN;AAAA,YACE,QAAQ,gBAAe,eAAe,MAAM,KAAK;AAAA,YACjD,YAAY,MAAM,SAAS,YAAY;AAAA,YACvC,UAAU,gBAAe,iBAAiB,MAAM,OAAO;AAAA,UACzD;AAAA,QACF;AAAA,QACA,OAAO;AAAA,UACL,QAAQ,SAAS;AAAA,UACjB,YAAY,SAAS;AAAA,UACrB,UAAU,SAAS;AAAA,QACrB;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA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,QAAQ,IAAI;AAAA,MACZ,WAAW,OAAO,IAAI,UAAU;AAAA,MAChC,WAAW,IAAI;AAAA,IACjB,EAAE;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,WACZ,YACA,YACgB;AAChB,WAAO,IAAI,gBAAe,YAAY,UAAU;AAAA,EAClD;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -6,11 +6,13 @@ import { StreamType } from "./contracts-api/contractValues.mjs";
|
|
|
6
6
|
import { Action } from "./contracts-api/action.mjs";
|
|
7
7
|
import { PrimitiveAction } from "./contracts-api/primitiveAction.mjs";
|
|
8
8
|
import { ComposedAction } from "./contracts-api/composedAction.mjs";
|
|
9
|
+
import { RoleManagement } from "./contracts-api/roleManagement.mjs";
|
|
9
10
|
export {
|
|
10
11
|
Action,
|
|
11
12
|
ComposedAction,
|
|
12
13
|
EthereumAddress,
|
|
13
14
|
PrimitiveAction,
|
|
15
|
+
RoleManagement,
|
|
14
16
|
StreamId,
|
|
15
17
|
StreamType,
|
|
16
18
|
visibility
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.common.ts"],
|
|
4
|
-
"sourcesContent": ["// Core client types\nexport type { TNClientOptions } from \"./client/client\";\nexport type { SignerInfo as EthProvider } from \"./client/client\";\n\n// Stream types and interfaces\nexport type { StreamLocator } from \"./types/stream\";\nexport type { StreamRecord } from \"./contracts-api/action\";\nexport type { GetRecordInput, GetFirstRecordInput } from \"./contracts-api/action\";\nexport type { InsertRecordInput } from \"./contracts-api/primitiveAction\";\nexport type { TaxonomySet, TaxonomyItem } from \"./contracts-api/composedAction\";\n\n// Utility types and classes\nexport { StreamId } from \"./util/StreamId\";\nexport { EthereumAddress } from \"./util/EthereumAddress\";\nexport { visibility } from \"./util/visibility\";\nexport type { VisibilityEnum } from \"./util/visibility\";\n\n// Stream type constants\nexport { StreamType } from \"./contracts-api/contractValues\";\n\n// Base classes\nexport { Action } from \"./contracts-api/action\";\nexport { PrimitiveAction } from \"./contracts-api/primitiveAction\";\nexport { ComposedAction } from \"./contracts-api/composedAction\";\n\n"],
|
|
5
|
-
"mappings": ";AAYA,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,kBAAkB;AAI3B,SAAS,kBAAkB;AAG3B,SAAS,cAAc;AACvB,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;",
|
|
4
|
+
"sourcesContent": ["// Core client types\nexport type { TNClientOptions } from \"./client/client\";\nexport type { SignerInfo as EthProvider } from \"./client/client\";\n\n// Stream types and interfaces\nexport type { StreamLocator } from \"./types/stream\";\nexport type { StreamRecord } from \"./contracts-api/action\";\nexport type { GetRecordInput, GetFirstRecordInput } from \"./contracts-api/action\";\nexport type { InsertRecordInput } from \"./contracts-api/primitiveAction\";\nexport type { TaxonomySet, TaxonomyItem } from \"./contracts-api/composedAction\";\n\n// Utility types and classes\nexport { StreamId } from \"./util/StreamId\";\nexport { EthereumAddress } from \"./util/EthereumAddress\";\nexport { visibility } from \"./util/visibility\";\nexport type { VisibilityEnum } from \"./util/visibility\";\n\n// Stream type constants\nexport { StreamType } from \"./contracts-api/contractValues\";\n\n// Base classes\nexport { Action } from \"./contracts-api/action\";\nexport { PrimitiveAction } from \"./contracts-api/primitiveAction\";\nexport { ComposedAction } from \"./contracts-api/composedAction\";\n\n// Role management exports\nexport { RoleManagement } from \"./contracts-api/roleManagement\";\nexport type { GrantRoleInput, RevokeRoleInput, AreMembersOfInput, WalletMembership } from \"./types/role\";\n\n"],
|
|
5
|
+
"mappings": ";AAYA,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,kBAAkB;AAI3B,SAAS,kBAAkB;AAG3B,SAAS,cAAc;AACvB,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;AAG/B,SAAS,sBAAsB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=role.mjs.map
|