@tempoxyz/id 0.0.0-pr-499.sha-3497972 → 0.0.0-pr-494.sha-e349a84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mpp/index.d.ts +109 -0
- package/dist/mpp/index.d.ts.map +1 -0
- package/dist/mpp/index.js +159 -0
- package/dist/mpp/index.js.map +1 -0
- package/dist/plugins/dialog.d.ts +0 -6
- package/dist/plugins/dialog.d.ts.map +1 -1
- package/dist/plugins/dialog.js +0 -1
- package/dist/plugins/dialog.js.map +1 -1
- package/package.json +10 -1
- package/src/mpp/index.ts +249 -0
- package/src/plugins/dialog.ts +0 -7
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry for MPP: `mpp`, a plugin that adds `tempoId.mpp`, which pays the 402 challenges of
|
|
3
|
+
* Machine Payments Protocol (MPP) endpoints with the session's access key, through mppx.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Id } from '@tempoxyz/id'
|
|
8
|
+
* import { mpp } from '@tempoxyz/id/mpp'
|
|
9
|
+
* import { dialog } from '@tempoxyz/id/plugins'
|
|
10
|
+
*
|
|
11
|
+
* const tempoId = Id.create({ accessKey, plugins: [dialog(), mpp()] })
|
|
12
|
+
*
|
|
13
|
+
* await tempoId.dialog.signIn()
|
|
14
|
+
* const response = await tempoId.mpp.fetch('https://api.example.com/fortune')
|
|
15
|
+
* ```
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
import { tempo } from 'mppx/client';
|
|
19
|
+
import * as Plugin from '../Plugin.js';
|
|
20
|
+
import type * as Session from '../Session.js';
|
|
21
|
+
/** A charge costs more than `maxAmount`, so the payer signs nothing. */
|
|
22
|
+
export declare class MaxAmountError extends Error {
|
|
23
|
+
readonly name = "mpp.MaxAmountError";
|
|
24
|
+
constructor(options: MaxAmountError.Options);
|
|
25
|
+
}
|
|
26
|
+
export declare namespace MaxAmountError {
|
|
27
|
+
type Options = {
|
|
28
|
+
/** The charge's amount, in the units of its token. */
|
|
29
|
+
amount: string;
|
|
30
|
+
/** The most that a charge may cost, in the units of its token. */
|
|
31
|
+
maxAmount: string;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns the plugin of MPP payments, which adds `tempoId.mpp`. `tempoId.mpp.fetch` is `fetch`
|
|
36
|
+
* that pays each charge an endpoint challenges for, and `tempoId.mpp.session()` returns a session
|
|
37
|
+
* manager, which pays each request of a channel with a voucher. The access key of the instance's
|
|
38
|
+
* session signs every payment, so no payment reaches the account's owner. `tempoId.mpp({ session })`
|
|
39
|
+
* pays with a fixed session's access key, such as one that `tempoId.backend.get` returns.
|
|
40
|
+
*
|
|
41
|
+
* The plugin reads the chains and transports of the `viem` plugin, or of a `viem` plugin of its
|
|
42
|
+
* own without one in `plugins`. `mppx` is an optional peer dependency of `@tempoxyz/id`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const tempoId = Id.create({ accessKey, plugins: [dialog(), mpp()] })
|
|
47
|
+
*
|
|
48
|
+
* const response = await tempoId.mpp.fetch('https://api.example.com/fortune')
|
|
49
|
+
*
|
|
50
|
+
* const session = tempoId.mpp.session()
|
|
51
|
+
* await session.fetch('https://api.example.com/content')
|
|
52
|
+
* await session.close()
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function mpp(options?: mpp.Options): Plugin.Plugin<"mpp", unknown, {
|
|
56
|
+
mpp: mpp.Namespace;
|
|
57
|
+
}>;
|
|
58
|
+
export declare namespace mpp {
|
|
59
|
+
type Options = Settings & {
|
|
60
|
+
/** Sends each request to an endpoint, and its paid retry. Defaults to the global `fetch`. */
|
|
61
|
+
fetch?: typeof globalThis.fetch | undefined;
|
|
62
|
+
};
|
|
63
|
+
/** The settings of the payments, which the plugin's options set and a call overrides. */
|
|
64
|
+
type Settings = {
|
|
65
|
+
/**
|
|
66
|
+
* The most that a charge may cost, in the units of its token, such as `'0.1'`. A larger charge
|
|
67
|
+
* throws `mpp.MaxAmountError` before the access key signs. Defaults to no maximum, within the
|
|
68
|
+
* access key's token limits.
|
|
69
|
+
*/
|
|
70
|
+
maxAmount?: string | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* The most that a session's channel holds, in the units of its token, such as `'0.05'`. It
|
|
73
|
+
* caps the deposit that an endpoint suggests and each top-up. Defaults to the deposit that the
|
|
74
|
+
* endpoint suggests, within the access key's token limits.
|
|
75
|
+
*/
|
|
76
|
+
maxDeposit?: string | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* Who broadcasts a charge's transaction: the endpoint (`'pull'`) or the app (`'push'`).
|
|
79
|
+
* Defaults to `'pull'`.
|
|
80
|
+
*/
|
|
81
|
+
mode?: 'pull' | 'push' | undefined;
|
|
82
|
+
};
|
|
83
|
+
/** The overrides of a payer, for one call. */
|
|
84
|
+
type Overrides = Settings & {
|
|
85
|
+
/**
|
|
86
|
+
* A fixed session, such as one that `tempoId.backend.get` returns, whose access key pays.
|
|
87
|
+
* Defaults to the instance's session, read at each payment.
|
|
88
|
+
*/
|
|
89
|
+
session?: Session.Session | undefined;
|
|
90
|
+
};
|
|
91
|
+
/** The payments of one access key. */
|
|
92
|
+
type Payer = {
|
|
93
|
+
/**
|
|
94
|
+
* Sends a request, and pays a 402 charge challenge with the access key, then returns the
|
|
95
|
+
* endpoint's response to the paid retry. Throws `Id.SignedOutError` without a session, and
|
|
96
|
+
* `Id.KeyRequiredError` for a session without an access key.
|
|
97
|
+
*/
|
|
98
|
+
fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
99
|
+
/**
|
|
100
|
+
* Returns a session manager of mppx, which opens a channel on the first request of a session
|
|
101
|
+
* endpoint, pays each request with a voucher of the access key, and closes the channel at
|
|
102
|
+
* `close()`. The manager holds the channel in memory, so close it before it is discarded.
|
|
103
|
+
*/
|
|
104
|
+
session: (parameters?: Omit<Parameters<typeof tempo.session.manager>[0], 'account' | 'client' | 'getClient'>) => ReturnType<typeof tempo.session.manager>;
|
|
105
|
+
};
|
|
106
|
+
/** `tempoId.mpp`: the payer of the instance's session, which a call fixes to another session. */
|
|
107
|
+
type Namespace = Payer & ((overrides?: Overrides) => Payer);
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mpp/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAQ,KAAK,EAAE,MAAM,aAAa,CAAA;AAMzC,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AAEtC,OAAO,KAAK,KAAK,OAAO,MAAM,eAAe,CAAA;AAE7C,wEAAwE;AACxE,qBAAa,cAAe,SAAQ,KAAK;IACvC,SAAkB,IAAI,wBAAuB;IAE7C,YAAY,OAAO,EAAE,cAAc,CAAC,OAAO,EAI1C;CACF;AAED,MAAM,CAAC,OAAO,WAAW,cAAc,CAAC;IACtC,KAAK,OAAO,GAAG;QACb,sDAAsD;QACtD,MAAM,EAAE,MAAM,CAAA;QACd,kEAAkE;QAClE,SAAS,EAAE,MAAM,CAAA;KAClB,CAAA;CACF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,GAAG,CAAC,OAAO,GAAE,GAAG,CAAC,OAAY;SAyED,GAAG,CAAC,SAAS;GAQxD;AAED,MAAM,CAAC,OAAO,WAAW,GAAG,CAAC;IAC3B,KAAK,OAAO,GAAG,QAAQ,GAAG;QACxB,6FAA6F;QAC7F,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,GAAG,SAAS,CAAA;KAC5C,CAAA;IAED,yFAAyF;IACzF,KAAK,QAAQ,GAAG;QACd;;;;WAIG;QACH,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC9B;;;;WAIG;QACH,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC/B;;;WAGG;QACH,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;KACnC,CAAA;IAED,8CAA8C;IAC9C,KAAK,SAAS,GAAG,QAAQ,GAAG;QAC1B;;;WAGG;QACH,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,GAAG,SAAS,CAAA;KACtC,CAAA;IAED,sCAAsC;IACtC,KAAK,KAAK,GAAG;QACX;;;;WAIG;QACH,KAAK,EAAE,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;QAC1E;;;;WAIG;QACH,OAAO,EAAE,CACP,UAAU,CAAC,EAAE,IAAI,CACf,UAAU,CAAC,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAC3C,SAAS,GAAG,QAAQ,GAAG,WAAW,CACnC,KACE,UAAU,CAAC,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;KAC9C,CAAA;IAED,iGAAiG;IACjG,KAAK,SAAS,GAAG,KAAK,GAAG,CAAC,CAAC,SAAS,CAAC,EAAE,SAAS,KAAK,KAAK,CAAC,CAAA;CAC5D"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry for MPP: `mpp`, a plugin that adds `tempoId.mpp`, which pays the 402 challenges of
|
|
3
|
+
* Machine Payments Protocol (MPP) endpoints with the session's access key, through mppx.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Id } from '@tempoxyz/id'
|
|
8
|
+
* import { mpp } from '@tempoxyz/id/mpp'
|
|
9
|
+
* import { dialog } from '@tempoxyz/id/plugins'
|
|
10
|
+
*
|
|
11
|
+
* const tempoId = Id.create({ accessKey, plugins: [dialog(), mpp()] })
|
|
12
|
+
*
|
|
13
|
+
* await tempoId.dialog.signIn()
|
|
14
|
+
* const response = await tempoId.mpp.fetch('https://api.example.com/fortune')
|
|
15
|
+
* ```
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
import { Mppx, tempo } from 'mppx/client';
|
|
19
|
+
import { createClient, formatUnits, parseUnits } from 'viem';
|
|
20
|
+
import * as AccessKey from '../AccessKey.js';
|
|
21
|
+
import * as Id from '../Id.js';
|
|
22
|
+
import * as Plugin from '../Plugin.js';
|
|
23
|
+
import { settings, viem } from '../plugins/viem.js';
|
|
24
|
+
/** A charge costs more than `maxAmount`, so the payer signs nothing. */
|
|
25
|
+
export class MaxAmountError extends Error {
|
|
26
|
+
name = 'mpp.MaxAmountError';
|
|
27
|
+
constructor(options) {
|
|
28
|
+
super(`The charge of ${options.amount} exceeds the \`maxAmount\` of ${options.maxAmount}. Raise \`maxAmount\` to pay it.`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns the plugin of MPP payments, which adds `tempoId.mpp`. `tempoId.mpp.fetch` is `fetch`
|
|
33
|
+
* that pays each charge an endpoint challenges for, and `tempoId.mpp.session()` returns a session
|
|
34
|
+
* manager, which pays each request of a channel with a voucher. The access key of the instance's
|
|
35
|
+
* session signs every payment, so no payment reaches the account's owner. `tempoId.mpp({ session })`
|
|
36
|
+
* pays with a fixed session's access key, such as one that `tempoId.backend.get` returns.
|
|
37
|
+
*
|
|
38
|
+
* The plugin reads the chains and transports of the `viem` plugin, or of a `viem` plugin of its
|
|
39
|
+
* own without one in `plugins`. `mppx` is an optional peer dependency of `@tempoxyz/id`.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const tempoId = Id.create({ accessKey, plugins: [dialog(), mpp()] })
|
|
44
|
+
*
|
|
45
|
+
* const response = await tempoId.mpp.fetch('https://api.example.com/fortune')
|
|
46
|
+
*
|
|
47
|
+
* const session = tempoId.mpp.session()
|
|
48
|
+
* await session.fetch('https://api.example.com/content')
|
|
49
|
+
* await session.close()
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function mpp(options = {}) {
|
|
53
|
+
const { fetch } = options;
|
|
54
|
+
// The context of each instance, by its configuration, since an app can pass one plugin to
|
|
55
|
+
// several instances.
|
|
56
|
+
const contexts = new WeakMap();
|
|
57
|
+
return Plugin.from({
|
|
58
|
+
extend(instance) {
|
|
59
|
+
const context = contexts.get(instance.config);
|
|
60
|
+
const held = instance.viem;
|
|
61
|
+
const namespace = held ?? viem().actions(context);
|
|
62
|
+
const { chains, transport } = namespace[settings];
|
|
63
|
+
// A client without an account reads the chain and broadcasts. The access key signs.
|
|
64
|
+
const clients = new Map();
|
|
65
|
+
// The payers of each access key, one for each setting of the charges.
|
|
66
|
+
const payers = new WeakMap();
|
|
67
|
+
/** Returns the client of the chain of `chainId`, one of the plugin's chains. */
|
|
68
|
+
function getClient(parameters = {}) {
|
|
69
|
+
const chainId = parameters.chainId ?? instance.config.chainId;
|
|
70
|
+
const existing = clients.get(chainId);
|
|
71
|
+
if (existing)
|
|
72
|
+
return existing;
|
|
73
|
+
const chain = chains.find((entry) => entry.id === chainId);
|
|
74
|
+
if (!chain)
|
|
75
|
+
throw new Error(`Invalid chain: ${chainId} is not one of the viem plugin's \`chains\`. Add its definition to \`chains\`.`);
|
|
76
|
+
const client = createClient({ chain, transport: transport(chainId) });
|
|
77
|
+
clients.set(chainId, client);
|
|
78
|
+
return client;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the payer of the access key of `overrides.session`, or of the instance's session,
|
|
82
|
+
* with the plugin's settings under `overrides`.
|
|
83
|
+
*/
|
|
84
|
+
function payer(overrides) {
|
|
85
|
+
const current = overrides.session ?? context.session.get();
|
|
86
|
+
if (!current)
|
|
87
|
+
throw new Id.SignedOutError();
|
|
88
|
+
if (!current.key)
|
|
89
|
+
throw new Id.KeyRequiredError();
|
|
90
|
+
const maxAmount = overrides.maxAmount ?? options.maxAmount;
|
|
91
|
+
const maxDeposit = overrides.maxDeposit ?? options.maxDeposit;
|
|
92
|
+
const mode = overrides.mode ?? options.mode;
|
|
93
|
+
let byKey = payers.get(current.key);
|
|
94
|
+
if (!byKey)
|
|
95
|
+
payers.set(current.key, (byKey = new Map()));
|
|
96
|
+
const name = [maxAmount, maxDeposit, mode].join('\n');
|
|
97
|
+
const existing = byKey.get(name);
|
|
98
|
+
if (existing)
|
|
99
|
+
return existing;
|
|
100
|
+
const created = create({
|
|
101
|
+
account: AccessKey.toAccount(AccessKey.from(current.key)),
|
|
102
|
+
fetch,
|
|
103
|
+
getClient,
|
|
104
|
+
maxAmount,
|
|
105
|
+
maxDeposit,
|
|
106
|
+
mode,
|
|
107
|
+
});
|
|
108
|
+
byKey.set(name, created);
|
|
109
|
+
return created;
|
|
110
|
+
}
|
|
111
|
+
/** Returns the payer that resolves its access key at each call, as `payer` does. */
|
|
112
|
+
function bind(overrides = {}) {
|
|
113
|
+
return {
|
|
114
|
+
// A missing session or access key rejects the request, as a failed fetch does.
|
|
115
|
+
fetch: async (input, init) => payer(overrides).fetch(input, init),
|
|
116
|
+
session: (parameters) => payer(overrides).session(parameters),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
mpp: Object.assign(bind, bind()),
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
id: 'mpp',
|
|
124
|
+
setup(value) {
|
|
125
|
+
contexts.set(value.config, value);
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/** Creates the payer of `account`. */
|
|
130
|
+
function create(options) {
|
|
131
|
+
const { account, fetch, getClient, maxAmount, maxDeposit, mode } = options;
|
|
132
|
+
const mppx = Mppx.create({
|
|
133
|
+
...(fetch ? { fetch } : {}),
|
|
134
|
+
methods: [tempo.charge({ account, getClient, ...(mode ? { mode } : {}) })],
|
|
135
|
+
// The payer reads each charge's amount before the access key signs. A TIP-20 token has 6
|
|
136
|
+
// decimals.
|
|
137
|
+
onChallenge: (challenge) => {
|
|
138
|
+
if (maxAmount === undefined)
|
|
139
|
+
return Promise.resolve(undefined);
|
|
140
|
+
const amount = BigInt(String(challenge.request.amount ?? 0));
|
|
141
|
+
if (amount > parseUnits(maxAmount, 6))
|
|
142
|
+
throw new MaxAmountError({ amount: formatUnits(amount, 6), maxAmount });
|
|
143
|
+
return Promise.resolve(undefined);
|
|
144
|
+
},
|
|
145
|
+
// The payer pays only through its own `fetch`, so the app's other requests stay unchanged.
|
|
146
|
+
polyfill: false,
|
|
147
|
+
});
|
|
148
|
+
return {
|
|
149
|
+
fetch: (input, init) => mppx.fetch(input, init),
|
|
150
|
+
session: (parameters = {}) => tempo.session.manager({
|
|
151
|
+
...(fetch ? { fetch } : {}),
|
|
152
|
+
...(maxDeposit ? { maxDeposit } : {}),
|
|
153
|
+
...parameters,
|
|
154
|
+
account,
|
|
155
|
+
getClient,
|
|
156
|
+
}),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/mpp/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAEzC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AAE5D,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,UAAU,CAAA;AAC9B,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAGnD,wEAAwE;AACxE,MAAM,OAAO,cAAe,SAAQ,KAAK;IACrB,IAAI,GAAG,oBAAoB,CAAA;IAE7C,YAAY,OAA+B;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4 +1,3 @@
|
|
|
1
|
-
import type * as Funding from '../Funding.js';
|
|
2
1
|
import * as SignIn from '../internal/signIn.js';
|
|
3
2
|
import * as Plugin from '../Plugin.js';
|
|
4
3
|
import type * as Session from '../Session.js';
|
|
@@ -41,11 +40,6 @@ export declare namespace dialog {
|
|
|
41
40
|
accessKey?: SignIn.AccessKeyOption;
|
|
42
41
|
/** A message whose EIP-191 hash the key authorization binds as its `witness`. */
|
|
43
42
|
challenge?: string | undefined;
|
|
44
|
-
/**
|
|
45
|
-
* The amount that the app needs in the user's account. A frame cannot show the funding page,
|
|
46
|
-
* so the dialog continues in a popup when it opens.
|
|
47
|
-
*/
|
|
48
|
-
funding?: Funding.Funding | undefined;
|
|
49
43
|
/**
|
|
50
44
|
* Whether the sign-in shows in a frame or in a popup window. Defaults to the plugin's `popup`.
|
|
51
45
|
* A popup opens only within a click.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog.d.ts","sourceRoot":"","sources":["../../src/plugins/dialog.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"dialog.d.ts","sourceRoot":"","sources":["../../src/plugins/dialog.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAA;AAC/C,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,KAAK,KAAK,OAAO,MAAM,eAAe,CAAA;AAE7C;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAC,OAAO,GAAE,MAAM,CAAC,OAAY;IAsB3C;;;OAGG;oBACW,OAAO,CAAC,OAAO,CAAC;IAC9B,sEAAsE;wBAExD,MAAM,CAAC,MAAM,CAAC,UAAU,GACnC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;YAwBlC;AAED,MAAM,CAAC,OAAO,WAAW,MAAM,CAAC;IAC9B,KAAK,OAAO,GAAG;QACb,+FAA+F;QAC/F,GAAG,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;QACzB,qFAAqF;QACrF,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;QAC3B,sGAAsG;QACtG,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;KAC9B,CAAA;IAED,UAAU,MAAM,CAAC;QACf,KAAK,UAAU,GAAG;YAChB;;;eAGG;YACH,SAAS,CAAC,EAAE,MAAM,CAAC,eAAe,CAAA;YAClC,iFAAiF;YACjF,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;YAC9B;;;eAGG;YACH,IAAI,CAAC,EAAE,QAAQ,GAAG,OAAO,GAAG,SAAS,CAAA;YACrC,gFAAgF;YAChF,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;SACjC,CAAA;KACF;CACF"}
|
package/dist/plugins/dialog.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog.js","sourceRoot":"","sources":["../../src/plugins/dialog.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,8FAA8F;AAC9F,OAAO,KAAK,OAAO,MAAM,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"dialog.js","sourceRoot":"","sources":["../../src/plugins/dialog.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,8FAA8F;AAC9F,OAAO,KAAK,OAAO,MAAM,eAAe,CAAA;AACxC,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAA;AAC/C,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AAGtC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,MAAM,CAAC,OAAO,GAAmB,EAAE;IACjD,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK,EAAE,OAAO,GAAG,IAAI,EAAE,GAAG,OAAO,CAAA;IACtD,+FAA+F;IAC/F,uEAAuE;IACvE,MAAM,OAAO,GAAG,IAAI,OAAO,EAGxB,CAAA;IACH,gFAAgF;IAChF,SAAS,QAAQ,CAAC,OAAuB;QACvC,IAAI,GAAG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MAAM,CAAC,OAAO,CAAC,CAAA;oBAC5B,OAAO,OAAO,CAAA;gBAChB,CAAC;aACF,CAAA;QACH,CAAC;QACD,EAAE,EAAE,QAAQ;QACZ,KAAK,CAAC,OAAO;YACX,4FAA4F;YAC5F,IAAI,KAAK,IAAI,CAAC,OAAO,IAAI,OAAO,QAAQ,KAAK,WAAW;gBAAE,OAAM;YAChE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAA;QAC3C,CAAC;KACF,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tempoxyz/id",
|
|
3
|
-
"version": "0.0.0-pr-
|
|
3
|
+
"version": "0.0.0-pr-494.sha-e349a84",
|
|
4
4
|
"description": "Identity and Auth for Tempo",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": [
|
|
@@ -41,6 +41,11 @@
|
|
|
41
41
|
"types": "./dist/internal.d.ts",
|
|
42
42
|
"default": "./dist/internal.js"
|
|
43
43
|
},
|
|
44
|
+
"./mpp": {
|
|
45
|
+
"src": "./src/mpp/index.ts",
|
|
46
|
+
"types": "./dist/mpp/index.d.ts",
|
|
47
|
+
"default": "./dist/mpp/index.js"
|
|
48
|
+
},
|
|
44
49
|
"./plugins": {
|
|
45
50
|
"src": "./src/plugins/index.ts",
|
|
46
51
|
"types": "./dist/plugins/index.d.ts",
|
|
@@ -80,6 +85,7 @@
|
|
|
80
85
|
"@wagmi/core": "^3.6.7",
|
|
81
86
|
"expo-secure-store": ">=14",
|
|
82
87
|
"expo-web-browser": ">=14",
|
|
88
|
+
"mppx": ">=0.12",
|
|
83
89
|
"react": ">=18",
|
|
84
90
|
"react-native-get-random-values": ">=1",
|
|
85
91
|
"viem": "^2.57.12"
|
|
@@ -94,6 +100,9 @@
|
|
|
94
100
|
"expo-web-browser": {
|
|
95
101
|
"optional": true
|
|
96
102
|
},
|
|
103
|
+
"mppx": {
|
|
104
|
+
"optional": true
|
|
105
|
+
},
|
|
97
106
|
"react": {
|
|
98
107
|
"optional": true
|
|
99
108
|
},
|
package/src/mpp/index.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry for MPP: `mpp`, a plugin that adds `tempoId.mpp`, which pays the 402 challenges of
|
|
3
|
+
* Machine Payments Protocol (MPP) endpoints with the session's access key, through mppx.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Id } from '@tempoxyz/id'
|
|
8
|
+
* import { mpp } from '@tempoxyz/id/mpp'
|
|
9
|
+
* import { dialog } from '@tempoxyz/id/plugins'
|
|
10
|
+
*
|
|
11
|
+
* const tempoId = Id.create({ accessKey, plugins: [dialog(), mpp()] })
|
|
12
|
+
*
|
|
13
|
+
* await tempoId.dialog.signIn()
|
|
14
|
+
* const response = await tempoId.mpp.fetch('https://api.example.com/fortune')
|
|
15
|
+
* ```
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
import { Mppx, tempo } from 'mppx/client'
|
|
19
|
+
import type { Chain, Client, Transport } from 'viem'
|
|
20
|
+
import { createClient, formatUnits, parseUnits } from 'viem'
|
|
21
|
+
import type { Account } from 'viem/tempo'
|
|
22
|
+
import * as AccessKey from '../AccessKey.js'
|
|
23
|
+
import * as Id from '../Id.js'
|
|
24
|
+
import * as Plugin from '../Plugin.js'
|
|
25
|
+
import { settings, viem } from '../plugins/viem.js'
|
|
26
|
+
import type * as Session from '../Session.js'
|
|
27
|
+
|
|
28
|
+
/** A charge costs more than `maxAmount`, so the payer signs nothing. */
|
|
29
|
+
export class MaxAmountError extends Error {
|
|
30
|
+
override readonly name = 'mpp.MaxAmountError'
|
|
31
|
+
|
|
32
|
+
constructor(options: MaxAmountError.Options) {
|
|
33
|
+
super(
|
|
34
|
+
`The charge of ${options.amount} exceeds the \`maxAmount\` of ${options.maxAmount}. Raise \`maxAmount\` to pay it.`,
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export declare namespace MaxAmountError {
|
|
40
|
+
type Options = {
|
|
41
|
+
/** The charge's amount, in the units of its token. */
|
|
42
|
+
amount: string
|
|
43
|
+
/** The most that a charge may cost, in the units of its token. */
|
|
44
|
+
maxAmount: string
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns the plugin of MPP payments, which adds `tempoId.mpp`. `tempoId.mpp.fetch` is `fetch`
|
|
50
|
+
* that pays each charge an endpoint challenges for, and `tempoId.mpp.session()` returns a session
|
|
51
|
+
* manager, which pays each request of a channel with a voucher. The access key of the instance's
|
|
52
|
+
* session signs every payment, so no payment reaches the account's owner. `tempoId.mpp({ session })`
|
|
53
|
+
* pays with a fixed session's access key, such as one that `tempoId.backend.get` returns.
|
|
54
|
+
*
|
|
55
|
+
* The plugin reads the chains and transports of the `viem` plugin, or of a `viem` plugin of its
|
|
56
|
+
* own without one in `plugins`. `mppx` is an optional peer dependency of `@tempoxyz/id`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const tempoId = Id.create({ accessKey, plugins: [dialog(), mpp()] })
|
|
61
|
+
*
|
|
62
|
+
* const response = await tempoId.mpp.fetch('https://api.example.com/fortune')
|
|
63
|
+
*
|
|
64
|
+
* const session = tempoId.mpp.session()
|
|
65
|
+
* await session.fetch('https://api.example.com/content')
|
|
66
|
+
* await session.close()
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function mpp(options: mpp.Options = {}) {
|
|
70
|
+
const { fetch } = options
|
|
71
|
+
// The context of each instance, by its configuration, since an app can pass one plugin to
|
|
72
|
+
// several instances.
|
|
73
|
+
const contexts = new WeakMap<Id.Config, Plugin.Context>()
|
|
74
|
+
|
|
75
|
+
return Plugin.from({
|
|
76
|
+
extend(instance: Id.Id) {
|
|
77
|
+
const context = contexts.get(instance.config)!
|
|
78
|
+
const held = (instance as unknown as { viem?: viem.Namespace }).viem
|
|
79
|
+
const namespace = held ?? (viem().actions!(context) as viem.Namespace)
|
|
80
|
+
const { chains, transport } = namespace[
|
|
81
|
+
settings as never
|
|
82
|
+
] as unknown as viem.Settings
|
|
83
|
+
// A client without an account reads the chain and broadcasts. The access key signs.
|
|
84
|
+
const clients = new Map<number, Client<Transport, Chain>>()
|
|
85
|
+
// The payers of each access key, one for each setting of the charges.
|
|
86
|
+
const payers = new WeakMap<AccessKey.AccessKey, Map<string, mpp.Payer>>()
|
|
87
|
+
|
|
88
|
+
/** Returns the client of the chain of `chainId`, one of the plugin's chains. */
|
|
89
|
+
function getClient(parameters: { chainId?: number | undefined } = {}) {
|
|
90
|
+
const chainId = parameters.chainId ?? instance.config.chainId
|
|
91
|
+
const existing = clients.get(chainId)
|
|
92
|
+
if (existing) return existing
|
|
93
|
+
const chain = chains.find((entry) => entry.id === chainId)
|
|
94
|
+
if (!chain)
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Invalid chain: ${chainId} is not one of the viem plugin's \`chains\`. Add its definition to \`chains\`.`,
|
|
97
|
+
)
|
|
98
|
+
const client = createClient({ chain, transport: transport(chainId) })
|
|
99
|
+
clients.set(chainId, client)
|
|
100
|
+
return client
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns the payer of the access key of `overrides.session`, or of the instance's session,
|
|
105
|
+
* with the plugin's settings under `overrides`.
|
|
106
|
+
*/
|
|
107
|
+
function payer(overrides: mpp.Overrides) {
|
|
108
|
+
const current = overrides.session ?? context.session.get()
|
|
109
|
+
if (!current) throw new Id.SignedOutError()
|
|
110
|
+
if (!current.key) throw new Id.KeyRequiredError()
|
|
111
|
+
const maxAmount = overrides.maxAmount ?? options.maxAmount
|
|
112
|
+
const maxDeposit = overrides.maxDeposit ?? options.maxDeposit
|
|
113
|
+
const mode = overrides.mode ?? options.mode
|
|
114
|
+
let byKey = payers.get(current.key)
|
|
115
|
+
if (!byKey) payers.set(current.key, (byKey = new Map()))
|
|
116
|
+
const name = [maxAmount, maxDeposit, mode].join('\n')
|
|
117
|
+
const existing = byKey.get(name)
|
|
118
|
+
if (existing) return existing
|
|
119
|
+
|
|
120
|
+
const created = create({
|
|
121
|
+
account: AccessKey.toAccount(AccessKey.from(current.key)),
|
|
122
|
+
fetch,
|
|
123
|
+
getClient,
|
|
124
|
+
maxAmount,
|
|
125
|
+
maxDeposit,
|
|
126
|
+
mode,
|
|
127
|
+
})
|
|
128
|
+
byKey.set(name, created)
|
|
129
|
+
return created
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Returns the payer that resolves its access key at each call, as `payer` does. */
|
|
133
|
+
function bind(overrides: mpp.Overrides = {}): mpp.Payer {
|
|
134
|
+
return {
|
|
135
|
+
// A missing session or access key rejects the request, as a failed fetch does.
|
|
136
|
+
fetch: async (input, init) => payer(overrides).fetch(input, init),
|
|
137
|
+
session: (parameters) => payer(overrides).session(parameters),
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
mpp: Object.assign(bind, bind()) as mpp.Namespace,
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
id: 'mpp',
|
|
146
|
+
setup(value) {
|
|
147
|
+
contexts.set(value.config, value)
|
|
148
|
+
},
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export declare namespace mpp {
|
|
153
|
+
type Options = Settings & {
|
|
154
|
+
/** Sends each request to an endpoint, and its paid retry. Defaults to the global `fetch`. */
|
|
155
|
+
fetch?: typeof globalThis.fetch | undefined
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The settings of the payments, which the plugin's options set and a call overrides. */
|
|
159
|
+
type Settings = {
|
|
160
|
+
/**
|
|
161
|
+
* The most that a charge may cost, in the units of its token, such as `'0.1'`. A larger charge
|
|
162
|
+
* throws `mpp.MaxAmountError` before the access key signs. Defaults to no maximum, within the
|
|
163
|
+
* access key's token limits.
|
|
164
|
+
*/
|
|
165
|
+
maxAmount?: string | undefined
|
|
166
|
+
/**
|
|
167
|
+
* The most that a session's channel holds, in the units of its token, such as `'0.05'`. It
|
|
168
|
+
* caps the deposit that an endpoint suggests and each top-up. Defaults to the deposit that the
|
|
169
|
+
* endpoint suggests, within the access key's token limits.
|
|
170
|
+
*/
|
|
171
|
+
maxDeposit?: string | undefined
|
|
172
|
+
/**
|
|
173
|
+
* Who broadcasts a charge's transaction: the endpoint (`'pull'`) or the app (`'push'`).
|
|
174
|
+
* Defaults to `'pull'`.
|
|
175
|
+
*/
|
|
176
|
+
mode?: 'pull' | 'push' | undefined
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The overrides of a payer, for one call. */
|
|
180
|
+
type Overrides = Settings & {
|
|
181
|
+
/**
|
|
182
|
+
* A fixed session, such as one that `tempoId.backend.get` returns, whose access key pays.
|
|
183
|
+
* Defaults to the instance's session, read at each payment.
|
|
184
|
+
*/
|
|
185
|
+
session?: Session.Session | undefined
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The payments of one access key. */
|
|
189
|
+
type Payer = {
|
|
190
|
+
/**
|
|
191
|
+
* Sends a request, and pays a 402 charge challenge with the access key, then returns the
|
|
192
|
+
* endpoint's response to the paid retry. Throws `Id.SignedOutError` without a session, and
|
|
193
|
+
* `Id.KeyRequiredError` for a session without an access key.
|
|
194
|
+
*/
|
|
195
|
+
fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>
|
|
196
|
+
/**
|
|
197
|
+
* Returns a session manager of mppx, which opens a channel on the first request of a session
|
|
198
|
+
* endpoint, pays each request with a voucher of the access key, and closes the channel at
|
|
199
|
+
* `close()`. The manager holds the channel in memory, so close it before it is discarded.
|
|
200
|
+
*/
|
|
201
|
+
session: (
|
|
202
|
+
parameters?: Omit<
|
|
203
|
+
Parameters<typeof tempo.session.manager>[0],
|
|
204
|
+
'account' | 'client' | 'getClient'
|
|
205
|
+
>,
|
|
206
|
+
) => ReturnType<typeof tempo.session.manager>
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** `tempoId.mpp`: the payer of the instance's session, which a call fixes to another session. */
|
|
210
|
+
type Namespace = Payer & ((overrides?: Overrides) => Payer)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Creates the payer of `account`. */
|
|
214
|
+
function create(options: {
|
|
215
|
+
account: Account.AccessKeyAccount
|
|
216
|
+
fetch: typeof globalThis.fetch | undefined
|
|
217
|
+
getClient: (parameters: { chainId?: number | undefined }) => Client
|
|
218
|
+
maxAmount: string | undefined
|
|
219
|
+
maxDeposit: string | undefined
|
|
220
|
+
mode: 'pull' | 'push' | undefined
|
|
221
|
+
}): mpp.Payer {
|
|
222
|
+
const { account, fetch, getClient, maxAmount, maxDeposit, mode } = options
|
|
223
|
+
const mppx = Mppx.create({
|
|
224
|
+
...(fetch ? { fetch } : {}),
|
|
225
|
+
methods: [tempo.charge({ account, getClient, ...(mode ? { mode } : {}) })],
|
|
226
|
+
// The payer reads each charge's amount before the access key signs. A TIP-20 token has 6
|
|
227
|
+
// decimals.
|
|
228
|
+
onChallenge: (challenge) => {
|
|
229
|
+
if (maxAmount === undefined) return Promise.resolve(undefined)
|
|
230
|
+
const amount = BigInt(String(challenge.request.amount ?? 0))
|
|
231
|
+
if (amount > parseUnits(maxAmount, 6))
|
|
232
|
+
throw new MaxAmountError({ amount: formatUnits(amount, 6), maxAmount })
|
|
233
|
+
return Promise.resolve(undefined)
|
|
234
|
+
},
|
|
235
|
+
// The payer pays only through its own `fetch`, so the app's other requests stay unchanged.
|
|
236
|
+
polyfill: false,
|
|
237
|
+
})
|
|
238
|
+
return {
|
|
239
|
+
fetch: (input, init) => mppx.fetch(input, init),
|
|
240
|
+
session: (parameters = {}) =>
|
|
241
|
+
tempo.session.manager({
|
|
242
|
+
...(fetch ? { fetch } : {}),
|
|
243
|
+
...(maxDeposit ? { maxDeposit } : {}),
|
|
244
|
+
...parameters,
|
|
245
|
+
account,
|
|
246
|
+
getClient,
|
|
247
|
+
}),
|
|
248
|
+
}
|
|
249
|
+
}
|
package/src/plugins/dialog.ts
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
/* istanbul ignore file -- Each action needs a browser window, for the dialog or the popup. */
|
|
6
6
|
import * as Actions from '../Actions.js'
|
|
7
|
-
import type * as Funding from '../Funding.js'
|
|
8
7
|
import * as SignIn from '../internal/signIn.js'
|
|
9
8
|
import * as Plugin from '../Plugin.js'
|
|
10
9
|
import type * as Session from '../Session.js'
|
|
@@ -58,7 +57,6 @@ export function dialog(options: dialog.Options = {}) {
|
|
|
58
57
|
.signIn(config, {
|
|
59
58
|
accessKey,
|
|
60
59
|
challenge: parameters.challenge,
|
|
61
|
-
funding: parameters.funding,
|
|
62
60
|
mode: parameters.mode ?? (popup ? 'popup' : 'iframe'),
|
|
63
61
|
signal: parameters.signal,
|
|
64
62
|
})
|
|
@@ -97,11 +95,6 @@ export declare namespace dialog {
|
|
|
97
95
|
accessKey?: SignIn.AccessKeyOption
|
|
98
96
|
/** A message whose EIP-191 hash the key authorization binds as its `witness`. */
|
|
99
97
|
challenge?: string | undefined
|
|
100
|
-
/**
|
|
101
|
-
* The amount that the app needs in the user's account. A frame cannot show the funding page,
|
|
102
|
-
* so the dialog continues in a popup when it opens.
|
|
103
|
-
*/
|
|
104
|
-
funding?: Funding.Funding | undefined
|
|
105
98
|
/**
|
|
106
99
|
* Whether the sign-in shows in a frame or in a popup window. Defaults to the plugin's `popup`.
|
|
107
100
|
* A popup opens only within a click.
|