How to use @requestnetwork/types - 10 common examples

To help you get started, we’ve selected a few @requestnetwork/types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RequestNetwork / requestNetwork / packages / usage-examples / src / request-logic-clear-request.ts View on Github external
(async (): Promise => {
  // Data access setup
  const dataAccess = new DataAccess(new MockStorage());
  await dataAccess.initialize();

  // A transaction manager, for example @requestnetwork/transaction-manager
  const transactionManager: TransactionTypes.ITransactionManager = new TransactionManager(
    dataAccess,
  );

  const requestLogic = new RequestLogic(transactionManager, signatureProvider);

  const signerIdentity = {
    type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
    value: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
  };

  // optionally, compute the request ID before actually creating it.
  const requestId = await requestLogic.computeRequestId(createParams, signerIdentity);
  console.log(`The request will be created with ID ${requestId}`);

  const { result } = await requestLogic.createRequest(createParams, signerIdentity);

  return result;
})()
  .then(request => {
github RequestNetwork / requestNetwork / packages / utils / src / simple-logger.ts View on Github external
// tslint:disable:no-console
import { Log as LogTypes } from '@requestnetwork/types';

// The default log level to use if none is used at the constructor.
const DEFAULT_LOG_LEVEL = LogTypes.LogLevel.INFO;

/**
 * Simple logger that outputs content to the console.
 */
export default class SimpleLogger implements LogTypes.ILogger {
  /**
   * maxLogLevel, the maximum log level to display
   */
  public maxLogLevel: LogTypes.LogLevel;

  /**
   * The output console to use for logging
   */
  public output = console;

  /**
github RequestNetwork / requestNetwork / packages / request-client.js / src / api / payment-network / erc20 / mainnet-address-based.ts View on Github external
import { AdvancedLogicTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import * as Types from '../../../types';

import ERC20AddressBased from './address-based';

const PAYMENT_NETWORK_ERC20_ADDRESS_BASED = ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_ADDRESS_BASED;

// ERC20 DAI contract address
// TODO: currently hard-coded to DAI, should get from ERC20 token list
const erc20ContractAddress = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359';

/**
 * Handle payment networks with mainnet BTC based address extension
 *
 * @class PaymentNetworkERC20AddressBased
 */
export default class PaymentNetworkERC20AddressBased implements Types.IPaymentNetwork {
  private erc20AddressBased: ERC20AddressBased;

  /**
   * @param advancedLogic Instance of Advanced Logic layer, to get the extension
   */
github RequestNetwork / requestNetwork / packages / advanced-logic / src / extensions / content-data.ts View on Github external
}

  if (requestState.extensions[ExtensionTypes.ID.CONTENT_DATA]) {
    throw Error(`This extension has already been created`);
  }

  if (!extensionAction.parameters.content) {
    throw Error('No content has been given for the extension content-data');
  }

  // Deep copy to not mutate the input parameter
  const copiedExtensionState: RequestLogicTypes.IExtensionStates = Utils.deepCopy(extensionsState);

  copiedExtensionState[ExtensionTypes.ID.CONTENT_DATA] = {
    events: [],
    id: ExtensionTypes.ID.CONTENT_DATA,
    type: ExtensionTypes.TYPE.CONTENT_DATA,
    values: { content: extensionAction.parameters.content },
    version: CURRENT_VERSION,
  };

  return copiedExtensionState;
}
github RequestNetwork / requestNetwork / packages / advanced-logic / src / extensions / content-data.ts View on Github external
function applyActionToExtension(
  extensionsState: RequestLogicTypes.IExtensionStates,
  extensionAction: ExtensionTypes.IAction,
  requestState: RequestLogicTypes.IRequest,
): RequestLogicTypes.IExtensionStates {
  if (extensionAction.action !== ExtensionTypes.ContentData.ACTION.CREATE) {
    throw Error(`Unknown action: ${extensionAction.action}`);
  }

  if (requestState.extensions[ExtensionTypes.ID.CONTENT_DATA]) {
    throw Error(`This extension has already been created`);
  }

  if (!extensionAction.parameters.content) {
    throw Error('No content has been given for the extension content-data');
  }

  // Deep copy to not mutate the input parameter
  const copiedExtensionState: RequestLogicTypes.IExtensionStates = Utils.deepCopy(extensionsState);

  copiedExtensionState[ExtensionTypes.ID.CONTENT_DATA] = {
    events: [],
    id: ExtensionTypes.ID.CONTENT_DATA,
    type: ExtensionTypes.TYPE.CONTENT_DATA,
    values: { content: extensionAction.parameters.content },
    version: CURRENT_VERSION,
github RequestNetwork / requestNetwork / packages / prototype-estimator / src / size.ts View on Github external
};
const signerIdentity: IdentityTypes.IIdentity = {
  type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
  value: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
};

const requestCreationHash: RequestLogicTypes.ICreateParameters = {
  currency: {
    network: 'mainnet',
    type: RequestLogicTypes.CURRENCY.ETH,
    value: 'ETH',
  },
  expectedAmount: '100000000000',
  payee: signerIdentity,
  payer: {
    type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
    value: '0x740fc87Bd3f41d07d23A01DEc90623eBC5fed9D6',
  },
};

// Signature provider setup
const signatureProvider = new EthereumPrivateKeySignatureProvider(signatureInfo);

// Advanced logic setup
const advancedLogic = new AdvancedLogic();

/**
 * Sets up the test environment: instantiate the layers, including a mock storage
 *
 * @returns {Promise}
 */
async function setup(): Promise<{ mockStorage: MockStorage; requestLogic: RequestLogic }> {
github RequestNetwork / requestNetwork / packages / request-logic / specs / example / example.ts View on Github external
async function foo(): Promise {
  // Bob (the payee)
  const bobRaw = {
    identity: {
      type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
      value: '0xAf083f77F1fFd54218d91491AFD06c9296EaC3ce',
    },
    signatureParams: {
      method: SignatureTypes.METHOD.ECDSA,
      privateKey: '0x04674d2e53e0e14653487d7323cc5f0a7959c83067f5654cafe4094bde90fa8a',
    },
  };

  // Alice (the payer)
  const aliceRaw = {
    identity: {
      type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
      value: '0x740fc87Bd3f41d07d23A01DEc90623eBC5fed9D6',
    },
    signatureParams: {
      method: SignatureTypes.METHOD.ECDSA,
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / create.ts View on Github external
}

  if (!Utils.amount.isValid(requestParameters.expectedAmount)) {
    throw new Error('expectedAmount must be a positive integer');
  }

  if (
    requestParameters.payee &&
    requestParameters.payee.type !== IdentityTypes.TYPE.ETHEREUM_ADDRESS
  ) {
    throw new Error('payee.type not supported');
  }

  if (
    requestParameters.payer &&
    requestParameters.payer.type !== IdentityTypes.TYPE.ETHEREUM_ADDRESS
  ) {
    throw new Error('payer.type not supported');
  }

  if (!requestParameters.timestamp) {
    requestParameters.timestamp = Utils.getCurrentTimestampInSecond();
  }

  // convert expectedAmount to string to have a consistent numbering
  requestParameters.expectedAmount = requestParameters.expectedAmount.toString();
  const version = Version.currentVersion;

  const unsignedAction: RequestLogicTypes.IUnsignedAction = {
    name: RequestLogicTypes.ACTION_NAME.CREATE,
    parameters: requestParameters,
    version,
github RequestNetwork / requestNetwork / packages / usage-examples / src / request-logic-clear-request.ts View on Github external
import { EthereumPrivateKeySignatureProvider } from '@requestnetwork/epk-signature';
import { RequestLogic } from '@requestnetwork/request-logic';
import { TransactionManager } from '@requestnetwork/transaction-manager';
import {
  IdentityTypes,
  RequestLogicTypes,
  SignatureProviderTypes,
  SignatureTypes,
  TransactionTypes,
} from '@requestnetwork/types';

import MockStorage from './mock/mock-storage';

const createParams = {
  currency: {
    type: RequestLogicTypes.CURRENCY.ETH,
    value: 'ETH',
  },
  expectedAmount: '170000000000',
  payee: {
    type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
    value: '0xAf083f77F1fFd54218d91491AFD06c9296EaC3ce',
  },
  payer: {
    type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
    value: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
  },
  timestamp: 1544426030,
};

// A signature provider, for example @requestnetwork/epk-signature
const signatureProvider: SignatureProviderTypes.ISignatureProvider = new EthereumPrivateKeySignatureProvider(
github RequestNetwork / requestNetwork / packages / request-client.js / src / api / request-network.ts View on Github external
}> {
    const requestParameters = parameters.requestInfo;
    const paymentNetworkCreationParameters = parameters.paymentNetwork;
    const contentData = parameters.contentData;
    const topics = parameters.topics || [];

    if (requestParameters.extensionsData) {
      throw new Error('extensionsData in request parameters must be empty');
    }

    // if request currency is a string, convert it to currency object
    if (typeof requestParameters.currency === 'string') {
      requestParameters.currency = stringToCurrency(requestParameters.currency);
    } else {
      // If ERC20, validate that the value is a checksum address
      if (requestParameters.currency.type === RequestLogicTypes.CURRENCY.ERC20) {
        if (!validERC20Address(requestParameters.currency.value)) {
          throw new Error(
            'The ERC20 currency address needs to be a valid Ethereum checksum address',
          );
        }
      }
    }

    // avoid mutation of the parameters
    const copiedRequestParameters = Utils.deepCopy(requestParameters);
    copiedRequestParameters.extensionsData = [];

    let paymentNetwork: Types.IPaymentNetwork | null = null;
    if (paymentNetworkCreationParameters) {
      paymentNetwork = PaymentNetworkFactory.createPaymentNetwork({
        advancedLogic: this.advancedLogic,