Powered by Coinflow
Payments Β· Documentation
Operational

πŸ‘ Wallet Implementation

This page is for advanced / cryptocurrency-native companies. If that’s not you, head back to the Quickstart for the standard flows.

Overview

Coinflow identifies every end-user making a purchase with a wallet. You can think of the wallet as a unique customer ID that helps us identify the customer. Merchants whose end-users don’t have crypto wallets can use the below code snippets to generate a wallet and pass it to Coinflow.

Generate Wallet Public Keys

Some Coinflow endpoints will require you to pass a wallet public key in the x-coinflow-auth-wallet header. The CoinflowPurchase component will also require you to pass a wallet object. The below snippets are examples of how you can generate a wallet public key from a unique customer id. The unique customer id should be identified by you, the merchant. Examples of what you can use here are: a UUID you use internally to identify the end-user or the end-user’s email address.

Public Keys on EVM Chains

How to generate pubkeyfor EVM chains:

const crypto = require('crypto');

async function createPubKey() {
  const userId = '123456789abcdefg'; // Replace this with any string that uniquely identifies the user in your system.
  const hash = crypto
    .createHash('sha256')
    .update(userId)
    .digest('hex')
    .substring(0, 40);
  const pubkey = `0x${hash}`;
  console.log(pubkey); // The public key to pass onto coinflow 
}

createPubKey();

Public Keys on Solana

How to generate pubkeyfor Solana:

import * as web3 from '@solana/web3.js';
import * as crypto from 'crypto';

async function createPubKey() {
  const userId = '123456789abcdefg'; //  Replace this with any string that uniquely identifies the user in your system.
  const hash = crypto
    .createHash('sha256')
    .update(userId)
    .digest();
  
  // Creates a Keypair from the hashed value
  const keyPair = web3.Keypair.fromSeed(hash.slice(0, 32)); // Seed must be 32 bytes

  const pubkey = keyPair.publicKey.toBase58();
  console.log(pubkey); // The public key to pass onto coinflow 
}

createPubKey();

Generate Solana Wallet Objects

Generate A Solana Wallet From Email Address

Merchants implementing CoinflowPurchase can use the following code snippet to generate a Solana wallet object from an end-user’s email address and pass it into the wallet prop. This feature is supported in React, Vue, and Angular for Coinflow package version 10.2.10 or later.

// How to create a wallet for the payer
// Creating a wallet for the payer is necessary to enable saved payments.
const getWallet = async (
  email: string, 
  env: 'sandbox' | 'prod'
): Promise<SolanaWallet | null> => {
  return CoinflowUtils.getWalletFromEmail({ email, merchantId: '<YOUR_MERCHANT_ID>', env });
};

const [wallet, setWallet] = useState<SolanaWallet| null>(null);
useEffect(() => {getWallet(email).then(setWallet)}, [email]);

if (!wallet) return null;

// Passing the wallet to the CoinflowPurchase component
<CoinflowPurchase
	...
  wallet={wallet} // pass the wallet generated from getWallet()
/>

Generate a Solana Wallet Object From User Id

Merchants integrating CoinflowPurchase can utilize the code snippet below to generate a Solana wallet object using a user ID of your choice. This userId can be any string you use to identify your customer, and the returned wallet object should be passed into the wallet prop. This functionality is available in React, Vue, and Angular with Coinflow package version 10.2.10 or later.

// How to create a wallet for the payer
async function createWallet() {
  return await CoinflowUtils.getWalletFromUserId({
    userId: '<USER_ID>',
    merchantId: '<YOUR_MERCHANT_ID>',
    env: 'sandbox' | 'prod'
  });
}

const [wallet, setWallet] = useState<SolanaWallet | null>(null);

useEffect(() => {
  createWallet().then(setWallet).catch((error) => {
    console.error('Failed to create wallet:', error);
  });
}, []);

if (!wallet) return null;

// Passing the wallet to the CoinflowPurchase component
<CoinflowPurchase
  // other props
  wallet={wallet} // pass the wallet generated from createWallet()
/>