0.0.0 • Published 2 years ago

qubic-js v0.0.0

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

qubic-js

Qubic client library. This is work in progress.

Installation

With pnpm:

pnpm add qubic-js

With yarn:

yarn add qubic-js

With npm:

npm i qubic-js

qubic

qubic.exports.crypto : Promise.<Crypto>

A promise which always resolves to object with crypto functions.

qubic.createClient(options) ⇒ Client

Emits: info, open, close, error, inclusion, rejection

ParamTypeDefaultDescription
optionsobjectClient options.
options.seedstringSeed in 55 lowercase latin chars.
options.indexnumber0Identity index.
options.connectionConnectionClient connection.
options.computorsArray.<object>Specifies 3 computors to connect to, and with what options. Ignored when connection option is used.
options.computors[].urlstringComputor url.
[options.computors[].options]objectWebSocket options.
options.synchronizationIntervalnumberIf no new tick appears after this interval an info event is emitted with updated sync status. Ignored when connection option is used.
options.adminPublicKeystringAdmin public key, for verification of current epoch and tick which are signed by admin. Ignored when connection option is used.
options.reconnectTimeoutDurationnumber100Reconnect timeout duration. Ignored when connection option is used.
options.dbobjectDatabase implementing the level interface, for storing transfers.
options.dbPathstringDatabase path.

Example

import { createClient } from 'qubic-js';

const client = createClient({
  seed: 'vmscmtbcqjbqyqcckegsfdsrcgjpeejobolmimgorsqwgupzhkevreu',
  computors: [
    { url: 'wss://AA.computor.com' },
    { url: 'wss://AB.computor.com' },
    { url: 'wss://AC.computor.com' },
  ],
  synchronizationInterval: 60 * 1000,
  adminPublicKey: '97CC65D1E59351EEFC776BCFF197533F148A8105DA84129C051F70DD9CA0FF82',
});

client.addListener('error', function (error) {
  console.log(error.message);
});
client.addListener('info', console.log);

qubic.createConnection(params) ⇒ Connection

Emits: info, open, close, error

ParamTypeDefaultDescription
paramsobjectConnection params.
params.computorsArray.<object>Specifies 3 computors to connect to, and with what options.
params.computors[].urlstringComputor url.
[params.computors[].options]objectWebSocket options. Node.js only.
params.synchronizationIntervalnumberIf no new tick appears after this interval an info event is emitted with updated sync status.
params.adminPublicKeystringAdmin public key, for verification of current epoch and tick which are signed by admin.
params.reconnectTimeoutDurationnumber100Reconnect timeout duration.

Example

import { createConnection } from 'qubic-js';

const connection = createConnection({
  computors: [
    { url: 'wss://AA.computor.com' },
    { url: 'wss://AB.computor.com' },
    { url: 'wss://AC.computor.com' },
  ],
  synchronizationInterval: 60 * 1000,
  adminPublicKey: '97CC65D1E59351EEFC776BCFF197533F148A8105DA84129C051F70DD9CA0FF82',
});

connection.addListener('error', function (error) {
  console.log(error.message);
});
connection.addListener('info', console.log);

qubic.privateKey(seed, index, K12) ⇒ Uint8Array

Generates a private key from seed.

Returns: Uint8Array - Private key bytes.

ParamTypeDescription
seedstringSeed in 55 lowercase latin chars.
indexnumberIdentity index.
K12K12K12 function.

qubic.createIdentity(seed, index) ⇒ Promise.<string>

Creates an identity with checksum.

Returns: Promise.<string> - Identity with checksum in uppercase hex.

ParamTypeDescription
seedstringSeed in 55 lowercase latin chars.
indexnumberIdentity index.

qubic.verifyChecksum(identity) ⇒ Promise.<boolean>

Validates integrity of identity with checksum.

ParamTypeDescription
identitystringIdentity in uppercase hex.

qubic.seedChecksum(seed) ⇒ Promise.<string>

Returns: Promise.<string> - Seed checksum in uppercase hex.

ParamTypeDescription
seedstringSeed in 55 lowercase latin chars.

qubic.createTransfer(params) ⇒ Promise.<object>

Creates a transfer of energy between 2 entities.

ParamTypeDescription
paramsobject
params.seedstringSeed in 55 lowercase latin chars.
params.fromobject
params.from.identitystringSender identity in uppercase hex.
params.from.indexnumberIndex of private key which was used to derive sender identity.
params.from.identityNoncenumberIdentity nonce.
params.from.enerybigintEnergy of sender identity.
params.toobject
params.to.identitystringRecipient identity in uppercase hex.
params.to.energybigintTransferred energy to recipient identity.

Example

import { createTransfer } from 'qubic-js';

createTransfer({
  seed: 'vmscmtbcqjbqyqcckegsfdsrcgjpeejobolmimgorsqwgupzhkevreu',
  from: {
    identity: '9F6ADD0C591DBB8C0CE1EDF6F63A9E1C7BD22CFBD20DE1469ADAA76A9C0023707BE416',
    index: 1337,
    identityNonce: 0,
    energy: bigInt(2),
  },
  to: {
    identity: 'CD5B4A78521A9F9428F442E60E25DA63247817AB6BBF406CC91393F6664E38CBFE68DC',
    energy: bigInt(1),
  },
})
  .then(function (transfer) {
    console.log(transfer);
  })
  .catch(function (error) {
    console.log(error.message);
  });

Client

Mixes: Connection

"inclusion"

Inclusion event.

Properties

NameTypeDescription
hashstringHash of included transfer in uppercase hex.
epochnumberEpoch at which transfer was included.
ticknumberTick at which transfer was included.

"rejection"

Rejection event.

Properties

NameTypeDescription
hashstringHash of rejected transfer in uppercase hex.
reasonstringReason of rejection.

Client.identity : string

Client identity in uppercase hex.

Client.createTransfer(to) ⇒ object

Returns: object - Transfer object.

ParamTypeDescription
toobject
to.identitystringRecipient identity in uppercase hex.
to.energybigintTransferred energy to recipient identity.

Client.terminate(options)

Closes database and connections to computors.

ParamTypeDefault
optionsobject
options.closeConnectionbooleantrue

Client.launch()

Launches client by opening database and connections to computors.

Emits: info, open, close, error, inclusion, rejection

Client.close()

Terminates all 3 WebSocket connections.

Mixes: close

Client.sendCommand(command, payload) ⇒ Promise.<(object|void)>

Sends a client command to each connected computor, and compares responses before resolving. Available client commands:

CommandPayloadResponseDescription
1{ identity }{ identity, identityNonce }Fetches identityNonce.
2{ identity }{ identity, energy }Fetches energy.
3{ message, signature }voidSends a transfer with base64-encoded message & signature fields.
4{ hash }{ hash, inclusionState, tick, epoch } or { hash, reason }Fetches status of a transfer. Rejects with reason in case account nonce has been overwritten.

Mixes: sendCommand

ParamTypeDescription
commandnumberCommand index, must be an integer.
payloadobjectPayload.

Client.setComputorUrl(index, url)

Sets one of the 3 computors url each time.

Mixes: setComputorUrl

ParamTypeDescription
indexnumberIndex of computor connection, 0, 1 or 2.
urlstringComputor url.

Client.open()

Opens all 3 WebSocket connections.

Mixes: open
Emits: info, open, close, error

Client.computors() ⇒ Array.<string>

Mixes: computors
Returns: Array.<string> - Array of computor urls.

Connection

"info"

Info event.

Properties

NameTypeDescription
syncStatusnumberIndicates which of the 3 computors have provided the same tick and epoch. 0 when offline, 3 when fully synced.
epochnumberCurrent epoch.
ticknumberCurrent tick.

"open" (event)

Open event. Emitted when a WebSocket connection opens.

ParamTypeDescription
eventeventWebSocket event.

"error" (event)

Error event. Emitted when a WebSocket connection errors.

ParamTypeDescription
eventeventWebSocket event.

"close" (event)

Close event. Emitted when a WebSocket connection closes.

ParamTypeDescription
eventeventWebSocket event.

Connection.close()

Terminates all 3 WebSocket connections.

Connection.sendCommand(command, payload) ⇒ Promise.<(object|void)>

Sends a client command to each connected computor, and compares responses before resolving. Available client commands:

CommandPayloadResponseDescription
1{ identity }{ identity, identityNonce }Fetches identityNonce.
2{ identity }{ identity, energy }Fetches energy.
3{ message, signature }voidSends a transfer with base64-encoded message & signature fields.
4{ hash }{ hash, inclusionState, tick, epoch } or { hash, reason }Fetches status of a transfer. Rejects with reason in case account nonce has been overwritten.
ParamTypeDescription
commandnumberCommand index, must be an integer.
payloadobjectPayload.

Connection.setComputorUrl(index, url)

Sets one of the 3 computors url each time.

ParamTypeDescription
indexnumberIndex of computor connection, 0, 1 or 2.
urlstringComputor url.

Connection.open()

Opens all 3 WebSocket connections.

Emits: info, open, close, error

Connection.computors() ⇒ Array.<string>

Returns: Array.<string> - Array of computor urls.

Crypto : object

Crypto.schnorrq : object

schnorrq.generatePublicKey(secretKey) ⇒ Uint8Array

ParamType
secretKeyUint8Array

schnorrq.sign(secretKey, publicKey, message) ⇒ Uint8Array

ParamType
secretKeyUint8Array
publicKeyUint8Array
messageUint8Array

schnorrq.verify(publicKey, message, signature) ⇒ number

Returns: number - 1 if valid, 0 if invalid

ParamType
publicKeyUint8Array
messageUint8Array
signatureUint8Array

Crypto.K12(input, output, outputLength, outputOffset)

ParamTypeDefault
inputUint8Array
outputUint8Array
outputLengthnumber
outputOffsetnumber0