Skip to content
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

[WIP] Add methods in SnapSigner to request permission before pay #99

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nitro-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"assert": "^2.0.0",
"debug": "^4.3.4",
"ethers": "^5.7.2",
"it-pipe": "^2.0.5",
"it-pipe": "^3.0.1",
"level": "^8.0.0",
"lodash": "^4.17.21",
"promjs": "^0.4.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/nitro-client/src/client/engine/store/durablestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,10 @@ export class DurableStore implements Store {
}
}

setVoucherInfo(channelId: Destination, v: VoucherInfo): void {
async setVoucherInfo(channelId: Destination, v: VoucherInfo): Promise<void> {
const vJSON = Buffer.from(JSONbigNative.stringify(v));

this.vouchers!.put(channelId.string(), vJSON);
await this.vouchers!.put(channelId.string(), vJSON);
}

async getVoucherInfo(channelId: Destination): Promise<[VoucherInfo | undefined, boolean]> {
Expand Down
8 changes: 4 additions & 4 deletions packages/nitro-client/src/payments/voucher-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Voucher, VoucherInfo } from './vouchers';
// VoucherStore is an interface for storing voucher information that the voucher manager expects.
// To avoid import cycles, this interface is defined in the payments package, but implemented in the store package.
export interface VoucherStore {
setVoucherInfo (channelId: Destination, v: VoucherInfo): void
setVoucherInfo (channelId: Destination, v: VoucherInfo): void | Promise<void>

getVoucherInfo (channelId: Destination): [VoucherInfo | undefined, boolean] | Promise<[VoucherInfo | undefined, boolean]>

Expand Down Expand Up @@ -49,7 +49,7 @@ export class VoucherManager {
throw new Error('Channel already registered');
}

this.store.setVoucherInfo(channelId, data);
await this.store.setVoucherInfo(channelId, data);
}

// Remove deletes the channel's status
Expand Down Expand Up @@ -85,7 +85,7 @@ export class VoucherManager {

await voucher.sign(signer);

this.store.setVoucherInfo(channelId, vInfo);
await this.store.setVoucherInfo(channelId, vInfo);

return voucher;
}
Expand Down Expand Up @@ -120,7 +120,7 @@ export class VoucherManager {

vInfo.largestVoucher = voucher;

this.store.setVoucherInfo(voucher.channelId, vInfo);
await this.store.setVoucherInfo(voucher.channelId, vInfo);
return received;
}

Expand Down
38 changes: 37 additions & 1 deletion packages/nitro-client/src/utils/signers/snap-signer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Signature, ethers } from 'ethers';

import { NitroSigner } from '@cerc-io/nitro-util';
import { NitroSigner, JSONbigNative } from '@cerc-io/nitro-util';
import { RateInfo, RateType } from '../types';

export class SnapSigner implements NitroSigner {
private provider: ethers.providers.ExternalProvider;
Expand Down Expand Up @@ -52,4 +53,39 @@ export class SnapSigner implements NitroSigner {

return signature as Signature;
}

async updateRates(endpoint: string, rates: RateInfo[]): Promise<void> {
await (this.provider.request as any)({
method: 'wallet_invokeSnap',
params: {
snapId: this.snapOrigin,
request: {
method: 'updateRates',
params: {
endpoint,
rates: JSONbigNative.stringify(rates),
},
},
},
});
}

async requestPermission(endpoint: string, rateType: RateType, name: string): Promise<boolean> {
const result = await (this.provider.request as any)({
method: 'wallet_invokeSnap',
params: {
snapId: this.snapOrigin,
request: {
method: 'requestPermission',
params: {
endpoint,
name,
rateType,
},
},
},
});

return Boolean(result);
}
}
11 changes: 11 additions & 0 deletions packages/nitro-client/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ export interface Actor {
privateKey: string;
chainPrivateKey: string;
}

export enum RateType {
Query = 'QUERY',
Mutation = 'MUTATION',
}

export interface RateInfo {
type: RateType;
name: string;
amount: bigint;
}
1 change: 0 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"cli": "DEBUG=ts-nitro:* ts-node src/index.ts",
"lint": "eslint .",
"test:e2e": "mocha test-e2e/**/*.test.ts --bail",
"test:deploy-contracts": "DEBUG=ts-nitro:* yarn ts-node test-e2e/scripts/deploy-contracts.ts",
"chain": "hardhat node"
},
"dependencies": {
Expand Down