Examples

Step-by-step examples to help developers get started with IPDW, from installation to data synchronization.

This section provides practical examples for developers looking to integrate IPDW into their applications. It covers the installation process, interface instantiation, and key-value data management within the IPDW. The examples also demonstrate how data can be synchronized automatically between devices using P2P communication, providing a hands-on guide to leveraging IPDW's full capabilities.

To get started with IPDW, you will need to follow these steps:

Install the package:

$ npm install ipdw

Instantiate ipdw with this interface:

public static async Create(privateKey: string, storageProvider: StorageProvider, salt?: Buffer): PromiseThen access to "data", which is a sharded map where you can get and set in a key-value style.

The full example with auto synchronization between devices:

import {IPDW, MemoryStorageProvider} from "ipdw";

// On device 1
(async function () {
    const privateKey = '0xb577c4367d79f1a7a0c8353f7937d601758d92c35df958781d72d70f9177e52f';
    const provider = await IPDWStorageProvider.Init(privateKey, new MemoryStorageProvider());
    const dataWallet = await DataWallet.Create(privateKey, provider);
    
    await dataWallet.set('test1', 'hello');

    const value1 = await dataWallet.get('test1');
    console.log('test1 value:', value1);
    // test1 value: hello

    // Run "device 2" and if reachable it will be discovered and synced
    const value2 = await dataWallet.get('test2');
    console.log('test2 value:', value2);
    // test2 value: world
})();

// On device 2
(async function () {
    const privateKey = '0xb577c4367d79f1a7a0c8353f7937d601758d92c35df958781d72d70f9177e52f';
    const provider = await IPDWStorageProvider.Init(privateKey, new MemoryStorageProvider());
    const dataWallet = await DataWallet.Create(privateKey, provider);
    
    await ipdw.data.set('test2', 'world');
})();

Last updated