TAP-WASM
WebAssembly bindings for the Transaction Authorization Protocol (TAP).
Features
- WebAssembly Support: Run TAP in browser and Node.js environments
- DIDComm Integration: Full support for DIDComm v2 messaging
- TAP Message Types: Support for all TAP message types
- Agent Management: Create and manage TAP agents
- Message Handling: Create, sign, and verify TAP messages
- Serialization: Efficient serialization between Rust and JavaScript
- Performance: Optimized for browser performance
- Shared Core: Uses the same core implementation as the native TAP agent
Installation
# Using npm
# Using yarn
Basic Usage
Browser with ES modules
import init from 'tap-wasm';
// Recommended pattern using a static create method
;
Node.js
const tap_wasm = require;
// Recommended pattern using a static create method
;
API Reference
Message Creation and Handling
Creating a Message
// Create a new message
const message = agent.;
// The message will have the following structure:
// {
// id: "msg_...", // Auto-generated UUID
// type: "https://tap.rsvp/schema/1.0#Transfer",
// from: "agent's DID",
// to: [],
// body: {},
// created: <timestamp>
// }
Message Properties
// Access and modify message properties
message. = "msg_123"; // Message ID
message. = "https://tap.rsvp/schema/1.0#Transfer"; // Message type
message. = "did:example:123"; // Sender DID
message. = ; // Recipient DIDs
message. = ; // Message body
message. = Date.; // Created timestamp
message. = Date. + 3600000; // Expiry timestamp
message. = "thread_123"; // Thread ID
message. = "parent_thread_123"; // Parent thread ID
Agent Management
Creating an Agent
// RECOMMENDED: Create a new agent using static factory pattern
// Use the factory method (RECOMMENDED)
const agent = await ;
// ALTERNATIVELY: Create directly (NOT RECOMMENDED - may cause WASM initialization errors)
// Only use this approach if you're certain WASM is already initialized
const agent2 = ;
Agent Operations
// Get agent properties
const did = agent.;
const nickname = agent.;
// Message packing and unpacking
const packedResult = await agent.;
const unpackedMessage = await agent.;
// Create a message
const newMessage = agent.;
// Register a message handler
agent.;
// Process an incoming message
const result = await agent.;
// Subscribe to all messages
const unsubscribe = agent.;
Node Management
// Create a TAP node
const node = ;
// Add agents to the node
node.;
node.;
// Get agents
const agent = node.;
const allAgents = node.;
// Remove an agent
node.;
Utility Functions
// Generate a UUID
const uuid = ;
Integration with tap-agent
This implementation wraps the core tap-agent
Rust crate, using its WASM-compatible features. This ensures compatibility and consistency between the WASM bindings and the native Rust implementation.
The integration:
- Uses the
WasmAgent
trait from thetap-agent
crate - Wraps the
TapAgent
implementation with WASM bindings - Provides JavaScript-friendly methods for all operations
- Leverages the same cryptographic operations as the native agent
Building from Source
Prerequisites
- Rust and Cargo (https://rustup.rs/)
- wasm-pack (https://rustwasm.github.io/wasm-pack/installer/)
Build Steps
# Clone the repository
# Build the WebAssembly package
# The output will be in the pkg/ directory
Examples
For more examples, see the examples directory.
Browser Example
A complete browser example is available at examples/browser-agent-example.html. It demonstrates:
- Creating a TAP agent
- Creating and modifying TAP messages
- Packing and unpacking messages
- Handling events
License
MIT License