@dsnp/graph-sdk
Advanced tools
Comparing version 0.0.0-daee06 to 0.0.0-e04907
@@ -1,22 +0,23 @@ | ||
import { ImportBundle, Update, DsnpGraphEdge, Action, DsnpPublicKey, DsnpKeys, Config } from "./models"; | ||
import { ImportBundle, Update, DsnpGraphEdge, Action, DsnpPublicKey, DsnpKeys, Config, ConnectionType, PrivacyType, GraphKeyPair } from "./models"; | ||
import { EnvironmentInterface } from "./models/environment"; | ||
export declare class Graph { | ||
private handle; | ||
constructor(environment: EnvironmentInterface, capacity?: number); | ||
constructor(environment: EnvironmentInterface); | ||
getGraphHandle(): number; | ||
getGraphConfig(environment: EnvironmentInterface): Promise<Config>; | ||
getGraphCapacity(): Promise<number>; | ||
getSchemaIdFromConfig(environment: EnvironmentInterface, connectionType: ConnectionType, privacyType: PrivacyType): Promise<number>; | ||
getGraphStatesCount(): Promise<number>; | ||
containsUserGraph(dsnpUserId: number): Promise<boolean>; | ||
containsUserGraph(dsnpUserId: string): Promise<boolean>; | ||
getGraphUsersCount(): Promise<number>; | ||
removeUserGraph(dsnpUserId: number): Promise<boolean>; | ||
removeUserGraph(dsnpUserId: string): Promise<boolean>; | ||
importUserData(payload: ImportBundle[]): Promise<boolean>; | ||
exportUpdates(): Promise<Update[]>; | ||
getConnectionsForUserGraph(dsnpUserId: number, schemaId: number, includePending: boolean): Promise<DsnpGraphEdge[]>; | ||
getConnectionsForUserGraph(dsnpUserId: string, schemaId: number, includePending: boolean): Promise<DsnpGraphEdge[]>; | ||
applyActions(actions: Action[]): Promise<boolean>; | ||
forceCalculateGraphs(dsnpUserId: number): Promise<Update[]>; | ||
getConnectionsWithoutKeys(): Promise<number[]>; | ||
getOneSidedPrivateFriendshipConnections(dsnpUserId: number): Promise<DsnpGraphEdge[]>; | ||
getPublicKeys(dsnpUserId: number): Promise<DsnpPublicKey[]>; | ||
deserializeDsnpKeys(keys: DsnpKeys): Promise<DsnpPublicKey[]>; | ||
forceCalculateGraphs(dsnpUserId: string): Promise<Update[]>; | ||
getConnectionsWithoutKeys(): Promise<string[]>; | ||
getOneSidedPrivateFriendshipConnections(dsnpUserId: string): Promise<DsnpGraphEdge[]>; | ||
getPublicKeys(dsnpUserId: string): Promise<DsnpPublicKey[]>; | ||
static deserializeDsnpKeys(keys: DsnpKeys): Promise<DsnpPublicKey[]>; | ||
static generateKeyPair(keyType: number): Promise<GraphKeyPair>; | ||
freeGraphState(): Promise<boolean>; | ||
@@ -23,0 +24,0 @@ printHelloGraph(): void; |
@@ -6,9 +6,4 @@ "use strict"; | ||
class Graph { | ||
constructor(environment, capacity) { | ||
if (capacity) { | ||
this.handle = index_1.graphsdkModule.initializeGraphStateWithCapacity(environment, capacity); | ||
} | ||
else { | ||
this.handle = index_1.graphsdkModule.initializeGraphState(environment); | ||
} | ||
constructor(environment) { | ||
this.handle = index_1.graphsdkModule.initializeGraphState(environment); | ||
} | ||
@@ -21,4 +16,4 @@ getGraphHandle() { | ||
} | ||
getGraphCapacity() { | ||
return index_1.graphsdkModule.getGraphCapacity(this.handle); | ||
getSchemaIdFromConfig(environment, connectionType, privacyType) { | ||
return index_1.graphsdkModule.getSchemaIdFromConfig(environment, connectionType, privacyType); | ||
} | ||
@@ -61,5 +56,8 @@ getGraphStatesCount() { | ||
} | ||
deserializeDsnpKeys(keys) { | ||
static deserializeDsnpKeys(keys) { | ||
return index_1.graphsdkModule.deserializeDsnpKeys(keys); | ||
} | ||
static generateKeyPair(keyType) { | ||
return index_1.graphsdkModule.generateKeyPair(keyType); | ||
} | ||
freeGraphState() { | ||
@@ -66,0 +64,0 @@ return index_1.graphsdkModule.freeGraphState(this.handle); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const graph_1 = require("./graph"); | ||
const models_1 = require("./models"); | ||
const config_1 = require("./models/config"); | ||
@@ -8,3 +9,2 @@ const environment_1 = require("./models/environment"); | ||
const config = {}; | ||
config.sdkMaxUsersGraphSize = 100; | ||
config.sdkMaxStaleFriendshipDays = 100; | ||
@@ -37,3 +37,2 @@ config.maxPageId = 100; | ||
expect(config).toBeDefined(); | ||
expect(config.sdkMaxUsersGraphSize).toEqual(100); | ||
await graph.freeGraphState(); | ||
@@ -46,3 +45,4 @@ }); | ||
expect(config).toBeDefined(); | ||
expect(config.sdkMaxUsersGraphSize).toEqual(1000); | ||
const schema_id = await graph.getSchemaIdFromConfig(environment, config_1.ConnectionType.Follow, config_1.PrivacyType.Public); | ||
expect(schema_id).toEqual(1); | ||
await graph.freeGraphState(); | ||
@@ -55,15 +55,4 @@ }); | ||
expect(config).toBeDefined(); | ||
expect(config.sdkMaxUsersGraphSize).toEqual(1000); | ||
await graph.freeGraphState(); | ||
}); | ||
test('initialize graph with low capacity of 100 should return the same capacity', async () => { | ||
const config = getTestConfig(); | ||
const environment = { environmentType: environment_1.EnvironmentType.Dev, config }; | ||
const graph = new graph_1.Graph(environment, 100); | ||
const handle = graph.getGraphHandle(); | ||
expect(handle).toBeDefined(); | ||
const capacity = await graph.getGraphCapacity(); | ||
expect(capacity).toEqual(100); | ||
await graph.freeGraphState(); | ||
}); | ||
test('getGraphStatesCount should be zero after previous graph is freed', async () => { | ||
@@ -108,3 +97,3 @@ const config = getTestConfig(); | ||
expect(handle).toBeDefined(); | ||
const contains = await graph.containsUserGraph(1); | ||
const contains = await graph.containsUserGraph("1"); | ||
expect(contains).toEqual(false); | ||
@@ -119,3 +108,3 @@ await graph.freeGraphState(); | ||
expect(handle).toBeDefined(); | ||
const removed = await graph.removeUserGraph(1); | ||
const removed = await graph.removeUserGraph("1"); | ||
expect(removed).toEqual(true); | ||
@@ -140,3 +129,3 @@ await graph.freeGraphState(); | ||
const dsnpKeys1 = { | ||
dsnpUserId: dsnpUserId1, | ||
dsnpUserId: dsnpUserId1.toString(), | ||
keysHash: 100, | ||
@@ -146,3 +135,3 @@ keys: [], | ||
const dsnpKeys2 = { | ||
dsnpUserId: dsnpUserId2, | ||
dsnpUserId: dsnpUserId2.toString(), | ||
keysHash: 100, | ||
@@ -152,3 +141,3 @@ keys: [], | ||
const importBundle1 = { | ||
dsnpUserId: dsnpUserId1, | ||
dsnpUserId: dsnpUserId1.toString(), | ||
schemaId: 1, | ||
@@ -160,3 +149,3 @@ keyPairs: keyPairs1, | ||
const importBundle2 = { | ||
dsnpUserId: dsnpUserId2, | ||
dsnpUserId: dsnpUserId2.toString(), | ||
schemaId: 1, | ||
@@ -193,9 +182,9 @@ keyPairs: keyPairs2, | ||
type: "Connect", | ||
ownerDsnpUserId: 1, | ||
ownerDsnpUserId: "1", | ||
connection: { | ||
dsnpUserId: 2, | ||
dsnpUserId: "2", | ||
schemaId: 1, | ||
}, | ||
dsnpKeys: { | ||
dsnpUserId: 2, | ||
dsnpUserId: "2", | ||
keysHash: 100, | ||
@@ -223,9 +212,9 @@ keys: [], | ||
type: "Connect", | ||
ownerDsnpUserId: 1, | ||
ownerDsnpUserId: "1", | ||
connection: { | ||
dsnpUserId: 2, | ||
dsnpUserId: "2", | ||
schemaId: 1, | ||
}, | ||
dsnpKeys: { | ||
dsnpUserId: 2, | ||
dsnpUserId: "2", | ||
keysHash: 100, | ||
@@ -238,6 +227,6 @@ keys: [], | ||
expect(applied).toEqual(true); | ||
const connections = await graph.getConnectionsForUserGraph(1, 1, true); | ||
const connections = await graph.getConnectionsForUserGraph("1", 1, true); | ||
expect(connections).toBeDefined(); | ||
expect(connections.length).toEqual(1); | ||
const forceCalculateGraphs = await graph.forceCalculateGraphs(1); | ||
const forceCalculateGraphs = await graph.forceCalculateGraphs("1"); | ||
expect(forceCalculateGraphs).toBeDefined(); | ||
@@ -256,3 +245,3 @@ expect(forceCalculateGraphs.length).toEqual(0); | ||
expect(async () => { | ||
await graph.getOneSidedPrivateFriendshipConnections(1); | ||
await graph.getOneSidedPrivateFriendshipConnections("1"); | ||
}).rejects.toThrow('User graph for 1 is not imported'); | ||
@@ -266,5 +255,5 @@ await graph.freeGraphState(); | ||
expect(handle).toBeDefined(); | ||
const connections = await graph.getPublicKeys(1); | ||
expect(connections).toBeDefined(); | ||
expect(connections.length).toEqual(0); | ||
const keys = await graph.getPublicKeys("1"); | ||
expect(keys).toBeDefined(); | ||
expect(keys.length).toEqual(0); | ||
await graph.freeGraphState(); | ||
@@ -278,11 +267,88 @@ }); | ||
const keys = { | ||
dsnpUserId: 2, | ||
dsnpUserId: "2", | ||
keysHash: 100, | ||
keys: [], | ||
}; | ||
const connections = await graph.deserializeDsnpKeys(keys); | ||
expect(connections).toBeDefined(); | ||
expect(connections.length).toEqual(0); | ||
const des_keys = await graph_1.Graph.deserializeDsnpKeys(keys); | ||
expect(des_keys).toBeDefined(); | ||
expect(des_keys.length).toEqual(0); | ||
await graph.freeGraphState(); | ||
}); | ||
test('Create and export a new graph', async () => { | ||
let mainnet_environment = { environmentType: environment_1.EnvironmentType.Mainnet }; | ||
let graph = new graph_1.Graph(mainnet_environment); | ||
let public_follow_graph_schema_id = await graph.getSchemaIdFromConfig(mainnet_environment, config_1.ConnectionType.Follow, config_1.PrivacyType.Public); | ||
let connect_action = { | ||
type: "Connect", | ||
ownerDsnpUserId: "1", | ||
connection: { | ||
dsnpUserId: "2", | ||
schemaId: public_follow_graph_schema_id, | ||
}, | ||
dsnpKeys: { | ||
dsnpUserId: "2", | ||
keysHash: 100, | ||
keys: [], | ||
}, | ||
}; | ||
let actions = []; | ||
actions.push(connect_action); | ||
let applied = await graph.applyActions(actions); | ||
expect(applied).toEqual(true); | ||
let connections_including_pending = await graph.getConnectionsForUserGraph("1", public_follow_graph_schema_id, true); | ||
expect(connections_including_pending).toBeDefined(); | ||
expect(connections_including_pending.length).toEqual(1); | ||
let exported = await graph.exportUpdates(); | ||
expect(exported).toBeDefined(); | ||
expect(exported.length).toEqual(1); | ||
}); | ||
test('Add a new graph key', async () => { | ||
const environment = { environmentType: environment_1.EnvironmentType.Mainnet }; | ||
const graph = new graph_1.Graph(environment); | ||
const dsnpOwnerId = 1; | ||
const x25519_public_key = [ | ||
15, 234, 44, 175, 171, 220, 131, 117, 43, 227, 111, 165, 52, 150, 64, 218, 44, 130, 138, | ||
221, 10, 41, 13, 241, 60, 210, 216, 23, 62, 178, 73, 111, | ||
]; | ||
const addGraphKeyAction = { | ||
type: "AddGraphKey", | ||
ownerDsnpUserId: dsnpOwnerId.toString(), | ||
newPublicKey: new Uint8Array(x25519_public_key), | ||
}; | ||
const actions = []; | ||
actions.push(addGraphKeyAction); | ||
const applied = await graph.applyActions(actions); | ||
expect(applied).toEqual(true); | ||
const exported = await graph.exportUpdates(); | ||
expect(exported).toBeDefined(); | ||
expect(exported.length).toEqual(1); | ||
}); | ||
test('Read and deserialize published graph keys', async () => { | ||
let dsnp_key_owner = 1000; | ||
// published keys blobs fetched from blockchain | ||
let published_keys_blob = [ | ||
64, 15, 234, 44, 175, 171, 220, 131, 117, 43, 227, 111, 165, 52, 150, 64, 218, 44, 130, | ||
138, 221, 10, 41, 13, 241, 60, 210, 216, 23, 62, 178, 73, 111, | ||
]; | ||
let dsnp_keys = { | ||
dsnpUserId: dsnp_key_owner.toString(), | ||
keysHash: 100, | ||
keys: [ | ||
{ | ||
index: 0, | ||
content: new Uint8Array(published_keys_blob), | ||
} | ||
], | ||
}; | ||
const environment = { environmentType: environment_1.EnvironmentType.Mainnet }; | ||
const deserialized_keys = await graph_1.Graph.deserializeDsnpKeys(dsnp_keys); | ||
expect(deserialized_keys).toBeDefined(); | ||
}); | ||
test('generateKeyPair should return a key pair', async () => { | ||
const keyPair = await graph_1.Graph.generateKeyPair(models_1.GraphKeyType.X25519); | ||
expect(keyPair).toBeDefined(); | ||
expect(keyPair.publicKey).toBeDefined(); | ||
expect(keyPair.secretKey).toBeDefined(); | ||
expect(keyPair.keyType).toEqual(models_1.GraphKeyType.X25519); | ||
}); | ||
//# sourceMappingURL=graph.test.js.map |
@@ -1,24 +0,26 @@ | ||
import { Action, Config, DsnpGraphEdge, DsnpKeys, DsnpPublicKey, EnvironmentInterface, ImportBundle, Update } from "./models"; | ||
import { Action, Config, ConnectionType, DsnpGraphEdge, DsnpKeys, DsnpPublicKey, EnvironmentInterface, GraphKeyPair, GraphKeyType, ImportBundle, PrivacyType, Update } from "./models"; | ||
export interface Native { | ||
printHelloGraph(): void; | ||
initializeGraphState(environment: EnvironmentInterface): number; | ||
initializeGraphStateWithCapacity(environment: EnvironmentInterface, capacity: number): number; | ||
getGraphConfig(environment: EnvironmentInterface): Promise<Config>; | ||
getGraphCapacity(handle: number): Promise<number>; | ||
getSchemaIdFromConfig(environment: EnvironmentInterface, connectionType: ConnectionType, privacyType: PrivacyType): Promise<number>; | ||
getGraphStatesCount(): Promise<number>; | ||
getGraphUsersCount(handle: number): Promise<number>; | ||
containsUserGraph(handle: number, dsnpUserId: number): Promise<boolean>; | ||
removeUserGraph(handle: number, dsnpUserId: number): Promise<boolean>; | ||
containsUserGraph(handle: number, dsnpUserId: string): Promise<boolean>; | ||
removeUserGraph(handle: number, dsnpUserId: string): Promise<boolean>; | ||
importUserData(handle: number, payload: ImportBundle[]): Promise<boolean>; | ||
applyActions(handle: number, actions: Action[]): Promise<boolean>; | ||
exportUpdates(handle: number): Promise<Update[]>; | ||
getConnectionsForUserGraph(handle: number, dsnpUserId: number, schemaId: number, includePending: boolean): Promise<DsnpGraphEdge[]>; | ||
forceCalculateGraphs(handle: number, dsnpUserId: number): Promise<Update[]>; | ||
getConnectionsWithoutKeys(handle: number): Promise<number[]>; | ||
getOneSidedPrivateFriendshipConnections(handle: number, dsnpUserId: number): Promise<DsnpGraphEdge[]>; | ||
getPublicKeys(handle: number, dsnpUserId: number): Promise<DsnpPublicKey[]>; | ||
getConnectionsForUserGraph(handle: number, dsnpUserId: string, schemaId: number, includePending: boolean): Promise<DsnpGraphEdge[]>; | ||
forceCalculateGraphs(handle: number, dsnpUserId: string): Promise<Update[]>; | ||
getConnectionsWithoutKeys(handle: number): Promise<string[]>; | ||
getOneSidedPrivateFriendshipConnections(handle: number, dsnpUserId: string): Promise<DsnpGraphEdge[]>; | ||
getPublicKeys(handle: number, dsnpUserId: string): Promise<DsnpPublicKey[]>; | ||
deserializeDsnpKeys(keys: DsnpKeys): Promise<DsnpPublicKey[]>; | ||
generateKeyPair(keyType: GraphKeyType): Promise<GraphKeyPair>; | ||
freeGraphState(handle: number): Promise<boolean>; | ||
} | ||
export declare const graphsdkModule: Native; | ||
export * from "./models"; | ||
export * from "./graph"; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -11,6 +25,6 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
try { | ||
return require(path_1.default.join(__dirname, "/graphsdk.node")); | ||
return require(path_1.default.join(__dirname, "/dsnp_graph_sdk_node.node")); | ||
} | ||
catch (error) { | ||
throw new Error("Unable to load the native module graphsdk.node"); | ||
throw new Error("Unable to load the native module dsnp_graph_sdk_node.node"); | ||
} | ||
@@ -20,2 +34,5 @@ } | ||
exports.graphsdkModule = loadNativeModule(); | ||
// Export the models | ||
__exportStar(require("./models"), exports); | ||
__exportStar(require("./graph"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -8,3 +8,2 @@ "use strict"; | ||
const config = {}; | ||
config.sdkMaxUsersGraphSize = 100; | ||
config.sdkMaxStaleFriendshipDays = 100; | ||
@@ -11,0 +10,0 @@ config.maxPageId = 100; |
@@ -5,3 +5,3 @@ import { Connection } from "./connection"; | ||
type: "Connect"; | ||
ownerDsnpUserId: number; | ||
ownerDsnpUserId: string; | ||
connection: Connection; | ||
@@ -12,3 +12,3 @@ dsnpKeys?: DsnpKeys; | ||
type: "Disconnect"; | ||
ownerDsnpUserId: number; | ||
ownerDsnpUserId: string; | ||
connection: Connection; | ||
@@ -18,3 +18,3 @@ } | ||
type: "AddGraphKey"; | ||
ownerDsnpUserId: number; | ||
ownerDsnpUserId: string; | ||
newPublicKey: Uint8Array; | ||
@@ -21,0 +21,0 @@ } |
@@ -18,3 +18,2 @@ declare enum DsnpVersion { | ||
interface Config { | ||
sdkMaxUsersGraphSize: number; | ||
sdkMaxStaleFriendshipDays: number; | ||
@@ -21,0 +20,0 @@ maxGraphPageSizeBytes: number; |
export interface Connection { | ||
dsnpUserId: number; | ||
dsnpUserId: string; | ||
schemaId: number; | ||
} | ||
//# sourceMappingURL=connection.d.ts.map |
export interface DsnpGraphEdge { | ||
userId: number; | ||
userId: string; | ||
since: number; | ||
} | ||
//# sourceMappingURL=graph_edge.d.ts.map |
@@ -6,3 +6,3 @@ export interface KeyData { | ||
export interface DsnpKeys { | ||
dsnpUserId: number; | ||
dsnpUserId: string; | ||
keysHash: number; | ||
@@ -25,3 +25,3 @@ keys: KeyData[]; | ||
export interface ImportBundle { | ||
dsnpUserId: number; | ||
dsnpUserId: string; | ||
schemaId: number; | ||
@@ -28,0 +28,0 @@ keyPairs: GraphKeyPair[]; |
export interface DsnpPublicKey { | ||
key: Uint8Array; | ||
keyId?: Uint8Array; | ||
keyId?: string; | ||
} | ||
//# sourceMappingURL=public_keys.d.ts.map |
export interface PersistPageUpdate { | ||
type: "PersistPage"; | ||
ownerDsnpUserId: number; | ||
ownerDsnpUserId: string; | ||
schemaId: number; | ||
@@ -11,3 +11,3 @@ pageId: number; | ||
type: "DeletePage"; | ||
ownerDsnpUserId: number; | ||
ownerDsnpUserId: string; | ||
schemaId: number; | ||
@@ -19,3 +19,3 @@ pageId: number; | ||
type: "AddKey"; | ||
ownerDsnpUserId: number; | ||
ownerDsnpUserId: string; | ||
prevHash: number; | ||
@@ -22,0 +22,0 @@ payload: Uint8Array; |
{ | ||
"name": "@dsnp/graph-sdk", | ||
"version": "0.0.0-daee06", | ||
"version": "0.0.0-e04907", | ||
"author": "Amplica Labs", | ||
@@ -12,6 +12,6 @@ "license": "ISC", | ||
"build": "tsc", | ||
"cp:graphsdk.node": "cp graphsdk.node dist/js/ && cp graphsdk.node js/", | ||
"native:build": "tsc && cargo-cp-artifact -a cdylib graphsdk graphsdk.node -- cargo build --message-format=json-render-diagnostics", | ||
"cp:dsnp_graph_sdk_node.node": "cp dsnp_graph_sdk_node.node dist/js/ && cp dsnp_graph_sdk_node.node js/", | ||
"native:build": "tsc && cargo-cp-artifact -a cdylib dsnp_graph_sdk_node dsnp_graph_sdk_node.node -- cargo build --message-format=json-render-diagnostics", | ||
"native:build-debug": "npm run native:build --", | ||
"native:build-release": "npm run native:build -- --release && npm run cp:graphsdk.node", | ||
"native:build-release": "npm run native:build -- --release && npm run cp:dsnp_graph_sdk_node.node", | ||
"test:cargo": "cargo test", | ||
@@ -52,3 +52,3 @@ "lint": "eslint js/ --ext .ts", | ||
}, | ||
"homepage": "https://github.com/LibertyDSNP/graph-sdk#readme" | ||
"homepage": "https://github.com/LibertyDSNP/graph-sdk/bridge/node/README.md" | ||
} |
206
README.md
@@ -10,3 +10,3 @@ # DSNP Graph SDK | ||
```bash | ||
npm install @dsnp/graph-sdk | ||
npm install @dsnp/graph-sdk@latest | ||
``` | ||
@@ -105,3 +105,3 @@ | ||
#### Constructor: new Graph(environment: EnvironmentInterface, capacity?: number) | ||
#### Constructor: new Graph(environment: EnvironmentInterface) | ||
@@ -111,3 +111,2 @@ Creates a new instance of the Graph class. | ||
- `environment`: An object that represents the environment details. | ||
- `capacity` (optional): The initial capacity of the graph. | ||
@@ -117,3 +116,2 @@ #### Methods | ||
- `getGraphHandle(): number`: Returns the handle to the native graph state. | ||
- `getGraphCapacity(): Promise<number>`: Retrieves the capacity of the graph. | ||
- `getGraphSize(): Promise<number>`: Retrieves the current size of the graph. | ||
@@ -133,2 +131,3 @@ - `containsUserGraph(dsnpUserId: number): Promise<boolean>`: Checks if the graph contains the user graph for the specified DSNP user ID. | ||
- `getGraphConfig(environment: EnvironmentInterface): Promise<Config>`: Retrieves the graph configuration. | ||
- `getSchemaIdFromConfig(environment: EnvironmentInterface, connectionType: ConnectionType, privacyType: PrivacyType): Promise<number>`: Retrieves the schema ID from the graph configuration. | ||
- `freeGraphState(): void`: Frees the graph state. | ||
@@ -148,2 +147,199 @@ | ||
- `DsnpKeys`: Encapsulates a DSNP user and their associated graph public keys. | ||
- `GraphKeyPair`: Represents a key pair for a DSNP user. | ||
- `PageData`: Represents the page data to be retrieved from the chain. | ||
- `Connection`: Represents a connection between two DSNP users.- ` | ||
## Examples | ||
### Create and export a new graph | ||
```typescript | ||
import { Graph, EnvironmentInterface, EnvironmentType } from "@dsnp/graph-sdk"; | ||
const environment: EnvironmentInterface = { environmentType: EnvironmentType.Mainnet }; | ||
const graph = new Graph(environment); | ||
const public_follow_schema_id = await graph.getSchemaIdFromConfig(environment, ConnectionType.Follow, PrivacyType.Public); | ||
const connect_action = { | ||
type: "Connect", | ||
ownerDsnpUserId: 1, | ||
connection: { | ||
dsnpUserId: 2, | ||
schemaId: public_follow_schema_id, | ||
}, | ||
dsnpKeys: { | ||
dsnpUserId: 2, | ||
keysHash: 100, | ||
keys: [], | ||
}, | ||
}; | ||
await graph.applyActions([connect_action]); | ||
const updates = await graph.exportUpdates(); | ||
graph.freeGraphState(); | ||
``` | ||
### Add a new graph key | ||
```typescript | ||
import { Graph, EnvironmentInterface, EnvironmentType } from "@dsnp/graph-sdk"; | ||
const environment: EnvironmentInterface = { environmentType: EnvironmentType.Mainnet }; | ||
const graph = new Graph(environment); | ||
const ownerDsnpUserId = 1; | ||
const x25519_public_key = [ 15, 234, 44, 175, 171, 220, 131, 117, 43, 227, 111, 165, 52, 150, 64, 218, 44, 130, 138, 221, 10, 41, 13, 241, 60, 210, 216, 23, 62, 178, 73, 111,]; | ||
const addGraphKeyAction = { | ||
type: "AddGraphKey", | ||
ownerDsnpUserId: dsnpOwnerId, | ||
newPublicKey: new Uint8Array(x25519_public_key), | ||
} as AddGraphKeyAction; | ||
await graph.applyActions([addGraphKeyAction]); | ||
const updates = await graph.exportUpdates(); | ||
graph.freeGraphState(); | ||
``` | ||
### Read and deserialize published graph keys | ||
```typescript | ||
import { Graph, EnvironmentInterface, EnvironmentType, DsnpPublicKey } from "@dsnp/graph-sdk"; | ||
const environment: EnvironmentInterface = { environmentType: EnvironmentType.Mainnet }; | ||
const graph = new Graph(environment); | ||
const dsnpUserId = 1000; | ||
// published keys blobs fetched from blockchain | ||
const published_keys_blob = [ 64, 15, 234, 44, 175, 171, 220, 131, 117, 43, 227, 111, 165, 52, 150, 64, 218, 44, 130, 138, 221, 10, 41, 13, 241, 60, 210, 216, 23, 62, 178, 73, 111,]; | ||
let dsnp_keys ={ | ||
dsnpUserId: dsnp_key_owner, | ||
keysHash: 100, | ||
keys: [ | ||
{ | ||
index: 0, | ||
content: new Uint8Array(published_keys_blob), | ||
} | ||
] as KeyData[], | ||
} as DsnpKeys; | ||
const deserialized_keys = await Graph.deserializeDsnpKeys(dsnp_keys); | ||
graph.freeGraphState(); | ||
``` | ||
### Update a Private Follow graph | ||
```typescript | ||
const environment: EnvironmentInterface = { environmentType: EnvironmentType Mainnet }; | ||
const graph = new Graph(environment); | ||
const dsnpOwnerId = 1; | ||
const private_follow_graph_schema_id = await graph.getSchemaIdFromConfig(environment, ConnectionType.Follow, PrivacyType.Private); | ||
const import_bundle = { | ||
dsnpUserId: dsnpOwnerId, | ||
schemaId: private_follow_graph_schema_id, | ||
keyPairs: [/* get key-pairs associated with the my_dsnp_user_id user from wallet */], | ||
dsnpKeys: { | ||
dsnpUserId: dsnpOwnerId, | ||
keysHash: 100, // get from blockchain | ||
keys: [/* published keys got from blockchain */], | ||
} as DsnpKeys, | ||
pages: [/* published graph pages got from blockchain */], | ||
} as ImportBundle; | ||
const imported = await graph.importUserData([import_bundle]); | ||
const connect_action: ConnectAction = { | ||
type: "Connect", | ||
ownerDsnpUserId: dsnpOwnerId, | ||
connection: { | ||
dsnpUserId: 2, | ||
schemaId: private_follow_graph_schema_id, | ||
} as Connection, | ||
} as ConnectAction; | ||
const actions = [] as Action[]; | ||
actions.push(connect_action); | ||
const applied = await graph.applyActions(actions); | ||
const exported_updates = await graph.exportUpdates(); | ||
graph.freeGraphState(); | ||
``` | ||
### Update a Private Friendship graph | ||
```typescript | ||
const environment: EnvironmentInterface = { environmentType: EnvironmentType Mainnet }; | ||
const graph = new Graph(environment); | ||
const dsnpOwnerId = 1; | ||
const private_friendship_graph_schema_id = await graph.getSchemaIdFromConfig(environment, ConnectionType.Friendship, PrivacyType.Private); | ||
const import_bundle = { | ||
dsnpUserId: dsnpOwnerId, | ||
schemaId: private_friendship_graph_schema_id, | ||
keyPairs: [/* get key-pairs associated with the my_dsnp_user_id user from wallet */], | ||
dsnpKeys: { | ||
dsnpUserId: dsnpOwnerId, | ||
keysHash: 100, // get from blockchain | ||
keys: [/* published keys got from blockchain */], | ||
} as DsnpKeys, | ||
pages: [/* published graph pages got from blockchain */], | ||
} as ImportBundle; | ||
const imported = await graph.importUserData([import_bundle]); | ||
// get all associated user without keys so we can fetch and import keys for them | ||
const user_without_keys = await graph.getConnectionsWithoutKeys(); | ||
let users_import_bundles = [] as ImportBundle[]; | ||
for (const user of user_without_keys) { | ||
let user_dsnp_keys = DsnpKeys {..} // fetch published DsnpKeys for user | ||
let user_pages = .. // fetch published private friendship pages for the user | ||
let user_import_bundle = ImportBundle { | ||
dsnpUserId: user, | ||
schemaId: private_friendship_graph_schema_id, | ||
keyPairs: []. // empty key pairs for user since we don't know and need their secret keys | ||
dsnpKeys: user_dsnp_keys, | ||
pages: user_pages, | ||
} as ImportBundle; | ||
} | ||
const imported = await graph.importUserData(users_import_bundles); | ||
const connect_action: ConnectAction = { | ||
type: "Connect", | ||
ownerDsnpUserId: dsnpOwnerId, | ||
connection: { | ||
dsnpUserId: 2, | ||
schemaId: private_friendship_graph_schema_id, | ||
} as Connection, | ||
dsnKeys: { | ||
dsnpUserId: 2, | ||
keysHash: 100, | ||
keys: [/* get keys from chain for user 2 */], | ||
} as DsnpKeys, | ||
} as ConnectAction; | ||
const actions = [] as Action[]; | ||
actions.push(connect_action); | ||
const applied = await graph.applyActions(actions); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
6365363
56
723
340
1
2