up4w-js v1.1.10
UP4WJS
up4w-js is a collection of JavaScript libraries written in TypeScript, with built-in type declaration files. It is used for interacting with the UP4W JSON API and is compatible with both Node.js and browser environments. It supports different coding styles for importing, including TypeScript, ECMAScript module, and CommonJS.
Installation
You can install the package either using NPM or using Yarn
Using NPM
npm install up4w-js up4w-serviceUsing Yarn
yarn add up4w-js up4w-serviceOverview
As you see, to communicate with the UP4W network, you need to install two package: up4w-js and up4w-service
up4w-service is responsible for automatically detecting the system architecture at runtime, launching the corresponding underlying UP4W program, and asynchronously returning the network access point of the currently running UP4W instance.
up4w-service can only run in Node.js environment.
| Package | Description |
|---|---|
| up4w-service | automatically detecting the system architecture at runtime, launching the corresponding underlying UP4W program. |
up4w-js consists of multiple independent core modules, each responsible for managing interactions with a specific set of interfaces in the underlying up4w system.
| Package | Description |
|---|---|
| up4w-js | A collection of all interfaces for interacting with the underlying UP4W network.It is also one of the two packages that you must use, the other one being up4w-service. |
Data response structure
All interfaces are implemented to return a Promise asynchronously and non-blocking. The data structure returned is as follows: | Field | Type | Description | Required | |----|----|----|---| | rsp | string | The module action |Y| | ret | object | The data returned has a specific structure that can vary depending on the interface. | N | | fin | boolean | indicates whether more messages will be responded |N| | err | number | Error reason, returned only when an error occurs. | N |
More infomation about response structure UEP_0021
Here is a example:
{
rsp: "msg.received",
ret: {
id: "3866091278",
timestamp: 1684658460224,
app: 1,
action: 4096,
recipient: "<a_base64_encode_address>"
sender: ":myself",
content: "Explain quantum computing in simple terms",
content_type: 13,
},
}Usage example
up4w-js supports both ES modules and CommonJS imports. In the upcoming examples, you can use import or require based on your module system.
We will use import by default for demonstration purposes.
// `es module` or `Typescript` style
import UP4WJS from "up4w-js";
import UP4WService from "up4w-service"
// or you can use commonjs style
const UP4WJS = require('up4w-js')
const UP4WService = require('up4w-service')
// now create instance
const service = new UP4WService()
start()
async function start() {
const response = await service.run()
// Got available endpoints, `ws` means websocket, the other one is `http`
// Use `ws` please for now
const { availableEndpoint: { ws } } = response
// Create up4w-js instance, UP4WJS class need only one arguments: endpoint
const up4wjs = new UP4WJS(ws)
// Waiting for up4wjs to complete initialization.
await up4wjs.whenReady({
app_name: "deso",
mrc: {
msgs_dir: ":mem",
default_swarm: "<a_base64_address>", //replace it with your swarm address
flags: ["delay00_load"],
},
mlt: {},
gdp: {},
pbc: {},
lsm: {},
})
// In the following documentation, you will learn more about the details of interface calls.
// do what you want ...
}up4w-service
Detecting the system architecture at runtime, launching the corresponding underlying UP4W network. In Linux systems, it will run up4w.so, in macOS it will run up4w.dylib, and in Windows it will run up4w.dll.
import UP4WService from "up4w-service"
const service = new UP4WService()
async function start() {
const resp = await service.run()
const {
availableEndpoint: { ws },
} = resp
// output: ws://127.0.0.1:52416/api
// `52416` is a random port number.
console.log(ws)
}service.run()
Running the underlying UP4W program.
Parameters: none
Returns:
Promise resolved with a object
- availableEndpoint
object- ws
stringAvailable websocket endpoint, with randomly assigned port number that may vary each time it is started - http
stringAvailable http endpoint, also with randomly assigned port number
- ws
up4wjs
A collection of methods for interacting with the underlying UP4W network.
import UP4WJS from "up4w-js"
const up4w = new UP4WJS(endpoint)Parameters:
| Field | Type | Description |
|---|---|---|
| endpoint | string | The entry point provided by the underlying UP4W network currently only supports WebSocket |
You need to obtain the
endpointthroughup4w-service.
Returns
A instance of up4wjs
Exmaple
import UP4WService from "up4w-service"
import UP4WJS from "up4w-js"
const service = new UP4WService()
async function start() {
const resp = await service.run()
const {
availableEndpoint: { ws },
} = resp
// create instance
const up4w = new UP4WJS(ws)
...
}up4w.version()
Get version and build information of the peer, no arguments.
const version = await up4w.version()
// output:{ rsp: 'core.ver', inc: '41c870e4389fceab', ret: 'version 1.1, build May 20 2023 20:45:33' }
conole.log(version)Parameters
none
Returns
Promise object resolved with a version string
up4w.whenReady(params)
Initialize all desired modules.
const result = await up4w.whenReady({
app_name: 'deso',
mrc: {
msgs_dir: ':mem',
default_swarm: "<a_base64_address>",
flags: ['delay00_load'],
},
mlt: {},
gdp: {},
pbc: {},
lsm: {},
})
console.log(result) // output: trueParameters:
params - object
app_nameis the name of the application, instances with differentapp_namewill not discover each other in nearby peers (UEP-8)mrcinitialize message relay core (UEP-12),msgs_dirspecifies the storage directory for saving pooled messages, or its value can be:memto just memory for temporary storage.media_dirspecifies storage directory for saving offload media, if specified, the distributed media store (UEP-16) will be initialized as well.dvsinitialize distributed key-value store (UEP-20) withkv_dirspecifies the storage directory, or memory.- flags
- "delay_load" indicates the
mediadb or thekvdb is not loaded when initialization - "db_dedicate" two db on disk, one for default swarm, another for all non-default ones (merged)
- "db_separated" one separated db on disk for every swarm
- "db_merged" a single db for all swarms (merged)
- "delay_load" indicates the
hobenable packet obfuscation (UEP-6).lsminitialize nearby peers module (UEP-8).mltenable multi-link tunnelsgdpinitialize gossip data protocol (UEP-11), it will be automatically initialized ifmrcordvsis specified.pbcenable packet bouncer
Returns
Promise resolved with boolean, true means initialize succesfully.
up4w.shutdown()
Uninitialize the UP4W stacks, no arguments, no return. Shutdown is unrecoverable.
await up4w.shutdown()Parameters
none
Returns
Promise object resolved with null
up4w.contact
up4w.contact.signinWithSeed(seed, profile)
Set current sign-in user by seed(private key)
const user = await up4w.contact.siginWithSeed("<a_base64_seed_string>")
// output '{"rsp":"social.signin","inc":"51db8a36ac7ef161","ret":{"pk":"e6t9rv1...pkgen3y40v73z0"}}'
console.log(user)Parameters
seed is the 28-byte root seed and mnemonic is its mnemonic encoding (UEP-13). Only one of seed and mnemonic is required to be specified.'
Returns
Promise object resolved with:
- pk -
stringa public key that has been encoded usingBase64
up4w.contact.siginWithMnemonic(words, profile)
Set current sign-in user by mnemonic words, this function is equivalent to up4w.siginWithSeed, but the difference lies in the input parameter.
const user = await up4w.contact.siginWithMnemonic("migrant adipex ... laos since")
// output '{"rsp":"social.signin","inc":"51db8a36ac7ef161","ret":{"pk":"e6t9rv1...pkgen3y40v73z0"}}'
console.log(user)Parameters
- words -
stringA list of 18 mnemonic words separated by spaces. - profie -
objectSame tosiginWithSeed
Returns
Same to up4w.signinWithSeed
up4w.contact.addUser(user)
Add a new user in the contact list, no return. This method will not send a greeting message to the user.
const result = await up4w.contact.addUser({
pk: "Target_User_PK",
name: 'Lufi'
})
// output {"rsp":"social.add_user","inc":"43bbe30cc17b88c6","ret":null}
cosnole.log(result)Parameters
user - object
- pk
stringa public key that has been encoded usingBase64 - name
stringUser's name , optional - gender
stringUser's gender, optional - geolocation
stringUser's geographical location, optional - greeting_secret
stringis the greeting secret required for adding a new friend, optional
Returns
Promise object resolved with null ret
up4w.contact.removeUser(pk)
Remove an existing user, no return.
const result = await up4w.contact.removeUser(Target_User_PK)
// output {"rsp":"social.remove_user","inc":"43bbe30cc17b88c6","ret":null}
cosnole.log(result)Parameters
- pk
stringa public key that has been encoded usingBase64
Returns
Promise object resolved with null ret
up4w.msg
up4w.msg.onMessage(callback, receiveParams)
Subscribe to message push notifications to listen for all events pushed by the UP4W network, such as chat replies, and so on.
import UP4WService from "up4w-service"
import UP4WJS from "up4w-js"
const service = new UP4WService()
async function start() {
const resp = await service.run()
const {
availableEndpoint: { ws },
} = resp
const up4w = new UP4WJS(ws)
await up4w.whenReady()
await up4w.contact.siginWithSeed("<a_base64_seed_string>")
// Receive message push notifications as early as possible.
up4w.msg.onMessage((message) => {
console.log("Receive message: ", message)
}, {app: 1})
}
start()The message looks like:
{
"rsp": "msg.received",
"ret": {
"swarm": " ... ",
"id": "795100302",
"timestamp": 1684752857888,
"app": 1,
"action": 4097,
"recipient": "<a_base64_encode_address>",
"sender": "<a_base64_encode_address>",
"content": "Quantum computing is a type of computing that ...",
"content_type": 13
}
}Parameters
callback:
function(message)- swram -
stringthe swarm DHT address in base16, or the alias as set by join swarm request (req:"swarm.join", UEP-21) idthe id of the message in the scope of current swarm, auint64_tin string.- timestamp -
numberof the message in millisecond - sender -
string: the public key of the sender in base64 - app -
numberthe built-in application id, anuint16_t - action -
numberthe operation code specific to the application, anuint16_t - recipient -
stringstands for the recipient, which is a user'spubkeyor that of a scenario-specific identity (UEP-13) - content -
stringthe content of the message can be parsed object, a plain text, or a base64 string according toappandaction - content_type -
numberthe type of the content (UEP-17) - media -
arrayattached media blobs . Note that, a pair of<timestamp, crc>is used to uniquely identify a message.
- swram -
receiveParams -
object- conversation -
stringabase64encoded public key, optional - app -
numberapplication id, optional
- conversation -
Returns
none
up4w.msg.sendText(message)
Send a text message to a specific recipient.
const up4w = new UP4WJS(ws)
await up4w.whenReady()
await up4w.contact.siginWithSeed("<a_base64_seed_string>")
up4w.msg.onMessage((message) => {
console.log("Receive message: ", message)
})
await up4w.contact.addUser({
pk: <a_base64_encode_publickey>",
name: 'Lufi',
})
const result = await up4w.sendText({
"recipient": "<a_base64_encode_address>",
"app": 1,
"action": 4096,
"content": "Explain quantum computing in simple terms",
"content_type": 13
})
console.log(result)result:
{
"rsp": "msg.text",
"inc": "be30cc17b88c6c76",
"ret": {
"swarm": "<your_swarm_address>",
"id": "1816980011727310231",
"timestamp": 1684752850240
}
}Parameters
- swarm -
stringthe swarm DHT address inbase16,optional - recipient -
stringstands for the recipient, which is a user'spubkeyor that of a scenario-specific identity - app -
numberapplication_id - action -
numberoperation code - content -
stringobjectmessage content - content_type -
numbercontent type, text message always be13, optional
Returns
Promise object, resolved with:
- swarm -
stringthe swarm DHT address - id -
stringid of message - timestamp -
numberSending time for millisecond
up4w.swarm
up4w.swarm.join(node)
Join a swarm and initialize swarm-specific protocols, no return.
await up4w.swarm.join(node)Parameters
node - object
addressthe 20-byte DHT address of the swarm in base16secretthe 32-byte private swarm secret in base64, optional. If specified, the swarm will be private swarmepochthe epoch unit in millisecond for the DAGM (UEP-12)ttlthe max TTL in epoch for the DAGMsubbandthe range of the subband, should be 2^n.media_sizemaxthe maximum size of attached media in messages, unspecified or 0 indicates media attachment is not allowedactiveinbound message will be pushed as a (rsp:"msg_received") message (UEP-22) if decryption succeededvalue_sizemaxthe maximum size of values in DVS (UEP-20)
Returns
Promise Object , resolved with null ret
up4w.swarm.leave(address)
Leave a swarm
await up4w.swarm.leave(address)Parameters
addressthe 20-byte DHT address of the swarm in base16
Returns
Promise Object , resolved with null ret
up4w.persistence
Distributed Key-Value Storage
up4w.persistence.set(data)
Set a value .
const result = await up4w.persistence.set(data)
// output {"rsp":"netkv.set","inc":"43bbe30cc17b88c6","ret":null}
console.log(result)Parameters
data- object
keythe 32-byte datakey in base 64 (UEP-21)slotthe storage slot of the value (0~255)ttlthe TTL in seconds of the valuevaluethe value data in base64secretthe AES secret for value encryption, optional.
Returns
Promise Object , resolved with null ret
up4w.persistence.get(data)
Get a value.
const result = await up4w.persistence.set(data)
// output {"rsp":"netkv.get","inc":"43bbe30cc17b88c6","ret":null}
console.log(result)Parameters
data- object
keythe 32-byte datakey in base 64(UEP-20)slotthe storage slot of the value (0~255)secretthe secret for value encryption, optional.rawindicate the response to be the raw binary media data, optional and synchronous invocation only
Returns
Promise object, resolved with vary result:
if the value is not encrypted, or correct secret is provided, a
base64encoded string will be return;if
rawis true, the response is just the raw binary data without the json formatted response encapsulation.