-
Notifications
You must be signed in to change notification settings - Fork 12
feat(wallet-daemon-signer): add stealth transfer support #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
metalaureate
wants to merge
1
commit into
tari-project:main
Choose a base branch
from
metalaureate:feat/wallet-daemon-stealth-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
packages/ootle-wallet-daemon-signer/src/daemon-stealth-factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Copyright 2024 The Tari Project | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| import type { | ||
| AccountsCreateStealthTransferStatementResponse, | ||
| ComponentAddressOrName, | ||
| OotleAddress, | ||
| ResourceAddress, | ||
| StealthTransferStatement as BindingsStealthTransferStatement, | ||
| TransferOutput, | ||
| } from "@tari-project/ootle-ts-bindings"; | ||
| import type { WalletDaemonClient } from "@tari-project/wallet_jrpc_client"; | ||
| import type { StealthOutputStatementFactory, StealthTransferStatement } from "@tari-project/ootle"; | ||
|
|
||
| export interface DaemonStealthFactoryOptions { | ||
| /** The wallet daemon JRPC client (obtain via `WalletDaemonSigner.getClient()`). */ | ||
| client: WalletDaemonClient; | ||
| /** The sender account that owns the stealth UTXOs to spend. */ | ||
| senderAccount: ComponentAddressOrName; | ||
| /** The resource to transfer (e.g. the Tari token). */ | ||
| resourceAddress: ResourceAddress; | ||
| /** The recipient's full Ootle address (bech32m-encoded, includes both owner and view key). */ | ||
| recipientAddress: OotleAddress; | ||
| } | ||
|
|
||
| /** | ||
| * A `StealthOutputStatementFactory` backed by a wallet daemon's | ||
| * `accounts.create_stealth_transfer_statement` JRPC endpoint. | ||
| * | ||
| * The wallet daemon handles all cryptographic operations (DH-KDF, Pedersen | ||
| * commitments, range proofs, balance proofs) server-side. No WASM crypto | ||
| * is required on the client. | ||
| * | ||
| * ## Usage | ||
| * | ||
| * ```ts | ||
| * const signer = await WalletDaemonSigner.connect({ url: "http://localhost:18103" }); | ||
| * const factory = new DaemonStealthFactory({ | ||
| * client: signer.getClient(), | ||
| * senderAccount: { Name: "default" }, | ||
| * resourceAddress: TARI_TOKEN, | ||
| * recipientAddress: "ootle1...", | ||
| * }); | ||
| * | ||
| * const spec = await new StealthTransfer(network, factory) | ||
| * .from(sourceAccount, resourceAddress) | ||
| * .to(recipientPublicKeyHex, 1_000_000n) | ||
| * .feeFrom(feeAccount, 1000n) | ||
| * .build(); | ||
| * ``` | ||
| * | ||
| * ## Type mapping | ||
| * | ||
| * The wallet daemon returns statements in the canonical bindings format | ||
| * (`@tari-project/ootle-ts-bindings` `StealthTransferStatement`), which uses | ||
| * `inputs_statement` / `outputs_statement` / `balance_proof`. | ||
| * | ||
| * The `@tari-project/ootle` package currently defines a simplified | ||
| * `StealthTransferStatement` with `outputs` / `balanceProof`. These types | ||
| * are structurally incompatible. This factory returns the **bindings format** | ||
| * (cast to satisfy the interface) because that is the format the on-chain | ||
| * `deposit_stealth` method expects when deserializing the JSON payload. | ||
| * | ||
| * A follow-up PR should align the `@tari-project/ootle` types with the | ||
| * bindings to eliminate this cast. | ||
| */ | ||
| export class DaemonStealthFactory implements StealthOutputStatementFactory { | ||
| private readonly client: WalletDaemonClient; | ||
| private readonly senderAccount: ComponentAddressOrName; | ||
| private readonly resourceAddress: ResourceAddress; | ||
| private readonly recipientAddress: OotleAddress; | ||
|
|
||
| constructor(options: DaemonStealthFactoryOptions) { | ||
| this.client = options.client; | ||
| this.senderAccount = options.senderAccount; | ||
| this.resourceAddress = options.resourceAddress; | ||
| this.recipientAddress = options.recipientAddress; | ||
| } | ||
|
|
||
| /** | ||
| * Generates a stealth transfer statement by delegating to the wallet daemon. | ||
| * | ||
| * @param _recipientPublicKeyHex - Ignored in the daemon flow. The recipient is | ||
| * identified by the `recipientAddress` provided at construction time (which | ||
| * embeds the public key). This parameter is accepted to satisfy the | ||
| * `StealthOutputStatementFactory` interface. | ||
| * @param amounts - Amount(s) to send in each stealth output. | ||
| */ | ||
| public async generateOutputsStatement( | ||
| _recipientPublicKeyHex: string, | ||
| amounts: bigint[], | ||
| ): Promise<StealthTransferStatement> { | ||
| const outputs: TransferOutput[] = amounts.map((amount) => ({ | ||
| address: this.recipientAddress, | ||
| revealed_amount: 0, | ||
| blinded_amount: amount, | ||
| memo: null, | ||
| pay_to: "StealthPublicKey" as const, | ||
| })); | ||
|
|
||
| const response = | ||
| await this.client.sendRequest<AccountsCreateStealthTransferStatementResponse>( | ||
| "accounts.create_stealth_transfer_statement", | ||
| { | ||
| requests: [ | ||
| { | ||
| sender_account: this.senderAccount, | ||
| resource_address: this.resourceAddress, | ||
| input_selection: { Selection: "PreferConfidential" }, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| outputs, | ||
| }, | ||
| ], | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.statements || response.statements.length === 0) { | ||
| throw new Error("Wallet daemon returned no stealth transfer statements"); | ||
| } | ||
|
|
||
| // The daemon returns the canonical bindings format. We cast to the tari.js | ||
| // StealthTransferStatement type. See class-level JSDoc for the type mismatch | ||
| // discussion — the bindings format is what deposit_stealth actually expects. | ||
| return response.statements[0] as unknown as StealthTransferStatement; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_recipientPublicKeyHexparameter is ignored in favor of therecipientAddressprovided during construction. This creates a risk where a caller of theStealthTransferbuilder might specify a different recipient via.to(key, amount)than the one this factory is configured for, leading to funds being sent to the wrong destination without any warning or error.Since the
StealthOutputStatementFactoryinterface only provides a hex public key, but the wallet daemon requires a fullOotleAddress(which includes the view key), this factory is effectively pinned to a single recipient. You should consider adding a check to verify that_recipientPublicKeyHexmatches the public key part ofthis.recipientAddress(if possible to decode) or at least document this limitation more explicitly in the method body to prevent accidental misuse.