npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ethereum-attestation-service/transitive-trust-sdk

v0.1.4

Published

A TypeScript SDK for computing transitive trust scores in a graph

Downloads

13

Readme

Transitive Trust Graph

This project implements a Transitive Trust Graph system in TypeScript, allowing for the computation of trust scores between nodes in a directed graph with separate positive and negative weights on edges.

Features

  • Create a directed graph with nodes and weighted edges
  • Compute trust scores between nodes in the graph
  • Handle both positive and negative trust weights
  • Efficient implementation using a priority queue for score propagation

Installation

To use this package in your project, install it via npm:

npm install @ethereum-attestation-service/transitive-trust-sdk

Usage

Here are examples of how to use the TransitiveTrustGraph class:

Example 1: Computing trust scores for specific targets

import { TransitiveTrustGraph } from "@ethereum-attestation-service/transitive-trust-sdk";

const graph = new TransitiveTrustGraph();

// Add edges to the graph
graph.addEdge("A", "B", 0.6, 0.2);
graph.addEdge("B", "C", 0.4, 0.1);
graph.addEdge("C", "D", 0.5, 0.3);
graph.addEdge("A", "C", 0.5, 0.1);

// Compute trust scores for specific targets
const scores = graph.computeTrustScores("A", ["D"]);
console.log(scores);
// Output: { D: { positiveScore: 0.2, negativeScore: 0.12, netScore: 0.08 } }

Example 2: Computing trust scores for the entire graph

import { TransitiveTrustGraph } from "@ethereum-attestation-service/transitive-trust-sdk";

const graph = new TransitiveTrustGraph();

// Add edges to the graph
graph.addEdge("A", "B", 0.8, 0.1);
graph.addEdge("B", "C", 0.6, 0.2);
graph.addEdge("C", "D", 0.7, 0.3);
graph.addEdge("A", "C", 0.5, 0.1);
graph.addEdge("B", "D", 0.4, 0.1);

// Compute trust scores for the entire graph from node "A"
const allScores = graph.computeTrustScores("A");
console.log(allScores);
// Output:
// {
//   B: { positiveScore: 0.8, negativeScore: 0.1, netScore: 0.7 },
//   C: { positiveScore: 0.74, negativeScore: 0.17, netScore: 0.57 },
//   D: { positiveScore: 0.518, negativeScore: 0.219, netScore: 0.299 }
// }

// You can also get all nodes and edges in the graph
const nodes = graph.getNodes();
console.log("Nodes:", nodes);
// Output: Nodes: ["A", "B", "C", "D"]

const edges = graph.getEdges();
console.log("Edges:", edges);
// Output: Edges: [
//   { source: "A", target: "B", positiveWeight: 0.8, negativeWeight: 0.1 },
//   { source: "B", target: "C", positiveWeight: 0.6, negativeWeight: 0.2 },
//   { source: "C", target: "D", positiveWeight: 0.7, negativeWeight: 0.3 },
//   { source: "A", target: "C", positiveWeight: 0.5, negativeWeight: 0.1 },
//   { source: "B", target: "D", positiveWeight: 0.4, negativeWeight: 0.1 }
// ]

API Reference

TransitiveTrustGraph

The TransitiveTrustGraph class provides the following methods:

addNode(node: string): void

Adds a node to the graph.

  • Parameters:
    • node: string - The node to add.
  • Throws:
    • Error if the node is not a non-empty string.

addEdge(source: string, target: string, positiveWeight: number, negativeWeight: number): void

Adds an edge to the graph with separate positive and negative weights.

  • Parameters:
    • source: string - The source node.
    • target: string - The target node.
    • positiveWeight: number - The positive weight of the edge (between 0 and 1, inclusive).
    • negativeWeight: number - The negative weight of the edge (between 0 and 1, inclusive).
  • Throws:
    • Error if the source or target is not a non-empty string, or if the weights are not between 0 and 1 (inclusive).

computeTrustScores(source: string, targets?: string[]): { [target: string]: { positiveScore: number; negativeScore: number; netScore: number } }

Computes the trust scores between a source node and specified target nodes (or all nodes if no targets are specified).

  • Parameters:
    • source: string - The source node.
    • targets?: string[] - Optional array of target nodes. If empty, computes for all nodes.
  • Returns:
    • An object containing the trust scores for the specified target nodes or all nodes.
  • Throws:
    • Error if the source or any target node is not found in the graph.

getNodes(): string[]

Returns all nodes in the graph.

  • Returns:
    • An array of all nodes.

getEdges(): { source: string; target: string; positiveWeight: number; negativeWeight: number }[]

Returns all edges in the graph.

  • Returns:
    • An array of objects representing edges, each containing:
      • source: string - The source node of the edge.
      • target: string - The target node of the edge.
      • positiveWeight: number - The positive weight of the edge.
      • negativeWeight: number - The negative weight of the edge.

Development

To set up the project for development:

  1. Clone the repository
  2. Install dependencies: npm install
  3. Run tests: npm test

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.