JSPM

  • Created
  • Published
  • Downloads 14
  • Score
    100M100P100Q54153F
  • License MIT

A comprehensive wallet SDK for React Native (pwc), supporting multi-chain and multi-account features.

Package Exports

  • p-sdk-wallet
  • p-sdk-wallet/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (p-sdk-wallet) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

p-sdk-wallet

A comprehensive, secure, and easy-to-use wallet SDK for React Native applications from PWC.

This SDK provides a high-level Vault API to manage multiple accounts (from mnemonic or private keys) under a single password, similar to MetaMask. It's built with high security in mind, using AES and PBKDF2 for strong encryption.

Features

  • 🔐 High Security: Uses AES and PBKDF2 for strong encryption of the entire vault.
  • 🏛️ Vault Architecture: Manages multiple accounts under a single password.
  • 🔑 Multi-Account: Supports HD wallets (BIP-44) and imported private key accounts.
  • 🌐 Multi-Chain: Ready for any EVM-compatible chain.
  • 👨‍💻 Dev-Friendly API: A simple, high-level API that abstracts away cryptographic complexity.

Installation

npm install p-sdk-wallet
# or
yarn add p-sdk-wallet

⚠️ Important: Setup Required

To use this SDK, your React Native application needs some configuration. Please follow these steps carefully.

Step 1: Install All Required Packages

Install the SDK itself, its peer dependencies, and all necessary polyfill packages with one command.

yarn add p-sdk-wallet ethers react-native-keychain @react-native-async-storage/async-storage react-native-get-random-values buffer process text-encoding stream-browserify events

Step 2: Configure Metro for Node.js Core Modules

Some libraries used by the SDK depend on Node.js core modules (stream, events) that don't exist in React Native. You need to tell Metro (the bundler) to use the browser-compatible versions you just installed.

Modify your metro.config.js at the root of your project:

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');

/**
 * Metro configuration
 * https://reactnative.dev/docs/metro
 *
 * @type {import('@react-native/metro-config').MetroConfig}
 */
const config = {
  resolver: {
    extraNodeModules: {
      // Polyfill for stream and events
      stream: require.resolve('stream-browserify'),
      events: require.resolve('events/'),
    },
  },
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

Note: After changing metro.config.js, you must restart your bundler with a cache reset: npx react-native start --reset-cache.

Step 3: Set up Global Polyfills

Finally, load the remaining polyfills into the global environment of your app.

In your main entry file (index.js or App.tsx), add the following code at the very top, before any other imports:

// --- Start of Polyfills ---

// Required for crypto operations
import 'react-native-get-random-values';

// Polyfill for Buffer
if (typeof global.Buffer === 'undefined') {
  global.Buffer = require('buffer').Buffer;
}

// Polyfill for process (required by some dependencies)
global.process = require('process');
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';

// Polyfill for TextEncoder/TextDecoder (required by some dependencies)
import { TextEncoder, TextDecoder } from 'text-encoding';
if (typeof global.TextEncoder === 'undefined') {
  global.TextEncoder = TextEncoder;
}
if (typeof global.TextDecoder === 'undefined') {
  global.TextDecoder = TextDecoder;
}

// --- End of Polyfills ---


// Your other imports and AppRegistry call...

Quick Start

import { Vault, type Account, type ChainId } from 'p-sdk-wallet';

// --- Create or Load a Vault ---

// Create a new vault with a fresh mnemonic
const { vault, encryptedVault } = await Vault.createNew('your-secure-password');

// You should save `encryptedVault` securely using Keychain or AsyncStorage
// For example: await Keychain.setGenericPassword('my-vault', JSON.stringify(encryptedVault));

// Later, you can load the vault
// const credentials = await Keychain.getGenericPassword();
// const loadedVault = await Vault.load('your-secure-password', JSON.parse(credentials.password));


// --- Account Management ---

// Add a new HD account
const account1 = await vault.addNewHDAccount();
console.log('New HD Account:', account1.address);

// Import an account from a private key
const account2 = await vault.importAccount('0x...');
console.log('Imported Account:', account2.address);

// Get all accounts
const allAccounts: Account[] = vault.getAccounts();


// --- Blockchain Interaction ---

// Get native balance (e.g., ETH on Ethereum mainnet)
const balance = await vault.getNativeBalance(account1.address, '1' as ChainId);
console.log('Balance:', ethers.formatEther(balance));

// Send native tokens
const txResponse = await vault.sendNativeToken(
  account1.address,
  '0xRecipientAddress...',
  '0.01', // 1 ETH
  '1' as ChainId
);
console.log('Transaction Sent:', txResponse.hash);

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

MIT