Skip to content

Documentation / @cashconnect-js/templates-dev / wallet/wallet-p2pkh

WalletP2PKHGenesisData ​

ts
type WalletP2PKHGenesisData = object;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:37

Properties ​

type ​

ts
type: "WalletP2PKH";

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:38

privateKey ​

ts
privateKey: string;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:39


WalletP2PKHFactory() ​

ts
type WalletP2PKHFactory<T> = (dependencies, genesisData) => Promise<T>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:480

Type Parameters ​

Type ParameterDefault type
T extends WalletP2PKHWalletP2PKH

Parameters ​

ParameterType
dependenciesWalletDependencies
genesisDataWalletP2PKHGenesisData

Returns ​

Promise<T>


WalletP2PKH ​

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:52

Example ​

ts
// Instantiate P2PKH Wallet.
const walletP2PKH = WalletP2PKH.fromWIF('someWif');

// Start monitoring the balance.
await walletP2PKH.startMonitoring();

Extends ​

Constructors ​

Constructor ​

ts
new WalletP2PKH(dependencies, genesisData): WalletP2PKH;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:138

Parameters ​
ParameterType
dependenciesRequired<WalletDependencies>
genesisDataOmit<WalletP2PKHGenesisData, "type">
Returns ​

WalletP2PKH

Overrides ​

BaseWallet.constructor

Methods ​

from() ​

ts
static from<T>(
   this, 
   deps, 
   genesisData): Promise<InstanceType<T>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:53

Type Parameters ​
Type Parameter
T extends typeof WalletP2PKH
Parameters ​
ParameterType
thisT
depsWalletDependencies
genesisDataOmit<WalletP2PKHGenesisData, "type">
Returns ​

Promise<InstanceType<T>>

fromBytes() ​

ts
static fromBytes<T>(
   this, 
   services, 
   privateKeyBytes): Promise<InstanceType<T>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:70

Type Parameters ​
Type Parameter
T extends typeof WalletP2PKH
Parameters ​
ParameterType
thisT
servicesWalletDependencies
privateKeyBytesUint8Array
Returns ​

Promise<InstanceType<T>>

fromHex() ​

ts
static fromHex<T>(
   this, 
   services, 
   privateKeyHex): Promise<InstanceType<T>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:84

Type Parameters ​
Type Parameter
T extends typeof WalletP2PKH
Parameters ​
ParameterType
thisT
servicesWalletDependencies
privateKeyHexstring
Returns ​

Promise<InstanceType<T>>

fromWIF() ​

ts
static fromWIF<T>(
   this, 
   services, 
   privateKeyWif): Promise<InstanceType<T>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:98

Type Parameters ​
Type Parameter
T extends typeof WalletP2PKH
Parameters ​
ParameterType
thisT
servicesWalletDependencies
privateKeyWifstring
Returns ​

Promise<InstanceType<T>>

generateRandom() ​

ts
static generateRandom<T>(this, services): Promise<InstanceType<T>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:112

Type Parameters ​
Type Parameter
T extends typeof WalletP2PKH
Parameters ​
ParameterType
thisT
servicesWalletDependencies
Returns ​

Promise<InstanceType<T>>

deriveKey() ​

ts
static deriveKey(genesisData, keyName): Uint8Array<ArrayBufferLike>;

Defined in: packages/templates-dev/src/wallet/base-wallet.ts:51

Derive a key to use based on the given Genesis Data.

Parameters ​
ParameterType
genesisDataWalletGenesisData
keyNamestring
Returns ​

Uint8Array<ArrayBufferLike>

Remarks ​

These keys can be used for Wallet Id's, Signing Keys, Encryption Keys and for Dapp Samdboxes.

Inherited from ​

BaseWallet.deriveKey

deriveId() ​

ts
static deriveId(genesisData): Uint8Array<ArrayBufferLike>;

Defined in: packages/templates-dev/src/wallet/base-wallet.ts:64

Parameters ​
ParameterType
genesisDataWalletGenesisData
Returns ​

Uint8Array<ArrayBufferLike>

Inherited from ​

BaseWallet.deriveId

start() ​

ts
start(): Promise<void>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:159

Start monitoring this wallet for any transaction activity.

Returns ​

Promise<void>

Overrides ​

BaseWallet.start

stop() ​

ts
stop(): Promise<void>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:178

Stop monitoring this wallet for any transaction activity.

Returns ​

Promise<void>

Overrides ​

BaseWallet.stop

destroy() ​

ts
destroy(): Promise<void>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:197

Remove any event listeners from this wallet.

Returns ​

Promise<void>

Overrides ​

BaseWallet.destroy

getTransactions() ​

ts
getTransactions(): Promise<ExtMap<{
  sourceOutputs: Output[];
  hash: TransactionHash;
  height: number;
  transaction: Transaction;
}>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:201

Fetch all transactions from the blockchain involving this wallet.

Returns ​

Promise<ExtMap<{ sourceOutputs: Output[]; hash: TransactionHash; height: number; transaction: Transaction; }>>

Remarks ​

We will want to add a query argument so that workers can use this efficiently:

ts
{
  limit?: number;
  offset?: number;
  sort?: () => number;
  filter?: () => boolean;
}

This "query" argument should be turned into a generic type as we will want to re-use it for Activities.

Overrides ​

BaseWallet.getTransactions

getUnspents() ​

ts
getUnspents(): Promise<BlockchainUTXOs>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:264

Fetch all unspents from the blockchain belonging to this wallet.

Returns ​

Promise<BlockchainUTXOs>

Overrides ​

BaseWallet.getUnspents

getUnspentDirectives() ​

ts
getUnspentDirectives(): Promise<ExtMap<{
  outpointIndex: number;
  outpointTransactionHash: Uint8Array<ArrayBufferLike>;
  sequenceNumber: number;
  unlockingBytecode: {
     compiler: Compiler<CompilationContextBch, CompilerConfiguration<CompilationContextBch>, AuthenticationProgramStateCommon>;
     data: {
        keys: {
           privateKeys: {
              key: Bytes;
           };
        };
     };
     script: string;
  };
  sourceOutput: {
     lockingBytecode: {
        compiler: Compiler<CompilationContextBch, CompilerConfiguration<CompilationContextBch>, AuthenticationProgramStateCommon>;
        data: {
           keys: {
              privateKeys: {
                 key: Bytes;
              };
           };
        };
        script: string;
     };
     token:   | {
        amount: bigint;
        category: Uint8Array;
        nft?: {
           capability: "none" | "mutable" | "minting";
           commitment: Uint8Array;
        };
      }
        | undefined;
     valueSatoshis: bigint;
  };
}>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:280

Fetch all unspents from the blockchain belonging to this wallet.

Returns ​

Promise<ExtMap<{ outpointIndex: number; outpointTransactionHash: Uint8Array<ArrayBufferLike>; sequenceNumber: number; unlockingBytecode: { compiler: Compiler<CompilationContextBch, CompilerConfiguration<CompilationContextBch>, AuthenticationProgramStateCommon>; data: { keys: { privateKeys: { key: Bytes; }; }; }; script: string; }; sourceOutput: { lockingBytecode: { compiler: Compiler<CompilationContextBch, CompilerConfiguration<CompilationContextBch>, AuthenticationProgramStateCommon>; data: { keys: { privateKeys: { key: Bytes; }; }; }; script: string; }; token: | { amount: bigint; category: Uint8Array; nft?: { capability: "none" | "mutable" | "minting"; commitment: Uint8Array; }; } | undefined; valueSatoshis: bigint; }; }>>

Remarks ​

I was considering removing this and just storing unspents as "UTXOSignable"s. But that might not be ideal if we want to have Watch-only wallets. A better approach might be to have Fetch Unspents return Array<UTXO | UTXOSignable> Still need to flesh that out.

EDIT: We're going to want to store state for caching anyway... May as well just getUnspentDirectives() from the local state instead of fetch. Likewise for balances.

Overrides ​

BaseWallet.getUnspentDirectives

getBalanceSats() ​

ts
getBalanceSats(): Promise<bigint>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:318

Get the satoshi balance of this wallet.

Returns ​

Promise<bigint>

Remarks ​

Consider confirmed/unconfirmed balances (or some other representation).

This becomes important in some instances because services like BitPay require one confirmation. This'll also have bearing on how we define our UTXO Selection strategies.

Overrides ​

BaseWallet.getBalanceSats

getBalanceTokens() ​

ts
getBalanceTokens(): Promise<TokenBalances>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:331

Get the token balance of this wallet.

Returns ​

Promise<TokenBalances>

Remarks ​

Consider confirmed/unconfirmed balances (or some other representation).

This becomes important in some instances because services like BitPay require one confirmation. This'll also have bearing on how we define our UTXO Selection strategies.

Overrides ​

BaseWallet.getBalanceTokens

getAddresses() ​

ts
getAddresses(): Promise<ExtMap<Address>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:344

Get all addresses belonging to this wallet.

Returns ​

Promise<ExtMap<Address>>

Overrides ​

BaseWallet.getAddresses

getReceivingAddress() ​

ts
getReceivingAddress(): Promise<Address>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:350

Get the address that this wallet should receive to.

Returns ​

Promise<Address>

Overrides ​

BaseWallet.getReceivingAddress

signTransaction() ​

ts
signTransaction(txTemplate): Promise<TransactionBuilder>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:354

Select UTXOs from this wallet to meet the amounts required.

Parameters ​
ParameterType
txTemplatePartial<TransactionTemplate>
Returns ​

Promise<TransactionBuilder>

Remarks ​

TODO: We are going to want to return the Transaction that was compiled.

TODO: This should accept a second argument (probably function) that can filter/sory UTXOs. The intent here is to provide a "spending policy". E.g. Only use (or prioritise) UTXOs with 1-conf.

Overrides ​

BaseWallet.signTransaction

sendTransaction() ​

ts
sendTransaction(txTemplate): Promise<Uint8Array<ArrayBufferLike>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:386

Select UTXOs from this wallet to meet the amounts required and broadcast it to the Blockchain.

Parameters ​
ParameterType
txTemplatePartial<TransactionTemplate>
Returns ​

Promise<Uint8Array<ArrayBufferLike>>

Remarks ​

TODO: We are going to want to return the Transaction that was compiled.

Overrides ​

BaseWallet.sendTransaction

instantiateTemplate() ​

ts
instantiateTemplate<T>(
   template, 
   instantiationData, 
   scope): Promise<TemplateInstance<T>>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:400

Type Parameters ​
Type ParameterDefault type
T extends objectobject
Parameters ​
ParameterTypeDefault value
templateTundefined
instantiationDataCompilationDataundefined
scopestring''
Returns ​

Promise<TemplateInstance<T>>

getId() ​

ts
getId(): Promise<string>;

Defined in: packages/templates-dev/src/wallet/base-wallet.ts:77

Get the ID for this wallet.

Returns ​

Promise<string>

Inherited from ​

BaseWallet.getId

getEncryptionKey() ​

ts
getEncryptionKey(): Promise<string>;

Defined in: packages/templates-dev/src/wallet/base-wallet.ts:86

Get the encryption key to use for this wallet.

Returns ​

Promise<string>

Inherited from ​

BaseWallet.getEncryptionKey

getSigningKey() ​

ts
getSigningKey(): Promise<string>;

Defined in: packages/templates-dev/src/wallet/base-wallet.ts:95

Get the signing key to use for this wallet.

Returns ​

Promise<string>

Inherited from ​

BaseWallet.getSigningKey

getUnspentTokens() ​

ts
getUnspentTokens(): Promise<BlockchainUTXOs>;

Defined in: packages/templates-dev/src/wallet/base-wallet.ts:144

Fetch all unspents tokens from the blockchain belonging to this wallet.

Returns ​

Promise<BlockchainUTXOs>

Inherited from ​

BaseWallet.getUnspentTokens

on() ​

ts
on<K>(
   type, 
   listener, 
   debounceMilliseconds?): OffCallback;

Defined in: packages/core/dist/primitives.d.ts:593

Registers a listener for the specified event type.

Type Parameters ​
Type ParameterDescription
K extends keyof WalletEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to listen for
listenerListener<WalletEvents[K]>The function to call when the event is emitted
debounceMilliseconds?numberOptional debounce time in milliseconds
Returns ​

OffCallback

A callback function that can be used to unregister this listener

Example ​
typescript
const off = emitter.on('data-change', (data) => {
  console.log('Data updated:', data);
}, 300); // Debounce for 300ms

// Later, unregister the listener
off();
Inherited from ​

BaseWallet.on

once() ​

ts
once<K>(
   type, 
   listener, 
   debounceMilliseconds?): OffCallback;

Defined in: packages/core/dist/primitives.d.ts:611

Registers a one-time listener for the specified event type. The listener will be automatically removed after being called once.

Type Parameters ​
Type ParameterDescription
K extends keyof WalletEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to listen for
listenerListener<WalletEvents[K]>The function to call when the event is emitted
debounceMilliseconds?numberOptional debounce time in milliseconds
Returns ​

OffCallback

A callback function that can be used to unregister this listener before it fires

Example ​
typescript
emitter.once('user-login', ({ userId }) => {
  console.log(`Welcome ${userId}! This will only show once.`);
});
Inherited from ​

BaseWallet.once

off() ​

ts
off<K>(type, listener): void;

Defined in: packages/core/dist/primitives.d.ts:626

Removes a specific listener for the given event type.

Type Parameters ​
Type ParameterDescription
K extends keyof WalletEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to remove the listener from
listenerListener<WalletEvents[K]>The listener function to remove
Returns ​

void

Example ​
typescript
const handler = (data) => console.log(data);
emitter.on('test', handler);
emitter.off('test', handler); // Removes the listener
Inherited from ​

BaseWallet.off

emit() ​

ts
emit<K>(type, payload): boolean;

Defined in: packages/core/dist/primitives.d.ts:648

Emits an event of the specified type with the given payload. All registered listeners for this event type will be called.

Type Parameters ​
Type ParameterDescription
K extends keyof WalletEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to emit
payloadWalletEvents[K]The data to pass to all listeners
Returns ​

boolean

true if there were listeners for this event, false otherwise

Example ​
typescript
const hasListeners = emitter.emit('user-login', {
  userId: '123',
  timestamp: Date.now()
});

if (!hasListeners) {
  console.log('No one was listening for user-login events');
}
Inherited from ​

BaseWallet.emit

removeAllListeners() ​

ts
removeAllListeners(): void;

Defined in: packages/core/dist/primitives.d.ts:659

Removes all listeners for all events. This effectively resets the EventEmitter to its initial state.

Returns ​

void

Example ​
typescript
emitter.removeAllListeners();
// All previously registered listeners are now gone
Inherited from ​

BaseWallet.removeAllListeners

waitFor() ​

ts
waitFor<K>(
   type, 
   predicate, 
   timeoutMs?): Promise<WalletEvents[K]>;

Defined in: packages/core/dist/primitives.d.ts:685

Returns a Promise that resolves when an event of the specified type is emitted and matches the given predicate.

Type Parameters ​
Type ParameterDescription
K extends keyof WalletEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to wait for
predicate(payload) => booleanA function that determines if the event payload matches what we're waiting for
timeoutMs?numberOptional timeout in milliseconds. If specified, the promise will reject after this time
Returns ​

Promise<WalletEvents[K]>

A Promise that resolves with the event payload when the condition is met

Throws ​

If the timeout is reached before a matching event occurs

Example ​
typescript
try {
  const loginData = await emitter.waitFor(
    'user-login',
    ({ userId }) => userId === 'admin',
    5000 // Wait up to 5 seconds
  );
  console.log('Admin logged in:', loginData);
} catch (error) {
  console.log('Timeout: Admin never logged in');
}
Inherited from ​

BaseWallet.waitFor

Properties ​

PropertyModifierTypeDefault valueOverridesDefined in
blockchainreadonlyWalletBlockchainundefinedBaseWallet.blockchainpackages/templates-dev/src/wallet/wallet-p2pkh.ts:126
genesisDatareadonlyWalletP2PKHGenesisDataundefinedBaseWallet.genesisDatapackages/templates-dev/src/wallet/wallet-p2pkh.ts:129
privateKeyreadonlyPrivateKeyundefined-packages/templates-dev/src/wallet/wallet-p2pkh.ts:132
isStartedpublicbooleanfalse-packages/templates-dev/src/wallet/wallet-p2pkh.ts:135
statuspublicstring | nullnull-packages/templates-dev/src/wallet/wallet-p2pkh.ts:136

useWalletP2PKH() ​

ts
function useWalletP2PKH<T>(dependencies, genesisData): Promise<T>;

Defined in: packages/templates-dev/src/wallet/wallet-p2pkh.ts:485

Type Parameters ​

Type ParameterDefault type
T extends WalletP2PKHWalletP2PKH

Parameters ​

ParameterType
dependenciesWalletDependencies
genesisDataWalletP2PKHGenesisData

Returns ​

Promise<T>