Skip to content

Commit

Permalink
build: move ExampleNodeWallet to own entrypoint
Browse files Browse the repository at this point in the history
No need to pollute the global API or bundle size with this.
  • Loading branch information
chadoh committed Mar 5, 2024
1 parent 523812e commit 561eee3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
27 changes: 27 additions & 0 deletions src/example_node_wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Keypair, TransactionBuilder, hash } from '.'

/**
* For use with {@link ContractClient} and {@link AssembledTransaction}.
* Implements `signTransaction` and `signAuthEntry` with signatures expected by
* those classes. This is useful for testing and maybe some simple Node
* applications. Feel free to use this as a starting point for your own
* Wallet/TransactionSigner implementation.
*/
export class ExampleNodeWallet {
constructor(
private keypair: Keypair,
private networkPassphrase: string,
) {}

signTransaction = async (tx: string) => {
const t = TransactionBuilder.fromXDR(tx, this.networkPassphrase);
t.sign(this.keypair);
return t.toXDR();
}

signAuthEntry = async (entryXdr: string): Promise<string> => {
return this.keypair
.sign(hash(Buffer.from(entryXdr, "base64")))
.toString('base64')
}
}
30 changes: 1 addition & 29 deletions src/soroban/contract_client.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
import { AssembledTransaction } from '.'
import { ContractSpec, Keypair, TransactionBuilder, hash, xdr } from '..'
import { ContractSpec, xdr } from '..'

export type XDR_BASE64 = string;

/**
* An example Wallet implementation that can be used for testing and
* potentially some simple Node applications, which provides `signTransaction`
* and `signAuthEntry` with the function signatures expected by ContractClient
* & AssembledTransaction.
*
* Feel free to use this as a starting point for your own
* Wallet/TransactionSigner implementation.
*/
export class ExampleNodeWallet {
constructor(
private keypair: Keypair,
private networkPassphrase: string,
) {}

signTransaction = async (tx: string) => {
const t = TransactionBuilder.fromXDR(tx, this.networkPassphrase);
t.sign(this.keypair);
return t.toXDR();
}

signAuthEntry = async (entryXdr: string): Promise<string> => {
return this.keypair
.sign(hash(Buffer.from(entryXdr, "base64")))
.toString('base64')
}
}

export type ContractClientOptions = {
/**
* The account to use for signing transactions. If not provided, a null
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { spawnSync } = require('node:child_process')
const { ContractSpec, Keypair, SorobanRpc } = require('../../..')
const { ContractSpec, Keypair } = require('../../..')
const { ExampleNodeWallet } = require('../../../lib/example_node_wallet')

const contracts = {
customTypes: {
Expand Down Expand Up @@ -53,7 +54,7 @@ async function clientFor(contract, { keypair = generateFundedKeypair(), contract
}

keypair = await keypair // eslint-disable-line no-param-reassign
const wallet = new SorobanRpc.ExampleNodeWallet(keypair, networkPassphrase)
const wallet = new ExampleNodeWallet(keypair, networkPassphrase)

const spec = new ContractSpec(contracts[contract].xdr)

Expand Down

0 comments on commit 561eee3

Please sign in to comment.