Skip to content

Commit

Permalink
add NDKWallet receiveToken
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Oct 28, 2024
1 parent 0a7e4d8 commit 2c1e9ad
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 141 deletions.
9 changes: 8 additions & 1 deletion docs/wallet/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ wallet.mints = choosenMints;
wallet.publishReplaceable();
```

##
## Receiving ecash
To receive ecash just call the `receiveToken` method on the wallet.

```ts
const tokenEvent = await wallet.receiveToken(token);
```

This will swap the tokens in the right mint and add them to the wallet. Note that if the mint of this token is not one of the ones in the wallet you will need to move them to the mint you want manually.
6 changes: 6 additions & 0 deletions ndk-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @nostr-dev-kit/ndk-cache-redis

## 0.3.5

### Patch Changes

- receive cashu

## 0.3.4

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions ndk-wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-wallet",
"version": "0.3.4",
"version": "0.3.5",
"description": "NDK Wallet",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://github.com/nostr-dev-kit/ndk",
"dependencies": {
"@cashu/cashu-ts": "1.1.0",
"@cashu/cashu-ts": "1.2.1",
"@getalby/sdk": "^3.6.1",
"@nostr-dev-kit/ndk": "workspace:*",
"debug": "^4.3.4",
Expand Down
34 changes: 28 additions & 6 deletions ndk-wallet/src/cashu/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import createDebug from "debug";
import type { MintUrl } from "./mint/utils.js";
import { NDKCashuPay } from "./pay.js";
import type { Proof } from "@cashu/cashu-ts";
import { CashuMint, CashuWallet } from "@cashu/cashu-ts";
import { CashuMint, CashuWallet, getDecodedToken } from "@cashu/cashu-ts";
import { NDKWalletChange } from "./history.js";
import { checkTokenProofs } from "./validate.js";
import { NDKWallet, NDKWalletBalance, NDKWalletEvents, NDKWalletStatus } from "../wallet/index.js";
Expand Down Expand Up @@ -309,6 +309,26 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents> implements NDK
return deposit;
}

/**
* Receives a token and adds it to the wallet
* @param token
* @returns the token event that was created
*/
public async receiveToken(token: string) {
const mint = getDecodedToken(token).token[0].mint
const wallet = new CashuWallet(new CashuMint(mint));
const proofs = await wallet.receive(token);
await this.saveProofs(proofs, mint);

const tokenEvent = new NDKCashuToken(this.event.ndk);
tokenEvent.proofs = proofs;
tokenEvent.mint = mint;
await tokenEvent.publish(this.relaySet);
this.addToken(tokenEvent);

return tokenEvent;
}

public async addHistoryItem(
direction: "in" | "out",
amount: number,
Expand Down Expand Up @@ -405,14 +425,16 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents> implements NDK
console.error("failed to publish token", e, tokenEvent.rawEvent());
});

const historyEvent = new NDKWalletChange(this.event.ndk);
historyEvent.tag(this.event);

if (nutzap) {
const historyEvent = new NDKWalletChange(this.event.ndk);
historyEvent.addRedeemedNutzap(nutzap);
historyEvent.tag(this.event);
historyEvent.tag(tokenEvent, NDKWalletChange.MARKERS.CREATED);
await historyEvent.sign();
historyEvent.publish(this.relaySet);
}

historyEvent.tag(tokenEvent, NDKWalletChange.MARKERS.CREATED);
await historyEvent.sign();
historyEvent.publish(this.relaySet);

return tokenEvent;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@changesets/cli": "^2.22.0",
"@nostr-dev-kit/eslint-config-custom": "workspace:*",
"@nostr-dev-kit/tsconfig": "workspace:*",
"eslint": "^9.12.0",
"eslint": "^9.13.0",
"mermaid": "^10.9.1",
"prettier": "^3.0.3",
"turbo": "^1.10.14",
Expand Down
Loading

0 comments on commit 2c1e9ad

Please sign in to comment.