12.1.2 • Published 13 days ago

@fireblocks/ncw-js-sdk v12.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
13 days ago

Fireblocks NCW JS SDK

The Official Javascript & Typescript SDK for Fireblocks Non-Custodial Wallet

Quick Start

Initialization

import {
  FireblocksNCWFactory,
  IEventsHandler,
  IFireblocksNCW,
  IMessagesHandler,
  TEvent,
  InMemorySecureStorageProvider,
} from "@fireblocks/ncw-js-sdk";

// Example Device ID
const deviceId = "f16e05e0-1869-4a6b-9678-17a1c14ed482";

// Initiate secure storage to hold generated data during SDK usage.
const secureStorageProvider = new InMemorySecureStorageProvider();

// Register a message handler to process outgoing message to your API
const messagesHandler: IMessagesHandler = {
  handleOutgoingMessage: (message: string) => {
    // Send the message to your API service
    return {} as any;
  },
};

// Register an events handler to handle on various events that the SDK emitts
const eventsHandler: IEventsHandler = {
  handleEvent: (event: TEvent) => {
    if (
      event.type === "key_descriptor_changed" ||
      event.type === "key_takeover_changed" ||
      event.type === "transaction_signature_changed" ||
      event.type === "join_wallet_descriptor"
    ) {
      // Do something when the event is fired.
      console.log(event);
    }
  },
};

// Initialize Fireblocks NCW SDK
const fireblocksNCW = await FireblocksNCWFactory({
  deviceId,
  messagesHandler,
  eventsHandler,
  secureStorageProvider,
});

Generate MPC Keys

import { IKeyDescriptor, TMPCAlgorithm } from "@fireblocks/ncw-js-sdk";

// Generate MPC Keys
const algorithms: Set<TMPCAlgorithm> = new Set(["MPC_CMP_ECDSA_SECP256K1"]);
const keyDescriptor: Set<IKeyDescriptor> = await fireblocksNCW.generateMPCKeys(algorithms);

The generate MPC keys process will emit IKeyTakeoverChangedEvent events.

Sign Transaction

import { ITransactionSignature } from "@fireblocks/ncw-js-sdk";

// Sign transaction
const result: ITransactionSignature = await fireblocksNCW.signTransaction("SOME_TX_UUID");
console.log(
  `txId: ${result.txId}`,
  `status: ${result.transactionSignatureStatus}`, // "PENDING" | "STARTED" | "COMPLETED" | "TIMEOUT" | "ERROR"
);

The sign process will emit ITransactionSignatureChangedEvent events.

Secure Storage

The following example uses a custom secure storage.

const secureStorageProvider = new PasswordEncryptedLocalStorage(deviceId, () => {
  const password = prompt("Enter password", "");
  if (password === null) {
    return Promise.reject(new Error("Rejected by user"));
  }
  return Promise.resolve(password || "");
});

// Initialize Fireblocks NCW SDK with your custom secure storage
const fireblocksNCW = await FireblocksNCW.initialize(deviceId, messagesHandler, eventsHandler, secureStorageProvider);

An example implementation of secure storage based on a user password.

import {
  BrowserLocalStorageProvider,
  ISecureStorageProvider,
  TReleaseSecureStorageCallback,
  decryptAesGCM,
  encryptAesGCM,
} from "@fireblocks/ncw-js-sdk";
import { md } from "node-forge";

export type GetUserPasswordFunc = () => Promise<string>;

/// This secure storage implementations creates an encryption key on-demand based on a user password

export class PasswordEncryptedLocalStorage extends BrowserLocalStorageProvider implements ISecureStorageProvider {
  private encKey: string | null = null;

  constructor(
    private _salt: string,
    private getPassword: GetUserPasswordFunc,
  ) {
    super();
  }

  public async getAccess(): Promise<TReleaseSecureStorageCallback> {
    this.encKey = await this._generateEncryptionKey();
    return async () => {
      await this._release();
    };
  }

  private async _release(): Promise<void> {
    this.encKey = null;
  }

  public async get(key: string): Promise<string | null> {
    if (!this.encKey) {
      throw new Error("Storage locked");
    }

    const encryptedData = await super.get(key);
    if (!encryptedData) {
      return null;
    }

    return decryptAesGCM(encryptedData, this.encKey, this._salt);
  }

  public async set(key: string, data: string): Promise<void> {
    if (!this.encKey) {
      throw new Error("Storage locked");
    }

    const encryptedData = await encryptAesGCM(data, this.encKey, this._salt);
    await super.set(key, encryptedData);
  }

  private async _generateEncryptionKey(): Promise<string> {
    let key = await this.getPassword();
    const md5 = md.md5.create();

    for (let i = 0; i < 1000; ++i) {
      md5.update(key);
      key = md5.digest().toHex();
    }

    return key;
  }
}

Development

  1. Clone the repository
  2. Install the dependencies with yarn
  3. Build the library with yarn build
  4. Run tests with yarn test

Tech

12.1.2

13 days ago

12.1.2-beta.1

28 days ago

12.1.1

1 month ago

12.0.1

2 months ago

12.0.1-rc.1

2 months ago

12.0.1-beta.4

2 months ago

12.0.1-beta.3

2 months ago

12.0.1-beta.2

2 months ago

12.0.1-beta.1

2 months ago

12.0.0

3 months ago

12.0.0-rc.1

3 months ago

11.0.2

3 months ago

11.0.2-beta.1

3 months ago

11.0.1

3 months ago

11.0.1-rc.3

3 months ago

11.0.1-rc.2

3 months ago

11.0.1-rc.1

3 months ago

11.0.0-rc.2

4 months ago

11.0.0

4 months ago

11.0.0-rc.1

4 months ago

11.0.0-beta.3

4 months ago

10.0.7

4 months ago

11.0.0-beta.1

4 months ago

11.0.0-beta.2

4 months ago

10.0.7-rc.4

4 months ago

10.0.7-rc.3

4 months ago

10.0.7-rc.2

4 months ago

10.0.6

4 months ago

10.0.6-rc.2

4 months ago

10.0.6-rc.4

4 months ago

10.0.6-beta.7

4 months ago

10.0.5

4 months ago

10.0.5-beta.5

4 months ago

10.0.5-beta.6

4 months ago

10.0.5-beta.1

4 months ago

10.0.5-beta.3

4 months ago

10.0.4

5 months ago

10.0.3-qa

5 months ago

10.0.3-pre-release

5 months ago

10.0.2

5 months ago

10.0.1

5 months ago

9.5.0

6 months ago

9.4.0

6 months ago

9.3.2

6 months ago

9.3.1

6 months ago

9.3.0

6 months ago

9.2.1

6 months ago

9.2.0

6 months ago

9.1.0

7 months ago

9.0.3

7 months ago

9.0.2

7 months ago

9.0.1

7 months ago

9.0.0

7 months ago

8.2.1

7 months ago

8.2.0

7 months ago

8.1.0

7 months ago

8.0.3

7 months ago

8.0.2

7 months ago

8.0.1

7 months ago

8.0.0

7 months ago

7.1.0

7 months ago

7.0.0

7 months ago

6.0.1

7 months ago

6.0.0

7 months ago

5.0.1

7 months ago

5.0.0

7 months ago

4.0.3

7 months ago

4.0.2

7 months ago

4.0.1

8 months ago

3.0.1

8 months ago

3.0.0

8 months ago

2.0.2

8 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.2.1

9 months ago

1.2.0

9 months ago

1.1.6

9 months ago

1.1.5

9 months ago

1.1.4

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.4

9 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago