Skip to main content
Protocol extensions for the x402 payment standard.

Installation

# npm
npm install @armory-sh/extensions

# yarn
yarn add @armory-sh/extensions

# pnpm
pnpm add @armory-sh/extensions

# bun
bun add @armory-sh/extensions

Available Extensions

ExtensionPurpose
Sign-In-With-XWallet authentication for repeat access
Payment IdentifierIdempotency for payment requests
BazaarResource discovery

API Reference

Hook Creators (Client-Side)

Hooks allow you to automatically handle server extension requirements.

createSIWxHook

Creates a hook that handles Sign-In-With-X authentication:
import { createSIWxHook } from '@armory-sh/extensions';

const hook = createSIWxHook({
  domain: 'example.com',
  statement: 'Sign in to access premium content',
  expirationSeconds: 3600
});
Config Options:
OptionTypeDescription
domainstringDomain requesting sign-in
statementstringHuman-readable statement
expirationSecondsnumberSeconds until expiration

createPaymentIdHook

Creates a hook that adds payment idempotency:
import { createPaymentIdHook } from '@armory-sh/extensions';

// Auto-generate payment ID
const hook = createPaymentIdHook();

// Or specify a custom ID
const hook = createPaymentIdHook({
  paymentId: 'my-custom-id-123'
});
Config Options:
OptionTypeDescription
paymentIdstringCustom payment identifier

createCustomHook

Create custom extension hooks:
import { createCustomHook } from '@armory-sh/extensions';

const customHook = createCustomHook({
  key: 'my-extension',
  handler: async (context) => {
    // Modify the payment payload
    if (context.payload) {
      context.payload.extensions = {
        ...(context.payload.extensions ?? {}),
        'my-extension': { data: 'value' }
      };
    }
  },
  priority: 75  // Higher priority runs first
});

Server Extension Declaration

For server-side middleware, declare extensions that clients should provide:

declareSIWxExtension

import { declareSIWxExtension } from '@armory-sh/extensions';

const extension = declareSIWxExtension({
  domain: 'api.example.com',
  statement: 'Sign in to access this API',
  network: 'eip155:8453',
  expirationSeconds: 3600
});

declarePaymentIdentifierExtension

import { declarePaymentIdentifierExtension } from '@armory-sh/extensions';

// Require payment ID from clients
const extension = declarePaymentIdentifierExtension({
  required: true
});

declareDiscoveryExtension

import { declareDiscoveryExtension } from '@armory-sh/extensions';

const extension = declareDiscoveryExtension({
  required: true
});

Verification

Verify client-provided extension data:

validateSIWxMessage

import { validateSIWxMessage, parseSIWxHeader } from '@armory-sh/extensions';

const payload = parseSIWxHeader(header);

const result = validateSIWxMessage(
  payload,
  'https://api.example.com/resource',
  { maxAge: 3600 }
);

if (result.valid) {
  console.log('Valid SIWX message');
} else {
  console.error('Invalid:', result.error);
}

verifySIWxSignature

import { verifySIWxSignature } from '@armory-sh/extensions';

const result = await verifySIWxSignature(payload, {
  evmVerifier: async (message, signature, address) => {
    // Custom signature verification
    return true;
  }
});

Utilities

generatePaymentId

Generate a random payment identifier:
import { generatePaymentId } from '@armory-sh/extensions';

const id = generatePaymentId();

extractExtension

Extract extension data from a payload:
import { extractExtension } from '@armory-sh/extensions';

const siwxData = extractExtension(payload.extensions, 'siwx');

Constants

import {
  SIGN_IN_WITH_X,
  PAYMENT_IDENTIFIER,
  BAZAAR,
} from '@armory-sh/extensions';

Types

import type {
  SIWxHookConfig,
  PaymentIdHookConfig,
  CustomHookConfig,
  SIWxExtensionConfig,
  PaymentIdentifierConfig,
  BazaarDiscoveryConfig,
  SIWxPayload,
  SIWxExtensionInfo,
  PaymentIdentifierExtensionInfo,
  BazaarExtensionInfo,
  Extension,
} from '@armory-sh/extensions';

Exports Summary

ExportDescription
createSIWxHookHook for Sign-In-With-X
createPaymentIdHookHook for payment idempotency
createCustomHookCreate custom hooks
declareSIWxExtensionDeclare SIWX extension for server
declarePaymentIdentifierExtensionDeclare payment ID extension
declareDiscoveryExtensionDeclare Bazaar discovery extension
validateSIWxMessageValidate SIWX payload
verifySIWxSignatureVerify SIWX signature
parseSIWxHeaderParse SIWX header
encodeSIWxHeaderEncode SIWX header
createSIWxMessageCreate SIWX message
createSIWxPayloadCreate SIWX payload
generatePaymentIdGenerate random payment ID
extractExtensionExtract extension data
isSIWxExtensionCheck if extension is SIWX
isPaymentIdentifierExtensionCheck if extension is Payment ID
isDiscoveryExtensionCheck if extension is Bazaar discovery