Skip to content

Forming and Signing Transactions

The following methods create and sign transactions, which you can then broadcast to the ADAMANT network:

Further reading:

TransactionType

TransactionType is an enum whose values represent Transaction Types.

ts
export enum TransactionType {
  SEND,
  SIGNATURE,
  DELEGATE,
  VOTE,
  MULTI,
  DAPP,
  IN_TRANSFER,
  OUT_TRANSFER,
  CHAT_MESSAGE,
  STATE,
}
ts
import {WebSocketClient, TransactionType} from 'adamant-api';

const ws = new WebSocketClient();

ws.on(TransactionType.SEND, tx => {
  console.log('Got a Token Transfer!');
});

export {ws};

See WebSocket Connections.

createSendTransaction()

Forms and signs a type 0 (Token Transfer) transaction.

ts
type SendTransactionData = {
  keyPair: {
    publicKey: Buffer;
    privateKey: Buffer;
  };
  recipientId: string;
  amount: number;
};

Returns a formed send transaction object with a signature, ready for broadcasting.

ts
import {createSendTransaction, createKeypairFromPassphrase} from 'adamant-api';

const keyPair = createKeypairFromPassphrase('apple banana...');

createSendTransaction({
  keyPair,
  recipientId: 'U123...',
  amount: 1000000, // amount in SAT
});

createStateTransaction()

Forms and signs a type 9 (Store data in KVS) transaction.

ts
type StateTransactionData = {
  keyPair: {
    publicKey: Buffer;
    privateKey: Buffer;
  };
  key: string;
  value: string;
};
ts
import {createStateTransaction, createKeypairFromPassphrase} from 'adamant-api';

const keyPair = createKeypairFromPassphrase('apple banana...');

createStateTransaction({
  keyPair,
  key: 'hello',
  value: 'world',
});

createChatTransaction()

Forms and signs a type 8 (Chat Message) transaction.

ts
enum MessageType {
  Chat = 1,
  Rich = 2,
  Signal = 3,
}

type ChatTransactionData = {
  keyPair: {
    publicKey: Buffer;
    privateKey: Buffer;
  };
  recipientId: `U${string}`;
  message_type: MessageType;
  amount?: number;
  message: string;
  own_message: string;
};
ts
import {
  createChatTransaction,
  createKeypairFromPassphrase,
  MessageType,
} from 'adamant-api';

const keyPair = createKeypairFromPassphrase('apple banana...');

createChatTransaction({
  keyPair,
  recipientId: 'U123...',
  message_type: MessageType.Chat,
  message: 'Hello, world!',
  own_message: '1',
});

createDelegateTransaction()

Forms and signs a type 2 (Delegate Registration) transaction.

ts
type DelegateTransactionData = {
  keyPair: {
    publicKey: Buffer;
    privateKey: Buffer;
  };
  username: string;
};
ts
import {
  createDelegateTransaction,
  createKeypairFromPassphrase,
} from 'adamant-api';

const keyPair = createKeypairFromPassphrase('apple banana...');

createDelegateTransaction({
  keyPair,
  username: 'my_unique_username',
});

createVoteTransaction()

Forms and signs a type 3 (Vote For Delegate) transaction.

ts
type VoteTransactionData = {
  keyPair: {
    publicKey: Buffer;
    privateKey: Buffer;
  };
  votes: string[];
};
ts
import {createVoteTransaction, createKeypairFromPassphrase} from 'adamant-api';

const keyPair = createKeypairFromPassphrase('apple banana...');

createVoteTransaction({
  keyPair,
  votes: [
    '+b3d0c0b99f64d0960324089eb678e90d8bcbb3dd8c73ee748e026f8b9a5b5468',
    '-9ef1f6212ae871716cfa2d04e3dc5339e8fe75f89818be21ee1d75004983e2a8',
  ],
});

Millisecond-precision timestamps

ADAMANT Node v0.10.0 adds an optional millisecond-precision timestampMs field on transactions. Every builder accepts an optional timestampMs:

ts
import {createSendTransaction, createKeypairFromPassphrase} from 'adamant-api';
import {getEpochTimeMs} from 'adamant-api';

const keyPair = createKeypairFromPassphrase('apple banana...');

createSendTransaction({
  keyPair,
  recipientId: 'U123...',
  amount: 1000000,
  timestampMs: getEpochTimeMs(), // optional, millisecond-precision
});

Signing semantics are unchanged

timestampMs is attached to the transaction object but excluded from the signed bytes. The transaction hash, ID, and signature are computed exactly as before, so they are unchanged whether or not you pass timestampMs.

Key points:

  • Opt-in and backward compatible. Omit timestampMs and behavior is identical to earlier versions. It is safe to send to nodes that predate v0.10.0 — they ignore the extra field; upgraded nodes persist it.
  • The second-precision timestamp is derived from timestampMs as Math.floor(timestampMs / 1000), so the two always agree within the same second. If you pass neither, the current epoch time is used.
  • Use getEpochTimeMs() to obtain an ADAMANT-epoch millisecond timestamp, and getEpochTime() for the second-precision value.

For the complete, generated type signatures of each helper, see the API Reference.

Released under the GPL-3.0 License.