@mysten/enoki 1.4.0 → 1.5.0
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/CHANGELOG.md +12 -0
- package/README.md +51 -0
- package/dist/EnokiClient/index.d.mts +19 -1
- package/dist/EnokiClient/index.d.mts.map +1 -1
- package/dist/EnokiClient/index.mjs +51 -10
- package/dist/EnokiClient/index.mjs.map +1 -1
- package/dist/EnokiClient/type.d.mts +1 -1
- package/dist/EnokiFlow.d.mts +22 -2
- package/dist/EnokiFlow.d.mts.map +1 -1
- package/dist/client.d.mts +2 -2
- package/dist/extension.d.mts +35 -10
- package/dist/extension.d.mts.map +1 -1
- package/dist/extension.mjs +56 -47
- package/dist/extension.mjs.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/wallet/wallet.mjs +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @mysten/enoki
|
|
2
2
|
|
|
3
|
+
## 1.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 7a6b363: - `EnokiClient.prepareSponsoredTransaction({ transaction, network })` sets Enoki's
|
|
8
|
+
sponsor as gas owner with an empty gas payment. It works with a public API key, so a browser can
|
|
9
|
+
prepare a transaction, have the wallet sign it, and send it to your backend.
|
|
10
|
+
`EnokiClient.getSponsorAddress({ network })` returns the cached address. The `enoki()` extension's
|
|
11
|
+
`prepareSponsoredTransaction` and `getSponsorAddress` now use these.
|
|
12
|
+
- `client.enoki.getSponsorSigner()` returns a remote signer for the sponsor SDK's `createSponsor`,
|
|
13
|
+
so a sponsor built with it uses Enoki. Transactions Enoki declines are returned as `Rejected`.
|
|
14
|
+
|
|
3
15
|
## 1.4.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -49,6 +49,57 @@ function LoginButton() {
|
|
|
49
49
|
}
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
### Sponsor transactions
|
|
53
|
+
|
|
54
|
+
Add the `enoki()` extension to a Sui client on your backend, with a private API key that has
|
|
55
|
+
sponsored transactions enabled. Enoki pays gas from its sponsor's address balance, so the sender
|
|
56
|
+
doesn't need SUI.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { enoki } from '@mysten/enoki';
|
|
60
|
+
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
61
|
+
|
|
62
|
+
const client = new SuiGrpcClient({
|
|
63
|
+
network: 'mainnet',
|
|
64
|
+
baseUrl: 'https://fullnode.mainnet.sui.io:443',
|
|
65
|
+
}).$extend(enoki({ apiKey: process.env.ENOKI_PRIVATE_KEY! }));
|
|
66
|
+
|
|
67
|
+
// Builds with Enoki as gas owner, signs, and has Enoki sponsor and execute it
|
|
68
|
+
const result = await client.enoki.sponsorAndExecuteTransaction({ transaction, signer });
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
To sign another way, such as with a wallet, or to check the transaction first, prepare it and send
|
|
72
|
+
the signed bytes:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
transaction.setSenderIfNotSet(signer.toSuiAddress());
|
|
76
|
+
await client.enoki.prepareSponsoredTransaction({ transaction });
|
|
77
|
+
const bytes = await transaction.build({ client });
|
|
78
|
+
const { signature } = await signer.signTransaction(bytes);
|
|
79
|
+
|
|
80
|
+
await client.enoki.sponsorAndExecuteTransaction({ transaction: bytes, signature });
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
If `isStaleTransactionError(error)` is true, the sponsor address or reference gas price changed
|
|
84
|
+
since the transaction was built. Create a new `Transaction`, sign it and try again; one that was
|
|
85
|
+
already built keeps its gas owner and gas price.
|
|
86
|
+
|
|
87
|
+
In a browser, prepare the transaction with an `EnokiClient` that uses your public API key, have the
|
|
88
|
+
wallet sign it, and send the bytes and signature to your backend:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { EnokiClient } from '@mysten/enoki';
|
|
92
|
+
|
|
93
|
+
const enoki = new EnokiClient({ apiKey: 'your-public-api-key' });
|
|
94
|
+
|
|
95
|
+
await enoki.prepareSponsoredTransaction({ transaction, network: 'mainnet' });
|
|
96
|
+
const { bytes, signature } = await dAppKit.signTransaction({ transaction });
|
|
97
|
+
// POST { bytes, signature } to your backend, which calls client.enoki.sponsorAndExecuteTransaction
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The sponsor address is cached for up to a minute, so after Enoki rotates it a retry may fail until
|
|
101
|
+
the cache refreshes.
|
|
102
|
+
|
|
52
103
|
### Low-level client usage
|
|
53
104
|
|
|
54
105
|
Use the client-only entry point to call the Enoki API without loading wallet helpers or local
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { CreateSponsoredTransactionApiInput, CreateSponsoredTransactionApiResponse, CreateSubnameApiInput, CreateSubnameApiResponse, CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, DeleteSubnameApiInput, ExecuteSponsoredTransactionApiInput, ExecuteSponsoredTransactionApiResponse, GetAppApiInput, GetAppApiResponse, GetSubnamesApiInput, GetSubnamesApiResponse, GetZkLoginAddressesApiInput, GetZkLoginAddressesApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse, SponsorAndExecuteTransactionApiInput, SponsorAndExecuteTransactionApiResponse } from "./type.mjs";
|
|
1
|
+
import { CreateSponsoredTransactionApiInput, CreateSponsoredTransactionApiResponse, CreateSubnameApiInput, CreateSubnameApiResponse, CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, DeleteSubnameApiInput, EnokiDomainNetwork, ExecuteSponsoredTransactionApiInput, ExecuteSponsoredTransactionApiResponse, GetAppApiInput, GetAppApiResponse, GetSubnamesApiInput, GetSubnamesApiResponse, GetZkLoginAddressesApiInput, GetZkLoginAddressesApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse, SponsorAndExecuteTransactionApiInput, SponsorAndExecuteTransactionApiResponse } from "./type.mjs";
|
|
2
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
2
3
|
|
|
3
4
|
//#region src/EnokiClient/index.d.ts
|
|
4
5
|
interface EnokiClientConfig {
|
|
@@ -51,6 +52,23 @@ declare class EnokiClient {
|
|
|
51
52
|
* sign again and retry. `already_sponsored` means Enoki has already sponsored this transaction.
|
|
52
53
|
*/
|
|
53
54
|
sponsorAndExecuteTransaction(input: SponsorAndExecuteTransactionApiInput): Promise<SponsorAndExecuteTransactionApiResponse>;
|
|
55
|
+
/** Enoki's sponsor address. Works with a public key. Cached for a minute, or until a stale error. */
|
|
56
|
+
getSponsorAddress({
|
|
57
|
+
network
|
|
58
|
+
}: {
|
|
59
|
+
network: EnokiDomainNetwork;
|
|
60
|
+
}): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Sets Enoki's sponsor as gas owner with an empty gas payment, without building. Works with a
|
|
63
|
+
* public key. Throws if the transaction has a different gas owner or a gas payment.
|
|
64
|
+
*/
|
|
65
|
+
prepareSponsoredTransaction({
|
|
66
|
+
transaction,
|
|
67
|
+
network
|
|
68
|
+
}: {
|
|
69
|
+
transaction: Transaction;
|
|
70
|
+
network: EnokiDomainNetwork;
|
|
71
|
+
}): Promise<Transaction>;
|
|
54
72
|
getSubnames(input: GetSubnamesApiInput): Promise<GetSubnamesApiResponse>;
|
|
55
73
|
createSubname(input: CreateSubnameApiInput): Promise<CreateSubnameApiResponse>;
|
|
56
74
|
deleteSubname(input: DeleteSubnameApiInput): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/EnokiClient/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/EnokiClient/index.ts"],"mappings":";;;;UAoCiB,iBAAA;;EAEhB,MAAA;EAFiC;EAKjC,MAAA;EALiC;EAQjC,gBAAA;AAAA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AA;AAAA"}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { normalizeSuiAddress } from "@mysten/sui/utils";
|
|
2
|
+
|
|
1
3
|
//#region src/EnokiClient/index.ts
|
|
2
4
|
const DEFAULT_API_URL = "https://api.enoki.mystenlabs.com";
|
|
5
|
+
const SPONSOR_ADDRESS_TTL_MS = 6e4;
|
|
3
6
|
const ZKLOGIN_HEADER = "zklogin-jwt";
|
|
4
7
|
var EnokiClientError = class extends Error {
|
|
5
8
|
constructor(status, response) {
|
|
@@ -38,6 +41,7 @@ var EnokiClient = class {
|
|
|
38
41
|
#apiUrl;
|
|
39
42
|
#apiKey;
|
|
40
43
|
#additionalEpochs;
|
|
44
|
+
#sponsorAddresses = /* @__PURE__ */ new Map();
|
|
41
45
|
constructor(config) {
|
|
42
46
|
this.#version = "v1";
|
|
43
47
|
this.#apiUrl = config.apiUrl ?? DEFAULT_API_URL;
|
|
@@ -108,17 +112,54 @@ var EnokiClient = class {
|
|
|
108
112
|
* On `invalid_gas_owner` or `invalid_gas_price`, rebuild with the value in the error's `data`,
|
|
109
113
|
* sign again and retry. `already_sponsored` means Enoki has already sponsored this transaction.
|
|
110
114
|
*/
|
|
111
|
-
sponsorAndExecuteTransaction(input) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
115
|
+
async sponsorAndExecuteTransaction(input) {
|
|
116
|
+
try {
|
|
117
|
+
return await this.#fetch("transaction-blocks/sponsor/execute", {
|
|
118
|
+
method: "POST",
|
|
119
|
+
body: JSON.stringify({
|
|
120
|
+
network: input.network,
|
|
121
|
+
transactionBytes: input.transactionBytes,
|
|
122
|
+
signature: input.signature,
|
|
123
|
+
allowedAddresses: input.allowedAddresses,
|
|
124
|
+
allowedMoveCallTargets: input.allowedMoveCallTargets
|
|
125
|
+
})
|
|
126
|
+
});
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (isStaleTransactionError(error)) this.#sponsorAddresses.delete(input.network === "testnet" ? "testnet" : "mainnet");
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** Enoki's sponsor address. Works with a public key. Cached for a minute, or until a stale error. */
|
|
133
|
+
getSponsorAddress({ network }) {
|
|
134
|
+
const cached = this.#sponsorAddresses.get(network);
|
|
135
|
+
if (cached && cached.expiresAt > Date.now()) return cached.address;
|
|
136
|
+
const address = this.getApp().then(({ sponsorAddresses }) => {
|
|
137
|
+
const sponsorAddress = sponsorAddresses?.[network];
|
|
138
|
+
if (!sponsorAddress) throw new Error(`Enoki does not sponsor transactions on ${network}`);
|
|
139
|
+
return normalizeSuiAddress(sponsorAddress);
|
|
140
|
+
});
|
|
141
|
+
const entry = {
|
|
142
|
+
address,
|
|
143
|
+
expiresAt: Date.now() + SPONSOR_ADDRESS_TTL_MS
|
|
144
|
+
};
|
|
145
|
+
address.catch(() => {
|
|
146
|
+
if (this.#sponsorAddresses.get(network) === entry) this.#sponsorAddresses.delete(network);
|
|
121
147
|
});
|
|
148
|
+
this.#sponsorAddresses.set(network, entry);
|
|
149
|
+
return address;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Sets Enoki's sponsor as gas owner with an empty gas payment, without building. Works with a
|
|
153
|
+
* public key. Throws if the transaction has a different gas owner or a gas payment.
|
|
154
|
+
*/
|
|
155
|
+
async prepareSponsoredTransaction({ transaction, network }) {
|
|
156
|
+
const sponsorAddress = await this.getSponsorAddress({ network });
|
|
157
|
+
const { gasData } = transaction.getData();
|
|
158
|
+
if (gasData.owner && normalizeSuiAddress(gasData.owner) !== sponsorAddress) throw new Error(`Transaction gas owner is ${gasData.owner}, not Enoki's sponsor ${sponsorAddress}`);
|
|
159
|
+
if (gasData.payment && gasData.payment.length > 0) throw new Error("Sponsored transactions pay gas from the sponsor; remove the gas payment");
|
|
160
|
+
transaction.setGasOwner(sponsorAddress);
|
|
161
|
+
transaction.setGasPayment([]);
|
|
162
|
+
return transaction;
|
|
122
163
|
}
|
|
123
164
|
getSubnames(input) {
|
|
124
165
|
const query = new URLSearchParams();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["#version","#apiUrl","#apiKey","#additionalEpochs","#fetch"],"sources":["../../src/EnokiClient/index.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type {\n\tCreateSponsoredTransactionApiInput,\n\tCreateSponsoredTransactionApiResponse,\n\tCreateSubnameApiInput,\n\tCreateSubnameApiResponse,\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tDeleteSubnameApiInput,\n\tDeleteSubnameApiResponse,\n\tExecuteSponsoredTransactionApiInput,\n\tExecuteSponsoredTransactionApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetSubnamesApiInput,\n\tGetSubnamesApiResponse,\n\tGetZkLoginAddressesApiInput,\n\tGetZkLoginAddressesApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\n\tSponsorAndExecuteTransactionApiInput,\n\tSponsorAndExecuteTransactionApiResponse,\n} from './type.js';\n\nconst DEFAULT_API_URL = 'https://api.enoki.mystenlabs.com';\nconst ZKLOGIN_HEADER = 'zklogin-jwt';\n\nexport interface EnokiClientConfig {\n\t/** The API key for the Enoki app, available in the Enoki Portal. */\n\tapiKey: string;\n\n\t/** The API URL for Enoki. In most cases, this should not be set. */\n\tapiUrl?: string;\n\n\t/** The amount of epochs that you would like to have the nonce be valid for. Range: `0 <= value <= 30` */\n\tadditionalEpochs?: number;\n}\n\nexport class EnokiClientError extends Error {\n\terrors: { code: string; message: string; data: unknown }[] = [];\n\tstatus: number;\n\tcode: string;\n\n\tconstructor(status: number, response: string) {\n\t\tlet errors;\n\t\ttry {\n\t\t\tconst parsedResponse = JSON.parse(response) as {\n\t\t\t\terrors: { code: string; message: string; data: unknown }[];\n\t\t\t};\n\t\t\terrors = parsedResponse.errors;\n\t\t} catch {\n\t\t\t// Ignore\n\t\t}\n\t\tconst cause = errors?.[0] ? new Error(errors[0].message) : undefined;\n\t\tsuper(`Request to Enoki API failed (status: ${status})`, {\n\t\t\tcause,\n\t\t});\n\t\tthis.errors = errors ?? [];\n\t\tthis.name = 'EnokiClientError';\n\t\tthis.status = status;\n\t\tthis.code = errors?.[0]?.code ?? 'unknown_error';\n\t}\n\n\t/**\n\t * True when the transaction must be built and signed again before retrying, because Enoki's\n\t * sponsor address or the reference gas price changed since it was built.\n\t */\n\tget requiresRebuild(): boolean {\n\t\treturn this.code === 'invalid_gas_owner' || this.code === 'invalid_gas_price';\n\t}\n}\n\n/**\n * Whether a sponsorship failed because the transaction was built with a sponsor address or\n * reference gas price that's no longer current. Build it again, have the sender sign it, and retry.\n */\nexport function isStaleTransactionError(error: unknown): error is EnokiClientError {\n\treturn error instanceof EnokiClientError && error.requiresRebuild;\n}\n\n/**\n * A low-level client for interacting with the Enoki API.\n */\nexport class EnokiClient {\n\t#version: string;\n\t#apiUrl: string;\n\t#apiKey: string;\n\t#additionalEpochs: number | undefined;\n\n\tconstructor(config: EnokiClientConfig) {\n\t\tthis.#version = 'v1';\n\t\tthis.#apiUrl = config.apiUrl ?? DEFAULT_API_URL;\n\t\tthis.#apiKey = config.apiKey;\n\t\tthis.#additionalEpochs = config.additionalEpochs;\n\t}\n\n\tgetApp(_input?: GetAppApiInput) {\n\t\treturn this.#fetch<GetAppApiResponse>('app', {\n\t\t\tmethod: 'GET',\n\t\t});\n\t}\n\n\tgetZkLogin(input: GetZkLoginApiInput) {\n\t\treturn this.#fetch<GetZkLoginApiResponse>('zklogin', {\n\t\t\tmethod: 'GET',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t});\n\t}\n\n\tgetZkLoginAddresses(input: GetZkLoginAddressesApiInput) {\n\t\treturn this.#fetch<GetZkLoginAddressesApiResponse>('zklogin/addresses', {\n\t\t\tmethod: 'GET',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t});\n\t}\n\n\tcreateZkLoginNonce(input: CreateZkLoginNonceApiInput) {\n\t\treturn this.#fetch<CreateZkLoginNonceApiResponse>('zklogin/nonce', {\n\t\t\tmethod: 'POST',\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tadditionalEpochs: input.additionalEpochs ?? this.#additionalEpochs,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateZkLoginZkp(input: CreateZkLoginZkpApiInput) {\n\t\treturn this.#fetch<CreateZkLoginZkpApiResponse>('zklogin/zkp', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tmaxEpoch: input.maxEpoch,\n\t\t\t\trandomness: input.randomness,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateSponsoredTransaction(input: CreateSponsoredTransactionApiInput) {\n\t\treturn this.#fetch<CreateSponsoredTransactionApiResponse>('transaction-blocks/sponsor', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: input.jwt\n\t\t\t\t? {\n\t\t\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t\t\t}\n\t\t\t\t: {},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tsender: input.sender,\n\t\t\t\tnetwork: input.network,\n\t\t\t\ttransactionBlockKindBytes: input.transactionKindBytes,\n\t\t\t\tallowedAddresses: input.allowedAddresses,\n\t\t\t\tallowedMoveCallTargets: input.allowedMoveCallTargets,\n\t\t\t}),\n\t\t});\n\t}\n\n\texecuteSponsoredTransaction(input: ExecuteSponsoredTransactionApiInput) {\n\t\treturn this.#fetch<ExecuteSponsoredTransactionApiResponse>(\n\t\t\t`transaction-blocks/sponsor/${input.digest}`,\n\t\t\t{\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tsignature: input.signature,\n\t\t\t\t}),\n\t\t\t},\n\t\t);\n\t}\n\n\t/**\n\t * Sponsors and executes a transaction in one request. Build it with the sponsor address from\n\t * `getApp` as gas owner and an empty gas payment, and have the sender sign it. Requires a private\n\t * API key.\n\t *\n\t * On `invalid_gas_owner` or `invalid_gas_price`, rebuild with the value in the error's `data`,\n\t * sign again and retry. `already_sponsored` means Enoki has already sponsored this transaction.\n\t */\n\tsponsorAndExecuteTransaction(input: SponsorAndExecuteTransactionApiInput) {\n\t\treturn this.#fetch<SponsorAndExecuteTransactionApiResponse>(\n\t\t\t'transaction-blocks/sponsor/execute',\n\t\t\t{\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tnetwork: input.network,\n\t\t\t\t\ttransactionBytes: input.transactionBytes,\n\t\t\t\t\tsignature: input.signature,\n\t\t\t\t\tallowedAddresses: input.allowedAddresses,\n\t\t\t\t\tallowedMoveCallTargets: input.allowedMoveCallTargets,\n\t\t\t\t}),\n\t\t\t},\n\t\t);\n\t}\n\n\tgetSubnames(input: GetSubnamesApiInput) {\n\t\tconst query = new URLSearchParams();\n\t\tif (input.address) {\n\t\t\tquery.set('address', input.address);\n\t\t}\n\t\tif (input.network) {\n\t\t\tquery.set('network', input.network);\n\t\t}\n\t\tif (input.domain) {\n\t\t\tquery.set('domain', input.domain);\n\t\t}\n\t\treturn this.#fetch<GetSubnamesApiResponse>(\n\t\t\t'subnames' + (query.size > 0 ? `?${query.toString()}` : ''),\n\t\t\t{\n\t\t\t\tmethod: 'GET',\n\t\t\t},\n\t\t);\n\t}\n\n\tcreateSubname(input: CreateSubnameApiInput) {\n\t\treturn this.#fetch<CreateSubnameApiResponse>('subnames', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: input.jwt\n\t\t\t\t? {\n\t\t\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t\t\t}\n\t\t\t\t: {},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tdomain: input.domain,\n\t\t\t\tsubname: input.subname,\n\t\t\t\ttargetAddress: input.targetAddress,\n\t\t\t}),\n\t\t});\n\t}\n\n\tdeleteSubname(input: DeleteSubnameApiInput) {\n\t\tthis.#fetch<DeleteSubnameApiResponse>('subnames', {\n\t\t\tmethod: 'DELETE',\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tdomain: input.domain,\n\t\t\t\tsubname: input.subname,\n\t\t\t}),\n\t\t});\n\t}\n\n\tasync #fetch<T = unknown>(path: string, init: RequestInit): Promise<T> {\n\t\tconst res = await fetch(`${this.#apiUrl}/${this.#version}/${path}`, {\n\t\t\t...init,\n\t\t\theaders: {\n\t\t\t\t...init.headers,\n\t\t\t\tAuthorization: `Bearer ${this.#apiKey}`,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Request-Id': crypto.randomUUID(),\n\t\t\t},\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new EnokiClientError(res.status, await res.text());\n\t\t}\n\n\t\tconst { data } = await res.json();\n\n\t\treturn data as T;\n\t}\n}\n"],"mappings":";AA4BA,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;AAavB,IAAa,mBAAb,cAAsC,MAAM;CAK3C,YAAY,QAAgB,UAAkB;EAC7C,IAAI;AACJ,MAAI;AAIH,YAHuB,KAAK,MAAM,SAAS,CAGnB;UACjB;EAGR,MAAM,QAAQ,SAAS,KAAK,IAAI,MAAM,OAAO,GAAG,QAAQ,GAAG;AAC3D,QAAM,wCAAwC,OAAO,IAAI,EACxD,OAC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;GACR,MAAM,KAAK,UAAU;IACpB,SAAS,MAAM;IACf,QAAQ,MAAM;IACd,SAAS,MAAM;IACf,CAAC;GACF,CAAC;;CAGH,OAAMA,MAAoB,MAAc,MAA+B;EACtE,MAAM,MAAM,MAAM,MAAM,GAAG,MAAKH,OAAQ,GAAG,MAAKD,QAAS,GAAG,QAAQ;GACnE,GAAG;GACH,SAAS;IACR,GAAG,KAAK;IACR,eAAe,UAAU,MAAKE;IAC9B,gBAAgB;IAChB,cAAc,OAAO,YAAY;IACjC;GACD,CAAC;AAEF,MAAI,CAAC,IAAI,GACR,OAAM,IAAI,iBAAiB,IAAI,QAAQ,MAAM,IAAI,MAAM,CAAC;EAGzD,MAAM,EAAE,SAAS,MAAM,IAAI,MAAM;AAEjC,SAAO"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["#version","#apiUrl","#apiKey","#additionalEpochs","#fetch","#sponsorAddresses"],"sources":["../../src/EnokiClient/index.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { normalizeSuiAddress } from '@mysten/sui/utils';\n\nimport type {\n\tCreateSponsoredTransactionApiInput,\n\tCreateSponsoredTransactionApiResponse,\n\tCreateSubnameApiInput,\n\tCreateSubnameApiResponse,\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tDeleteSubnameApiInput,\n\tDeleteSubnameApiResponse,\n\tEnokiDomainNetwork,\n\tExecuteSponsoredTransactionApiInput,\n\tExecuteSponsoredTransactionApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetSubnamesApiInput,\n\tGetSubnamesApiResponse,\n\tGetZkLoginAddressesApiInput,\n\tGetZkLoginAddressesApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\n\tSponsorAndExecuteTransactionApiInput,\n\tSponsorAndExecuteTransactionApiResponse,\n} from './type.js';\n\nconst DEFAULT_API_URL = 'https://api.enoki.mystenlabs.com';\nconst SPONSOR_ADDRESS_TTL_MS = 60_000;\nconst ZKLOGIN_HEADER = 'zklogin-jwt';\n\nexport interface EnokiClientConfig {\n\t/** The API key for the Enoki app, available in the Enoki Portal. */\n\tapiKey: string;\n\n\t/** The API URL for Enoki. In most cases, this should not be set. */\n\tapiUrl?: string;\n\n\t/** The amount of epochs that you would like to have the nonce be valid for. Range: `0 <= value <= 30` */\n\tadditionalEpochs?: number;\n}\n\nexport class EnokiClientError extends Error {\n\terrors: { code: string; message: string; data: unknown }[] = [];\n\tstatus: number;\n\tcode: string;\n\n\tconstructor(status: number, response: string) {\n\t\tlet errors;\n\t\ttry {\n\t\t\tconst parsedResponse = JSON.parse(response) as {\n\t\t\t\terrors: { code: string; message: string; data: unknown }[];\n\t\t\t};\n\t\t\terrors = parsedResponse.errors;\n\t\t} catch {\n\t\t\t// Ignore\n\t\t}\n\t\tconst cause = errors?.[0] ? new Error(errors[0].message) : undefined;\n\t\tsuper(`Request to Enoki API failed (status: ${status})`, {\n\t\t\tcause,\n\t\t});\n\t\tthis.errors = errors ?? [];\n\t\tthis.name = 'EnokiClientError';\n\t\tthis.status = status;\n\t\tthis.code = errors?.[0]?.code ?? 'unknown_error';\n\t}\n\n\t/**\n\t * True when the transaction must be built and signed again before retrying, because Enoki's\n\t * sponsor address or the reference gas price changed since it was built.\n\t */\n\tget requiresRebuild(): boolean {\n\t\treturn this.code === 'invalid_gas_owner' || this.code === 'invalid_gas_price';\n\t}\n}\n\n/**\n * Whether a sponsorship failed because the transaction was built with a sponsor address or\n * reference gas price that's no longer current. Build it again, have the sender sign it, and retry.\n */\nexport function isStaleTransactionError(error: unknown): error is EnokiClientError {\n\treturn error instanceof EnokiClientError && error.requiresRebuild;\n}\n\n/**\n * A low-level client for interacting with the Enoki API.\n */\nexport class EnokiClient {\n\t#version: string;\n\t#apiUrl: string;\n\t#apiKey: string;\n\t#additionalEpochs: number | undefined;\n\t#sponsorAddresses = new Map<\n\t\tEnokiDomainNetwork,\n\t\t{ address: Promise<string>; expiresAt: number }\n\t>();\n\n\tconstructor(config: EnokiClientConfig) {\n\t\tthis.#version = 'v1';\n\t\tthis.#apiUrl = config.apiUrl ?? DEFAULT_API_URL;\n\t\tthis.#apiKey = config.apiKey;\n\t\tthis.#additionalEpochs = config.additionalEpochs;\n\t}\n\n\tgetApp(_input?: GetAppApiInput) {\n\t\treturn this.#fetch<GetAppApiResponse>('app', {\n\t\t\tmethod: 'GET',\n\t\t});\n\t}\n\n\tgetZkLogin(input: GetZkLoginApiInput) {\n\t\treturn this.#fetch<GetZkLoginApiResponse>('zklogin', {\n\t\t\tmethod: 'GET',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t});\n\t}\n\n\tgetZkLoginAddresses(input: GetZkLoginAddressesApiInput) {\n\t\treturn this.#fetch<GetZkLoginAddressesApiResponse>('zklogin/addresses', {\n\t\t\tmethod: 'GET',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t});\n\t}\n\n\tcreateZkLoginNonce(input: CreateZkLoginNonceApiInput) {\n\t\treturn this.#fetch<CreateZkLoginNonceApiResponse>('zklogin/nonce', {\n\t\t\tmethod: 'POST',\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tadditionalEpochs: input.additionalEpochs ?? this.#additionalEpochs,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateZkLoginZkp(input: CreateZkLoginZkpApiInput) {\n\t\treturn this.#fetch<CreateZkLoginZkpApiResponse>('zklogin/zkp', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tmaxEpoch: input.maxEpoch,\n\t\t\t\trandomness: input.randomness,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateSponsoredTransaction(input: CreateSponsoredTransactionApiInput) {\n\t\treturn this.#fetch<CreateSponsoredTransactionApiResponse>('transaction-blocks/sponsor', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: input.jwt\n\t\t\t\t? {\n\t\t\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t\t\t}\n\t\t\t\t: {},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tsender: input.sender,\n\t\t\t\tnetwork: input.network,\n\t\t\t\ttransactionBlockKindBytes: input.transactionKindBytes,\n\t\t\t\tallowedAddresses: input.allowedAddresses,\n\t\t\t\tallowedMoveCallTargets: input.allowedMoveCallTargets,\n\t\t\t}),\n\t\t});\n\t}\n\n\texecuteSponsoredTransaction(input: ExecuteSponsoredTransactionApiInput) {\n\t\treturn this.#fetch<ExecuteSponsoredTransactionApiResponse>(\n\t\t\t`transaction-blocks/sponsor/${input.digest}`,\n\t\t\t{\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tsignature: input.signature,\n\t\t\t\t}),\n\t\t\t},\n\t\t);\n\t}\n\n\t/**\n\t * Sponsors and executes a transaction in one request. Build it with the sponsor address from\n\t * `getApp` as gas owner and an empty gas payment, and have the sender sign it. Requires a private\n\t * API key.\n\t *\n\t * On `invalid_gas_owner` or `invalid_gas_price`, rebuild with the value in the error's `data`,\n\t * sign again and retry. `already_sponsored` means Enoki has already sponsored this transaction.\n\t */\n\tasync sponsorAndExecuteTransaction(input: SponsorAndExecuteTransactionApiInput) {\n\t\ttry {\n\t\t\treturn await this.#fetch<SponsorAndExecuteTransactionApiResponse>(\n\t\t\t\t'transaction-blocks/sponsor/execute',\n\t\t\t\t{\n\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\tnetwork: input.network,\n\t\t\t\t\t\ttransactionBytes: input.transactionBytes,\n\t\t\t\t\t\tsignature: input.signature,\n\t\t\t\t\t\tallowedAddresses: input.allowedAddresses,\n\t\t\t\t\t\tallowedMoveCallTargets: input.allowedMoveCallTargets,\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tif (isStaleTransactionError(error)) {\n\t\t\t\tthis.#sponsorAddresses.delete(input.network === 'testnet' ? 'testnet' : 'mainnet');\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/** Enoki's sponsor address. Works with a public key. Cached for a minute, or until a stale error. */\n\tgetSponsorAddress({ network }: { network: EnokiDomainNetwork }): Promise<string> {\n\t\tconst cached = this.#sponsorAddresses.get(network);\n\t\tif (cached && cached.expiresAt > Date.now()) {\n\t\t\treturn cached.address;\n\t\t}\n\n\t\tconst address = this.getApp().then(({ sponsorAddresses }) => {\n\t\t\tconst sponsorAddress = sponsorAddresses?.[network];\n\t\t\tif (!sponsorAddress) {\n\t\t\t\tthrow new Error(`Enoki does not sponsor transactions on ${network}`);\n\t\t\t}\n\t\t\treturn normalizeSuiAddress(sponsorAddress);\n\t\t});\n\t\tconst entry = { address, expiresAt: Date.now() + SPONSOR_ADDRESS_TTL_MS };\n\t\taddress.catch(() => {\n\t\t\tif (this.#sponsorAddresses.get(network) === entry) {\n\t\t\t\tthis.#sponsorAddresses.delete(network);\n\t\t\t}\n\t\t});\n\t\tthis.#sponsorAddresses.set(network, entry);\n\t\treturn address;\n\t}\n\n\t/**\n\t * Sets Enoki's sponsor as gas owner with an empty gas payment, without building. Works with a\n\t * public key. Throws if the transaction has a different gas owner or a gas payment.\n\t */\n\tasync prepareSponsoredTransaction({\n\t\ttransaction,\n\t\tnetwork,\n\t}: {\n\t\ttransaction: Transaction;\n\t\tnetwork: EnokiDomainNetwork;\n\t}): Promise<Transaction> {\n\t\tconst sponsorAddress = await this.getSponsorAddress({ network });\n\t\tconst { gasData } = transaction.getData();\n\n\t\tif (gasData.owner && normalizeSuiAddress(gasData.owner) !== sponsorAddress) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction gas owner is ${gasData.owner}, not Enoki's sponsor ${sponsorAddress}`,\n\t\t\t);\n\t\t}\n\t\tif (gasData.payment && gasData.payment.length > 0) {\n\t\t\tthrow new Error('Sponsored transactions pay gas from the sponsor; remove the gas payment');\n\t\t}\n\n\t\ttransaction.setGasOwner(sponsorAddress);\n\t\ttransaction.setGasPayment([]);\n\t\treturn transaction;\n\t}\n\n\tgetSubnames(input: GetSubnamesApiInput) {\n\t\tconst query = new URLSearchParams();\n\t\tif (input.address) {\n\t\t\tquery.set('address', input.address);\n\t\t}\n\t\tif (input.network) {\n\t\t\tquery.set('network', input.network);\n\t\t}\n\t\tif (input.domain) {\n\t\t\tquery.set('domain', input.domain);\n\t\t}\n\t\treturn this.#fetch<GetSubnamesApiResponse>(\n\t\t\t'subnames' + (query.size > 0 ? `?${query.toString()}` : ''),\n\t\t\t{\n\t\t\t\tmethod: 'GET',\n\t\t\t},\n\t\t);\n\t}\n\n\tcreateSubname(input: CreateSubnameApiInput) {\n\t\treturn this.#fetch<CreateSubnameApiResponse>('subnames', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: input.jwt\n\t\t\t\t? {\n\t\t\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t\t\t}\n\t\t\t\t: {},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tdomain: input.domain,\n\t\t\t\tsubname: input.subname,\n\t\t\t\ttargetAddress: input.targetAddress,\n\t\t\t}),\n\t\t});\n\t}\n\n\tdeleteSubname(input: DeleteSubnameApiInput) {\n\t\tthis.#fetch<DeleteSubnameApiResponse>('subnames', {\n\t\t\tmethod: 'DELETE',\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tdomain: input.domain,\n\t\t\t\tsubname: input.subname,\n\t\t\t}),\n\t\t});\n\t}\n\n\tasync #fetch<T = unknown>(path: string, init: RequestInit): Promise<T> {\n\t\tconst res = await fetch(`${this.#apiUrl}/${this.#version}/${path}`, {\n\t\t\t...init,\n\t\t\theaders: {\n\t\t\t\t...init.headers,\n\t\t\t\tAuthorization: `Bearer ${this.#apiKey}`,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Request-Id': crypto.randomUUID(),\n\t\t\t},\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new EnokiClientError(res.status, await res.text());\n\t\t}\n\n\t\tconst { data } = await res.json();\n\n\t\treturn data as T;\n\t}\n}\n"],"mappings":";;;AAgCA,MAAM,kBAAkB;AACxB,MAAM,yBAAyB;AAC/B,MAAM,iBAAiB;AAavB,IAAa,mBAAb,cAAsC,MAAM;CAK3C,YAAY,QAAgB,UAAkB;EAC7C,IAAI;AACJ,MAAI;AAIH,YAHuB,KAAK,MAAM,SAAS,CAGnB;UACjB;EAGR,MAAM,QAAQ,SAAS,KAAK,IAAI,MAAM,OAAO,GAAG,QAAQ,GAAG;AAC3D,QAAM,wCAAwC,OAAO,IAAI,EACxD,OACA,CAAC;gBAjB0D,EAAE;AAkB9D,OAAK,SAAS,UAAU,EAAE;AAC1B,OAAK,OAAO;AACZ,OAAK,SAAS;AACd,OAAK,OAAO,SAAS,IAAI,QAAQ;;;;;;CAOlC,IAAI,kBAA2B;AAC9B,SAAO,KAAK,SAAS,uBAAuB,KAAK,SAAS;;;;;;;AAQ5D,SAAgB,wBAAwB,OAA2C;AAClF,QAAO,iBAAiB,oBAAoB,MAAM;;;;;AAMnD,IAAa,cAAb,MAAyB;CACxB;CACA;CACA;CACA;CACA,oCAAoB,IAAI,KAGrB;CAEH,YAAY,QAA2B;AACtC,QAAKA,UAAW;AAChB,QAAKC,SAAU,OAAO,UAAU;AAChC,QAAKC,SAAU,OAAO;AACtB,QA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
|
-
export { AuthProvider, CreateSponsoredTransactionApiInput, CreateSponsoredTransactionApiResponse, CreateSubnameApiInput, CreateSubnameApiResponse, CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, DeleteSubnameApiInput, EnokiNetwork, ExecuteSponsoredTransactionApiInput, ExecuteSponsoredTransactionApiResponse, GetAppApiInput, GetAppApiResponse, GetSubnamesApiInput, GetSubnamesApiResponse, GetZkLoginAddressesApiInput, GetZkLoginAddressesApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse, SponsorAndExecuteTransactionApiInput, SponsorAndExecuteTransactionApiResponse };
|
|
138
|
+
export { AuthProvider, CreateSponsoredTransactionApiInput, CreateSponsoredTransactionApiResponse, CreateSubnameApiInput, CreateSubnameApiResponse, CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, DeleteSubnameApiInput, EnokiDomainNetwork, EnokiNetwork, ExecuteSponsoredTransactionApiInput, ExecuteSponsoredTransactionApiResponse, GetAppApiInput, GetAppApiResponse, GetSubnamesApiInput, GetSubnamesApiResponse, GetZkLoginAddressesApiInput, GetZkLoginAddressesApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse, SponsorAndExecuteTransactionApiInput, SponsorAndExecuteTransactionApiResponse };
|
|
139
139
|
//# sourceMappingURL=type.d.mts.map
|
package/dist/EnokiFlow.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthProvider,
|
|
1
|
+
import { AuthProvider, EnokiNetwork } from "./EnokiClient/type.mjs";
|
|
2
2
|
import { EnokiClient, EnokiClientConfig } from "./EnokiClient/index.mjs";
|
|
3
3
|
import { Encryption } from "./encryption.mjs";
|
|
4
4
|
import { EnokiKeypair } from "./EnokiKeypair.mjs";
|
|
@@ -70,7 +70,27 @@ declare class EnokiFlow {
|
|
|
70
70
|
network
|
|
71
71
|
}?: {
|
|
72
72
|
network?: EnokiNetwork;
|
|
73
|
-
}): Promise<
|
|
73
|
+
}): Promise<{
|
|
74
|
+
proofPoints: {
|
|
75
|
+
a: Iterable<string> & {
|
|
76
|
+
length: number;
|
|
77
|
+
};
|
|
78
|
+
b: Iterable<Iterable<string> & {
|
|
79
|
+
length: number;
|
|
80
|
+
}> & {
|
|
81
|
+
length: number;
|
|
82
|
+
};
|
|
83
|
+
c: Iterable<string> & {
|
|
84
|
+
length: number;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
issBase64Details: {
|
|
88
|
+
value: string;
|
|
89
|
+
indexMod4: number;
|
|
90
|
+
};
|
|
91
|
+
headerBase64: string;
|
|
92
|
+
addressSeed: string;
|
|
93
|
+
}>;
|
|
74
94
|
getKeypair({
|
|
75
95
|
network
|
|
76
96
|
}?: {
|
package/dist/EnokiFlow.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EnokiFlow.d.mts","names":[],"sources":["../src/EnokiFlow.ts"],"mappings":";;;;;;;;;;AA2BA;;KAAY,eAAA,GAAkB,iBAAA;EAG1B,+BAAA;EAKQ;;;;EAAR,KAAA,GAAQ,SAAA;EALR;;;;EAUA,UAAA,GAAa,UAAA;AAAA;EAGb,KAAA;EACA,UAAA;EAI+B;;AAKnC;EALI,+BAAA;AAAA;AAAA,UAKa,YAAA;EAChB,QAAA,GAAW,YAAA;EACX,OAAA;EACA,IAAA;EACA,SAAA;AAAA;AAAA,UAIgB,cAAA;EAChB,gBAAA;EACA,QAAA;EACA,UAAA;EACA,SAAA;EAEA,GAAA;EACA,KAAA,GAAQ,sBAAA;AAAA;;;;cAWI,SAAA;EAAA;EASZ,eAAA,EAAiB,YAAA;IAAe,WAAA;IAAsB,KAAA,EAAO,cAAA;EAAA;EAC7D,aAAA,EAAe,YAAA,CAAa,YAAA;cAEhB,MAAA,EAAQ,eAAA;EAAA,IA0ChB,WAAA,CAAA,GAAW,WAAA;EAIT,sBAAA,CAAuB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,YAAA;EAAA,IAAmB,OAAA;;;;;;;;;;;;;;;;;;;;;EA6CrD,UAAA,CAAA;IAAa;EAAA;IAAa,OAAA,GAAU,YAAA;EAAA,IAAmB,OAAA,CAAA,YAAA;AAAA"}
|
package/dist/client.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { AuthProvider, EnokiNetwork } from "./EnokiClient/type.mjs";
|
|
2
2
|
import { EnokiClient, EnokiClientConfig, EnokiClientError, isStaleTransactionError } from "./EnokiClient/index.mjs";
|
|
3
|
-
import { EnokiClientExtension, EnokiExtensionOptions, EnokiPrepareSponsoredTransactionOptions, EnokiSponsorAndExecuteTransactionOptions, enoki } from "./extension.mjs";
|
|
4
|
-
export { type AuthProvider, EnokiClient, type EnokiClientConfig, EnokiClientError, EnokiClientExtension, type EnokiExtensionOptions, type EnokiNetwork, type EnokiPrepareSponsoredTransactionOptions, type EnokiSponsorAndExecuteTransactionOptions, enoki, isStaleTransactionError };
|
|
3
|
+
import { EnokiClientExtension, EnokiExtensionOptions, EnokiPrepareSponsoredTransactionOptions, EnokiSponsorAndExecuteTransactionOptions, EnokiSponsorSigner, enoki } from "./extension.mjs";
|
|
4
|
+
export { type AuthProvider, EnokiClient, type EnokiClientConfig, EnokiClientError, EnokiClientExtension, type EnokiExtensionOptions, type EnokiNetwork, type EnokiPrepareSponsoredTransactionOptions, type EnokiSponsorAndExecuteTransactionOptions, type EnokiSponsorSigner, enoki, isStaleTransactionError };
|
package/dist/extension.d.mts
CHANGED
|
@@ -31,6 +31,27 @@ interface EnokiPrepareSponsoredTransactionOptions {
|
|
|
31
31
|
/** The transaction to prepare. Enoki is set as gas owner, paying from its address balance. */
|
|
32
32
|
transaction: Transaction;
|
|
33
33
|
}
|
|
34
|
+
/** A sponsor SDK `RemoteSigner` backed by Enoki: pass it as `createSponsor`'s `signer`. */
|
|
35
|
+
interface EnokiSponsorSigner {
|
|
36
|
+
toSuiAddress(): string;
|
|
37
|
+
signAndExecuteTransaction(options: {
|
|
38
|
+
transaction: Uint8Array;
|
|
39
|
+
additionalSignatures: string[];
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
$kind: 'Rejected';
|
|
43
|
+
issues: Issue[];
|
|
44
|
+
policyIssues: Issue[];
|
|
45
|
+
analysisIssues: Issue[];
|
|
46
|
+
reason: 'POLICY_REJECTED';
|
|
47
|
+
} | SuiClientTypes.TransactionResult<{
|
|
48
|
+
effects: true;
|
|
49
|
+
}>>;
|
|
50
|
+
}
|
|
51
|
+
type Issue = {
|
|
52
|
+
code?: string;
|
|
53
|
+
message: string;
|
|
54
|
+
};
|
|
34
55
|
/**
|
|
35
56
|
* Adds Enoki sponsorship to a Sui client. Requires a private API key with sponsored transactions
|
|
36
57
|
* enabled.
|
|
@@ -60,16 +81,9 @@ declare class EnokiClientExtension {
|
|
|
60
81
|
client: ClientWithCoreApi;
|
|
61
82
|
enoki: EnokiClient;
|
|
62
83
|
});
|
|
63
|
-
/** Enoki's sponsor address on the client's network.
|
|
84
|
+
/** Enoki's sponsor address on the client's network. */
|
|
64
85
|
getSponsorAddress(): Promise<string>;
|
|
65
|
-
/**
|
|
66
|
-
* Sets Enoki as the transaction's gas owner with an empty gas payment.
|
|
67
|
-
* Doesn't build it, so a wallet can still resolve the rest. Build or have a wallet sign it, run
|
|
68
|
-
* any checks, then pass the bytes and signature to `sponsorAndExecuteTransaction`.
|
|
69
|
-
*
|
|
70
|
-
* Safe to call on a transaction that's already prepared. Throws if the transaction already has
|
|
71
|
-
* a different gas owner, or a gas payment.
|
|
72
|
-
*/
|
|
86
|
+
/** Sets Enoki as gas owner with an empty gas payment, without building. */
|
|
73
87
|
prepareSponsoredTransaction({
|
|
74
88
|
transaction
|
|
75
89
|
}: EnokiPrepareSponsoredTransactionOptions): Promise<Transaction>;
|
|
@@ -84,7 +98,18 @@ declare class EnokiClientExtension {
|
|
|
84
98
|
* transaction, sign it and call this again; the sponsor address is fetched again.
|
|
85
99
|
*/
|
|
86
100
|
sponsorAndExecuteTransaction<Include extends SuiClientTypes.TransactionInclude = {}>(options: EnokiSponsorAndExecuteTransactionOptions<Include>): Promise<SuiClientTypes.TransactionResult<Include>>;
|
|
101
|
+
/**
|
|
102
|
+
* A signer that makes Enoki the sponsor in the sponsor SDK's `createSponsor`. Create the sponsor
|
|
103
|
+
* per request. Enoki's `invalid_transaction` is returned as `Rejected`.
|
|
104
|
+
*/
|
|
105
|
+
getSponsorSigner({
|
|
106
|
+
allowedAddresses,
|
|
107
|
+
allowedMoveCallTargets
|
|
108
|
+
}?: {
|
|
109
|
+
allowedAddresses?: string[];
|
|
110
|
+
allowedMoveCallTargets?: string[];
|
|
111
|
+
}): Promise<EnokiSponsorSigner>;
|
|
87
112
|
}
|
|
88
113
|
//#endregion
|
|
89
|
-
export { EnokiClientExtension, EnokiExtensionOptions, EnokiPrepareSponsoredTransactionOptions, EnokiSponsorAndExecuteTransactionOptions, enoki };
|
|
114
|
+
export { EnokiClientExtension, EnokiExtensionOptions, EnokiPrepareSponsoredTransactionOptions, EnokiSponsorAndExecuteTransactionOptions, EnokiSponsorSigner, enoki };
|
|
90
115
|
//# sourceMappingURL=extension.d.mts.map
|
package/dist/extension.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extension.d.mts","names":[],"sources":["../src/extension.ts"],"mappings":";;;;;;KAYY,qBAAA,kCAAuD,IAAA,CAClE,iBAAA;EAGA,IAAA,GAAO,IAAA;AAAA;AAAA,UAGE,iCAAA,iBAAkD,cAAA,CAAe,kBAAA,UAClE,cAAA,CAAe,uBAAA;EAPvB;EASA,gBAAA;EANO;EAQP,sBAAA;EACA,OAAA,GAAU,OAAA,GAAU,cAAA,CAAe,kBAAA;AAAA;AAAA,KAGxB,wCAAA,iBACK,cAAA,CAAe,kBAAA,SAC5B,iCAAA,CAAkC,OAAA;EAjBrC,+FAqBG,WAAA,EAAa,WAAA,EAlBT;EAoBJ,MAAA,EAAQ,MA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AA;EAAA;;IAIE,MAAA;IAAQ;EAAA;IAAW,MAAA,EAAQ,iBAAA;IAAmB,KAAA,EAAO,WAAA;EAAA;EA3CzD;EAiDV,iBAAA,CAAA,GAAqB,OAAA;EA5CJ;EAiDjB,2BAAA,CAAA;IACC;EAAA,GACE,uCAAA,GAA0C,OAAA,CAAQ,WAAA;EA/ClD;;;;;;;;;;EA6DG,4BAAA,iBAA6C,cAAA,CAAe,kBAAA,MAAA,CACjE,OAAA,EAAS,wCAAA,CAAyC,OAAA,IAChD,OAAA,CAAQ,cAAA,CAAe,iBAAA,CAAkB,OAAA;EA3ElB;;;;EAsHpB,gBAAA,CAAA;IACL,gBAAA;IACA;EAAA;IAEA,gBAAA;IACA,sBAAA;EAAA,IACQ,OAAA,CAAQ,kBAAA;AAAA"}
|
package/dist/extension.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { EnokiClient,
|
|
1
|
+
import { EnokiClient, EnokiClientError } from "./EnokiClient/index.mjs";
|
|
2
|
+
import { fromBase64, toBase64 } from "@mysten/sui/utils";
|
|
2
3
|
import { TransactionDataBuilder } from "@mysten/sui/transactions";
|
|
3
|
-
import { fromBase64, normalizeSuiAddress, toBase64 } from "@mysten/sui/utils";
|
|
4
4
|
|
|
5
5
|
//#region src/extension.ts
|
|
6
6
|
/**
|
|
@@ -28,43 +28,20 @@ function enoki({ name = "enoki", ...config }) {
|
|
|
28
28
|
var EnokiClientExtension = class {
|
|
29
29
|
#client;
|
|
30
30
|
#enoki;
|
|
31
|
-
#sponsorAddress;
|
|
32
31
|
constructor({ client, enoki: enoki$1 }) {
|
|
33
32
|
this.#client = client;
|
|
34
33
|
this.#enoki = enoki$1;
|
|
35
34
|
}
|
|
36
|
-
/** Enoki's sponsor address on the client's network.
|
|
35
|
+
/** Enoki's sponsor address on the client's network. */
|
|
37
36
|
getSponsorAddress() {
|
|
38
|
-
|
|
39
|
-
const network = this.#network();
|
|
40
|
-
const promise = this.#enoki.getApp().then(({ sponsorAddresses }) => {
|
|
41
|
-
const address = sponsorAddresses?.[network];
|
|
42
|
-
if (!address) throw new Error(`Enoki does not sponsor transactions on ${network}`);
|
|
43
|
-
return normalizeSuiAddress(address);
|
|
44
|
-
});
|
|
45
|
-
this.#sponsorAddress = promise;
|
|
46
|
-
promise.catch(() => {
|
|
47
|
-
if (this.#sponsorAddress === promise) this.#sponsorAddress = void 0;
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
return this.#sponsorAddress;
|
|
37
|
+
return this.#enoki.getSponsorAddress({ network: this.#network() });
|
|
51
38
|
}
|
|
52
|
-
/**
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* a different gas owner, or a gas payment.
|
|
59
|
-
*/
|
|
60
|
-
async prepareSponsoredTransaction({ transaction }) {
|
|
61
|
-
const sponsorAddress = await this.getSponsorAddress();
|
|
62
|
-
const data = transaction.getData();
|
|
63
|
-
if (data.gasData.owner && normalizeSuiAddress(data.gasData.owner) !== sponsorAddress) throw new Error(`Transaction gas owner is ${data.gasData.owner}, not Enoki's sponsor ${sponsorAddress}`);
|
|
64
|
-
if (data.gasData.payment && data.gasData.payment.length > 0) throw new Error("Sponsored transactions pay gas from the sponsor; remove the gas payment");
|
|
65
|
-
transaction.setGasOwner(sponsorAddress);
|
|
66
|
-
transaction.setGasPayment([]);
|
|
67
|
-
return transaction;
|
|
39
|
+
/** Sets Enoki as gas owner with an empty gas payment, without building. */
|
|
40
|
+
prepareSponsoredTransaction({ transaction }) {
|
|
41
|
+
return this.#enoki.prepareSponsoredTransaction({
|
|
42
|
+
transaction,
|
|
43
|
+
network: this.#network()
|
|
44
|
+
});
|
|
68
45
|
}
|
|
69
46
|
/**
|
|
70
47
|
* Has Enoki sponsor and execute a transaction, and waits for it. Returns the same result as
|
|
@@ -92,20 +69,14 @@ var EnokiClientExtension = class {
|
|
|
92
69
|
}
|
|
93
70
|
const transactionBytes = typeof bytes === "string" ? fromBase64(bytes) : bytes;
|
|
94
71
|
const digest = TransactionDataBuilder.getDigestFromBytes(transactionBytes);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
allowedMoveCallTargets: options.allowedMoveCallTargets
|
|
104
|
-
});
|
|
105
|
-
} catch (error) {
|
|
106
|
-
if (isStaleTransactionError(error)) this.#sponsorAddress = void 0;
|
|
107
|
-
throw error;
|
|
108
|
-
}
|
|
72
|
+
options.signal?.throwIfAborted();
|
|
73
|
+
const response = await this.#enoki.sponsorAndExecuteTransaction({
|
|
74
|
+
network,
|
|
75
|
+
transactionBytes: toBase64(transactionBytes),
|
|
76
|
+
signature,
|
|
77
|
+
allowedAddresses: options.allowedAddresses,
|
|
78
|
+
allowedMoveCallTargets: options.allowedMoveCallTargets
|
|
79
|
+
});
|
|
109
80
|
if (response.digest !== digest) throw new Error(`Enoki returned digest ${response.digest}, but the transaction is ${digest}`);
|
|
110
81
|
return this.#client.core.waitForTransaction({
|
|
111
82
|
digest,
|
|
@@ -113,6 +84,44 @@ var EnokiClientExtension = class {
|
|
|
113
84
|
signal: options.signal
|
|
114
85
|
});
|
|
115
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* A signer that makes Enoki the sponsor in the sponsor SDK's `createSponsor`. Create the sponsor
|
|
89
|
+
* per request. Enoki's `invalid_transaction` is returned as `Rejected`.
|
|
90
|
+
*/
|
|
91
|
+
async getSponsorSigner({ allowedAddresses, allowedMoveCallTargets } = {}) {
|
|
92
|
+
const address = await this.getSponsorAddress();
|
|
93
|
+
return {
|
|
94
|
+
toSuiAddress: () => address,
|
|
95
|
+
signAndExecuteTransaction: async ({ transaction, additionalSignatures, signal }) => {
|
|
96
|
+
if (additionalSignatures.length !== 1) throw new Error("Enoki sponsorship takes exactly one user signature.");
|
|
97
|
+
try {
|
|
98
|
+
return await this.sponsorAndExecuteTransaction({
|
|
99
|
+
transaction,
|
|
100
|
+
signature: additionalSignatures[0],
|
|
101
|
+
include: { effects: true },
|
|
102
|
+
signal,
|
|
103
|
+
allowedAddresses,
|
|
104
|
+
allowedMoveCallTargets
|
|
105
|
+
});
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error instanceof EnokiClientError && error.code === "invalid_transaction") {
|
|
108
|
+
const issues = error.errors.map(({ code, message }) => ({
|
|
109
|
+
code,
|
|
110
|
+
message
|
|
111
|
+
}));
|
|
112
|
+
return {
|
|
113
|
+
$kind: "Rejected",
|
|
114
|
+
issues,
|
|
115
|
+
policyIssues: issues,
|
|
116
|
+
analysisIssues: [],
|
|
117
|
+
reason: "POLICY_REJECTED"
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
116
125
|
#network() {
|
|
117
126
|
const network = this.#client.network;
|
|
118
127
|
if (network === "mainnet" || network === "testnet") return network;
|
package/dist/extension.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extension.mjs","names":["#client","#enoki","enoki","#sponsorAddress","#network"],"sources":["../src/extension.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';\nimport type { Signer } from '@mysten/sui/cryptography';\nimport { TransactionDataBuilder, type Transaction } from '@mysten/sui/transactions';\nimport { fromBase64, normalizeSuiAddress, toBase64 } from '@mysten/sui/utils';\n\nimport type { EnokiClientConfig } from './EnokiClient/index.js';\nimport { EnokiClient, isStaleTransactionError } from './EnokiClient/index.js';\nimport type { EnokiDomainNetwork } from './EnokiClient/type.js';\n\nexport type EnokiExtensionOptions<Name extends string = 'enoki'> = Omit<\n\tEnokiClientConfig,\n\t'additionalEpochs'\n> & {\n\tname?: Name;\n};\n\ninterface EnokiSponsorAndExecuteBaseOptions<Include extends SuiClientTypes.TransactionInclude>\n\textends SuiClientTypes.CoreClientMethodOptions {\n\t/** Addresses the transaction may use, in addition to the app's allowlist in the Enoki Portal. */\n\tallowedAddresses?: string[];\n\t/** Move call targets the transaction may call, in addition to the app's allowlist in the Enoki Portal. */\n\tallowedMoveCallTargets?: string[];\n\tinclude?: Include & SuiClientTypes.TransactionInclude;\n}\n\nexport type EnokiSponsorAndExecuteTransactionOptions<\n\tInclude extends SuiClientTypes.TransactionInclude = {},\n> = EnokiSponsorAndExecuteBaseOptions<Include> &\n\t(\n\t\t| {\n\t\t\t\t/** The transaction to send. Enoki is set as gas owner, and the sender defaults to `signer`. */\n\t\t\t\ttransaction: Transaction;\n\t\t\t\t/** Signs as the transaction sender. */\n\t\t\t\tsigner: Signer;\n\t\t\t\tsignature?: never;\n\t\t }\n\t\t| {\n\t\t\t\t/**\n\t\t\t\t * Built transaction bytes (or base64) of a transaction prepared with\n\t\t\t\t * `prepareSponsoredTransaction`.\n\t\t\t\t */\n\t\t\t\ttransaction: Uint8Array | string;\n\t\t\t\t/** The sender's signature over the bytes. */\n\t\t\t\tsignature: string;\n\t\t\t\tsigner?: never;\n\t\t }\n\t);\n\nexport interface EnokiPrepareSponsoredTransactionOptions {\n\t/** The transaction to prepare. Enoki is set as gas owner, paying from its address balance. */\n\ttransaction: Transaction;\n}\n\n/**\n * Adds Enoki sponsorship to a Sui client. Requires a private API key with sponsored transactions\n * enabled.\n *\n * @example\n * ```ts\n * const client = new SuiGrpcClient({ network: 'mainnet', baseUrl }).$extend(\n * enoki({ apiKey: process.env.ENOKI_PRIVATE_KEY! }),\n * );\n *\n * const result = await client.enoki.sponsorAndExecuteTransaction({ transaction, signer });\n * ```\n */\nexport function enoki<const Name extends string = 'enoki'>({\n\tname = 'enoki' as Name,\n\t...config\n}: EnokiExtensionOptions<Name>) {\n\treturn {\n\t\tname,\n\t\tregister: (client: ClientWithCoreApi) =>\n\t\t\tnew EnokiClientExtension({ client, enoki: new EnokiClient(config) }),\n\t};\n}\n\nexport class EnokiClientExtension {\n\t#client: ClientWithCoreApi;\n\t#enoki: EnokiClient;\n\t#sponsorAddress?: Promise<string>;\n\n\tconstructor({ client, enoki }: { client: ClientWithCoreApi; enoki: EnokiClient }) {\n\t\tthis.#client = client;\n\t\tthis.#enoki = enoki;\n\t}\n\n\t/** Enoki's sponsor address on the client's network. Cached until a transaction is rejected as stale. */\n\tgetSponsorAddress(): Promise<string> {\n\t\tif (!this.#sponsorAddress) {\n\t\t\tconst network = this.#network();\n\t\t\tconst promise = this.#enoki.getApp().then(({ sponsorAddresses }) => {\n\t\t\t\tconst address = sponsorAddresses?.[network];\n\t\t\t\tif (!address) {\n\t\t\t\t\tthrow new Error(`Enoki does not sponsor transactions on ${network}`);\n\t\t\t\t}\n\t\t\t\treturn normalizeSuiAddress(address);\n\t\t\t});\n\t\t\tthis.#sponsorAddress = promise;\n\t\t\tpromise.catch(() => {\n\t\t\t\tif (this.#sponsorAddress === promise) {\n\t\t\t\t\tthis.#sponsorAddress = undefined;\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t\treturn this.#sponsorAddress;\n\t}\n\n\t/**\n\t * Sets Enoki as the transaction's gas owner with an empty gas payment.\n\t * Doesn't build it, so a wallet can still resolve the rest. Build or have a wallet sign it, run\n\t * any checks, then pass the bytes and signature to `sponsorAndExecuteTransaction`.\n\t *\n\t * Safe to call on a transaction that's already prepared. Throws if the transaction already has\n\t * a different gas owner, or a gas payment.\n\t */\n\tasync prepareSponsoredTransaction({\n\t\ttransaction,\n\t}: EnokiPrepareSponsoredTransactionOptions): Promise<Transaction> {\n\t\tconst sponsorAddress = await this.getSponsorAddress();\n\t\tconst data = transaction.getData();\n\n\t\tif (data.gasData.owner && normalizeSuiAddress(data.gasData.owner) !== sponsorAddress) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction gas owner is ${data.gasData.owner}, not Enoki's sponsor ${sponsorAddress}`,\n\t\t\t);\n\t\t}\n\t\tif (data.gasData.payment && data.gasData.payment.length > 0) {\n\t\t\tthrow new Error('Sponsored transactions pay gas from the sponsor; remove the gas payment');\n\t\t}\n\n\t\ttransaction.setGasOwner(sponsorAddress);\n\t\ttransaction.setGasPayment([]);\n\t\treturn transaction;\n\t}\n\n\t/**\n\t * Has Enoki sponsor and execute a transaction, and waits for it. Returns the same result as\n\t * `client.core.signAndExecuteTransaction`.\n\t *\n\t * With a `signer`, the transaction is prepared with `prepareSponsoredTransaction`, built and\n\t * signed here. With a `signature`, the bytes are sent as they are.\n\t *\n\t * Enoki errors are thrown as `EnokiClientError`. If `isStaleTransactionError(error)`, build a new\n\t * transaction, sign it and call this again; the sponsor address is fetched again.\n\t */\n\tasync sponsorAndExecuteTransaction<Include extends SuiClientTypes.TransactionInclude = {}>(\n\t\toptions: EnokiSponsorAndExecuteTransactionOptions<Include>,\n\t): Promise<SuiClientTypes.TransactionResult<Include>> {\n\t\tconst network = this.#network();\n\n\t\tlet bytes: Uint8Array | string;\n\t\tlet signature: string;\n\t\tif (options.signer) {\n\t\t\tconst { transaction, signer } = options;\n\t\t\ttransaction.setSenderIfNotSet(signer.toSuiAddress());\n\t\t\tawait this.prepareSponsoredTransaction({ transaction });\n\t\t\tbytes = await transaction.build({ client: this.#client });\n\t\t\t({ signature } = await signer.signTransaction(bytes));\n\t\t} else {\n\t\t\tbytes = options.transaction;\n\t\t\tsignature = options.signature;\n\t\t}\n\n\t\tconst transactionBytes = typeof bytes === 'string' ? fromBase64(bytes) : bytes;\n\t\t// Wait on the digest of the bytes that were signed, not one reported by the server.\n\t\tconst digest = TransactionDataBuilder.getDigestFromBytes(transactionBytes);\n\n\t\tlet response: { digest: string };\n\t\ttry {\n\t\t\toptions.signal?.throwIfAborted();\n\t\t\tresponse = await this.#enoki.sponsorAndExecuteTransaction({\n\t\t\t\tnetwork,\n\t\t\t\ttransactionBytes: toBase64(transactionBytes),\n\t\t\t\tsignature,\n\t\t\t\tallowedAddresses: options.allowedAddresses,\n\t\t\t\tallowedMoveCallTargets: options.allowedMoveCallTargets,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tif (isStaleTransactionError(error)) {\n\t\t\t\tthis.#sponsorAddress = undefined;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t\tif (response.digest !== digest) {\n\t\t\tthrow new Error(`Enoki returned digest ${response.digest}, but the transaction is ${digest}`);\n\t\t}\n\n\t\treturn this.#client.core.waitForTransaction({\n\t\t\tdigest,\n\t\t\tinclude: options.include,\n\t\t\tsignal: options.signal,\n\t\t});\n\t}\n\n\t#network(): EnokiDomainNetwork {\n\t\tconst network: string = this.#client.network;\n\t\tif (network === 'mainnet' || network === 'testnet') {\n\t\t\treturn network;\n\t\t}\n\t\tthrow new Error(`Enoki sponsorship is not available on ${network}`);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqEA,SAAgB,MAA2C,EAC1D,OAAO,SACP,GAAG,UAC4B;AAC/B,QAAO;EACN;EACA,WAAW,WACV,IAAI,qBAAqB;GAAE;GAAQ,OAAO,IAAI,YAAY,OAAO;GAAE,CAAC;EACrE;;AAGF,IAAa,uBAAb,MAAkC;CACjC;CACA;CACA;CAEA,YAAY,EAAE,QAAQ,kBAA4D;AACjF,QAAKA,SAAU;AACf,QAAKC,QAASC;;;CAIf,oBAAqC;AACpC,MAAI,CAAC,MAAKC,gBAAiB;GAC1B,MAAM,UAAU,MAAKC,SAAU;GAC/B,MAAM,UAAU,MAAKH,MAAO,QAAQ,CAAC,MAAM,EAAE,uBAAuB;IACnE,MAAM,UAAU,mBAAmB;AACnC,QAAI,CAAC,QACJ,OAAM,IAAI,MAAM,0CAA0C,UAAU;AAErE,WAAO,oBAAoB,QAAQ;KAClC;AACF,SAAKE,iBAAkB;AACvB,WAAQ,YAAY;AACnB,QAAI,MAAKA,mBAAoB,QAC5B,OAAKA,iBAAkB;KAEvB;;AAEH,SAAO,MAAKA;;;;;;;;;;CAWb,MAAM,4BAA4B,EACjC,eACiE;EACjE,MAAM,iBAAiB,MAAM,KAA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QAAQ;GACjB,QAAQ,QAAQ;GAChB,CAAC;;CAGH,WAA+B;EAC9B,MAAM,UAAkB,MAAKA,OAAQ;AACrC,MAAI,YAAY,aAAa,YAAY,UACxC,QAAO;AAER,QAAM,IAAI,MAAM,yCAAyC,UAAU"}
|
|
1
|
+
{"version":3,"file":"extension.mjs","names":["#client","#enoki","enoki","#network"],"sources":["../src/extension.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';\nimport type { Signer } from '@mysten/sui/cryptography';\nimport { TransactionDataBuilder, type Transaction } from '@mysten/sui/transactions';\nimport { fromBase64, toBase64 } from '@mysten/sui/utils';\n\nimport type { EnokiClientConfig } from './EnokiClient/index.js';\nimport { EnokiClient, EnokiClientError } from './EnokiClient/index.js';\nimport type { EnokiDomainNetwork } from './EnokiClient/type.js';\n\nexport type EnokiExtensionOptions<Name extends string = 'enoki'> = Omit<\n\tEnokiClientConfig,\n\t'additionalEpochs'\n> & {\n\tname?: Name;\n};\n\ninterface EnokiSponsorAndExecuteBaseOptions<Include extends SuiClientTypes.TransactionInclude>\n\textends SuiClientTypes.CoreClientMethodOptions {\n\t/** Addresses the transaction may use, in addition to the app's allowlist in the Enoki Portal. */\n\tallowedAddresses?: string[];\n\t/** Move call targets the transaction may call, in addition to the app's allowlist in the Enoki Portal. */\n\tallowedMoveCallTargets?: string[];\n\tinclude?: Include & SuiClientTypes.TransactionInclude;\n}\n\nexport type EnokiSponsorAndExecuteTransactionOptions<\n\tInclude extends SuiClientTypes.TransactionInclude = {},\n> = EnokiSponsorAndExecuteBaseOptions<Include> &\n\t(\n\t\t| {\n\t\t\t\t/** The transaction to send. Enoki is set as gas owner, and the sender defaults to `signer`. */\n\t\t\t\ttransaction: Transaction;\n\t\t\t\t/** Signs as the transaction sender. */\n\t\t\t\tsigner: Signer;\n\t\t\t\tsignature?: never;\n\t\t }\n\t\t| {\n\t\t\t\t/**\n\t\t\t\t * Built transaction bytes (or base64) of a transaction prepared with\n\t\t\t\t * `prepareSponsoredTransaction`.\n\t\t\t\t */\n\t\t\t\ttransaction: Uint8Array | string;\n\t\t\t\t/** The sender's signature over the bytes. */\n\t\t\t\tsignature: string;\n\t\t\t\tsigner?: never;\n\t\t }\n\t);\n\nexport interface EnokiPrepareSponsoredTransactionOptions {\n\t/** The transaction to prepare. Enoki is set as gas owner, paying from its address balance. */\n\ttransaction: Transaction;\n}\n\n/** A sponsor SDK `RemoteSigner` backed by Enoki: pass it as `createSponsor`'s `signer`. */\nexport interface EnokiSponsorSigner {\n\ttoSuiAddress(): string;\n\tsignAndExecuteTransaction(options: {\n\t\ttransaction: Uint8Array;\n\t\tadditionalSignatures: string[];\n\t\tsignal?: AbortSignal;\n\t}): Promise<\n\t\t| {\n\t\t\t\t$kind: 'Rejected';\n\t\t\t\tissues: Issue[];\n\t\t\t\tpolicyIssues: Issue[];\n\t\t\t\tanalysisIssues: Issue[];\n\t\t\t\treason: 'POLICY_REJECTED';\n\t\t }\n\t\t| SuiClientTypes.TransactionResult<{ effects: true }>\n\t>;\n}\n\ntype Issue = { code?: string; message: string };\n\n/**\n * Adds Enoki sponsorship to a Sui client. Requires a private API key with sponsored transactions\n * enabled.\n *\n * @example\n * ```ts\n * const client = new SuiGrpcClient({ network: 'mainnet', baseUrl }).$extend(\n * enoki({ apiKey: process.env.ENOKI_PRIVATE_KEY! }),\n * );\n *\n * const result = await client.enoki.sponsorAndExecuteTransaction({ transaction, signer });\n * ```\n */\nexport function enoki<const Name extends string = 'enoki'>({\n\tname = 'enoki' as Name,\n\t...config\n}: EnokiExtensionOptions<Name>) {\n\treturn {\n\t\tname,\n\t\tregister: (client: ClientWithCoreApi) =>\n\t\t\tnew EnokiClientExtension({ client, enoki: new EnokiClient(config) }),\n\t};\n}\n\nexport class EnokiClientExtension {\n\t#client: ClientWithCoreApi;\n\t#enoki: EnokiClient;\n\n\tconstructor({ client, enoki }: { client: ClientWithCoreApi; enoki: EnokiClient }) {\n\t\tthis.#client = client;\n\t\tthis.#enoki = enoki;\n\t}\n\n\t/** Enoki's sponsor address on the client's network. */\n\tgetSponsorAddress(): Promise<string> {\n\t\treturn this.#enoki.getSponsorAddress({ network: this.#network() });\n\t}\n\n\t/** Sets Enoki as gas owner with an empty gas payment, without building. */\n\tprepareSponsoredTransaction({\n\t\ttransaction,\n\t}: EnokiPrepareSponsoredTransactionOptions): Promise<Transaction> {\n\t\treturn this.#enoki.prepareSponsoredTransaction({ transaction, network: this.#network() });\n\t}\n\n\t/**\n\t * Has Enoki sponsor and execute a transaction, and waits for it. Returns the same result as\n\t * `client.core.signAndExecuteTransaction`.\n\t *\n\t * With a `signer`, the transaction is prepared with `prepareSponsoredTransaction`, built and\n\t * signed here. With a `signature`, the bytes are sent as they are.\n\t *\n\t * Enoki errors are thrown as `EnokiClientError`. If `isStaleTransactionError(error)`, build a new\n\t * transaction, sign it and call this again; the sponsor address is fetched again.\n\t */\n\tasync sponsorAndExecuteTransaction<Include extends SuiClientTypes.TransactionInclude = {}>(\n\t\toptions: EnokiSponsorAndExecuteTransactionOptions<Include>,\n\t): Promise<SuiClientTypes.TransactionResult<Include>> {\n\t\tconst network = this.#network();\n\n\t\tlet bytes: Uint8Array | string;\n\t\tlet signature: string;\n\t\tif (options.signer) {\n\t\t\tconst { transaction, signer } = options;\n\t\t\ttransaction.setSenderIfNotSet(signer.toSuiAddress());\n\t\t\tawait this.prepareSponsoredTransaction({ transaction });\n\t\t\tbytes = await transaction.build({ client: this.#client });\n\t\t\t({ signature } = await signer.signTransaction(bytes));\n\t\t} else {\n\t\t\tbytes = options.transaction;\n\t\t\tsignature = options.signature;\n\t\t}\n\n\t\tconst transactionBytes = typeof bytes === 'string' ? fromBase64(bytes) : bytes;\n\t\t// Wait on the digest of the bytes that were signed, not one reported by the server.\n\t\tconst digest = TransactionDataBuilder.getDigestFromBytes(transactionBytes);\n\n\t\toptions.signal?.throwIfAborted();\n\t\tconst response = await this.#enoki.sponsorAndExecuteTransaction({\n\t\t\tnetwork,\n\t\t\ttransactionBytes: toBase64(transactionBytes),\n\t\t\tsignature,\n\t\t\tallowedAddresses: options.allowedAddresses,\n\t\t\tallowedMoveCallTargets: options.allowedMoveCallTargets,\n\t\t});\n\t\tif (response.digest !== digest) {\n\t\t\tthrow new Error(`Enoki returned digest ${response.digest}, but the transaction is ${digest}`);\n\t\t}\n\n\t\treturn this.#client.core.waitForTransaction({\n\t\t\tdigest,\n\t\t\tinclude: options.include,\n\t\t\tsignal: options.signal,\n\t\t});\n\t}\n\n\t/**\n\t * A signer that makes Enoki the sponsor in the sponsor SDK's `createSponsor`. Create the sponsor\n\t * per request. Enoki's `invalid_transaction` is returned as `Rejected`.\n\t */\n\tasync getSponsorSigner({\n\t\tallowedAddresses,\n\t\tallowedMoveCallTargets,\n\t}: {\n\t\tallowedAddresses?: string[];\n\t\tallowedMoveCallTargets?: string[];\n\t} = {}): Promise<EnokiSponsorSigner> {\n\t\tconst address = await this.getSponsorAddress();\n\t\treturn {\n\t\t\ttoSuiAddress: () => address,\n\t\t\tsignAndExecuteTransaction: async ({ transaction, additionalSignatures, signal }) => {\n\t\t\t\tif (additionalSignatures.length !== 1) {\n\t\t\t\t\tthrow new Error('Enoki sponsorship takes exactly one user signature.');\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\treturn await this.sponsorAndExecuteTransaction({\n\t\t\t\t\t\ttransaction,\n\t\t\t\t\t\tsignature: additionalSignatures[0],\n\t\t\t\t\t\tinclude: { effects: true },\n\t\t\t\t\t\tsignal,\n\t\t\t\t\t\tallowedAddresses,\n\t\t\t\t\t\tallowedMoveCallTargets,\n\t\t\t\t\t});\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof EnokiClientError && error.code === 'invalid_transaction') {\n\t\t\t\t\t\tconst issues = error.errors.map(({ code, message }) => ({ code, message }));\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t$kind: 'Rejected',\n\t\t\t\t\t\t\tissues,\n\t\t\t\t\t\t\tpolicyIssues: issues,\n\t\t\t\t\t\t\tanalysisIssues: [],\n\t\t\t\t\t\t\treason: 'POLICY_REJECTED',\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t}\n\n\t#network(): EnokiDomainNetwork {\n\t\tconst network: string = this.#client.network;\n\t\tif (network === 'mainnet' || network === 'testnet') {\n\t\t\treturn network;\n\t\t}\n\t\tthrow new Error(`Enoki sponsorship is not available on ${network}`);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA0FA,SAAgB,MAA2C,EAC1D,OAAO,SACP,GAAG,UAC4B;AAC/B,QAAO;EACN;EACA,WAAW,WACV,IAAI,qBAAqB;GAAE;GAAQ,OAAO,IAAI,YAAY,OAAO;GAAE,CAAC;EACrE;;AAGF,IAAa,uBAAb,MAAkC;CACjC;CACA;CAEA,YAAY,EAAE,QAAQ,kBAA4D;AACjF,QAAKA,SAAU;AACf,QAAKC,QAASC;;;CAIf,oBAAqC;AACpC,SAAO,MAAKD,MAAO,kBAAkB,EAAE,SAAS,MAAKE,SAAU,EAAE,CAAC;;;CAInE,4BAA4B,EAC3B,eACiE;AACjE,SAAO,MAAKF,MAAO,4BAA4B;GAAE;GAAa,SAAS,MAAKE,SAAU;GAAE,CAAC;;;;;;;;;;;;CAa1F,MAAM,6BACL,SACqD;EACrD,MAAM,UAAU,MAAKA,SAAU;EAE/B,IAAI;EACJ,IAAI;AACJ,MAAI,QAAQ,QAAQ;GACnB,MAAM,EAAE,aAAa,WAAW;AAChC,eAAY,kBAAkB,OAAO,cAAc,CAAC;AACpD,SAAM,KAAK,4BAA4B,EAAE,aAAa,CAAC;AACvD,WAAQ,MAAM,YAAY,MAAM,EAAE,QAAQ,MAAKH,QAAS,CAAC;AACzD,IAAC,CAAE,aAAc,MAAM,OAAO,gBAAgB,MAAM;SAC9C;AACN,WAAQ,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GED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AuthProvider, EnokiNetwork } from "./EnokiClient/type.mjs";
|
|
2
2
|
import { EnokiClient, EnokiClientConfig, EnokiClientError, isStaleTransactionError } from "./EnokiClient/index.mjs";
|
|
3
|
-
import { EnokiClientExtension, EnokiExtensionOptions, EnokiPrepareSponsoredTransactionOptions, EnokiSponsorAndExecuteTransactionOptions, enoki } from "./extension.mjs";
|
|
3
|
+
import { EnokiClientExtension, EnokiExtensionOptions, EnokiPrepareSponsoredTransactionOptions, EnokiSponsorAndExecuteTransactionOptions, EnokiSponsorSigner, enoki } from "./extension.mjs";
|
|
4
4
|
import "./client.mjs";
|
|
5
5
|
import { Encryption, createDefaultEncryption } from "./encryption.mjs";
|
|
6
6
|
import { EnokiKeypair, EnokiPublicKey } from "./EnokiKeypair.mjs";
|
|
@@ -12,4 +12,4 @@ import { registerEnokiWallets } from "./wallet/register.mjs";
|
|
|
12
12
|
import { enokiWalletsInitializer } from "./wallet/initializer.mjs";
|
|
13
13
|
import { getSession, getWalletMetadata, isEnokiWallet, isFacebookWallet, isGoogleWallet, isTwitchWallet } from "./wallet/utils.mjs";
|
|
14
14
|
import { isEnokiNetwork } from "./utils.mjs";
|
|
15
|
-
export { AuthProvider, type Encryption, EnokiClient, EnokiClientConfig, EnokiClientError, EnokiClientExtension, EnokiExtensionOptions, EnokiFlow, type EnokiFlowConfig, EnokiKeypair, EnokiNetwork, EnokiPrepareSponsoredTransactionOptions, EnokiPublicKey, EnokiSponsorAndExecuteTransactionOptions, type EnokiWallet, type RegisterEnokiWalletsOptions, type SyncStore, createDefaultEncryption, createInMemoryStorage, createLocalStorage, createSessionStorage, enoki, enokiWalletsInitializer, getSession, getWalletMetadata, isEnokiNetwork, isEnokiWallet, isFacebookWallet, isGoogleWallet, isStaleTransactionError, isTwitchWallet, registerEnokiWallets };
|
|
15
|
+
export { AuthProvider, type Encryption, EnokiClient, EnokiClientConfig, EnokiClientError, EnokiClientExtension, EnokiExtensionOptions, EnokiFlow, type EnokiFlowConfig, EnokiKeypair, EnokiNetwork, EnokiPrepareSponsoredTransactionOptions, EnokiPublicKey, EnokiSponsorAndExecuteTransactionOptions, EnokiSponsorSigner, type EnokiWallet, type RegisterEnokiWalletsOptions, type SyncStore, createDefaultEncryption, createInMemoryStorage, createLocalStorage, createSessionStorage, enoki, enokiWalletsInitializer, getSession, getWalletMetadata, isEnokiNetwork, isEnokiWallet, isFacebookWallet, isGoogleWallet, isStaleTransactionError, isTwitchWallet, registerEnokiWallets };
|
package/dist/wallet/wallet.mjs
CHANGED
|
@@ -2,8 +2,8 @@ import { EnokiClient } from "../EnokiClient/index.mjs";
|
|
|
2
2
|
import { EnokiKeypair } from "../EnokiKeypair.mjs";
|
|
3
3
|
import { EnokiGetMetadata, EnokiGetSession } from "./features.mjs";
|
|
4
4
|
import { EnokiWalletState } from "./state.mjs";
|
|
5
|
-
import { Transaction } from "@mysten/sui/transactions";
|
|
6
5
|
import { fromBase64, toBase64 } from "@mysten/sui/utils";
|
|
6
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
7
7
|
import { WebCryptoSigner } from "@mysten/webcrypto-signer";
|
|
8
8
|
import { decodeJwt } from "@mysten/sui/zklogin";
|
|
9
9
|
import { get, set } from "idb-keyval";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mysten/enoki",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "zkLogin authentication and sponsored transactions",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Mysten Labs <build@mystenlabs.com>",
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"@wallet-standard/ui": "^1.0.3",
|
|
51
51
|
"idb-keyval": "^6.3.0",
|
|
52
52
|
"nanostores": "^1.4.2",
|
|
53
|
-
"@mysten/utils": "^0.4.2",
|
|
54
53
|
"@mysten/wallet-standard": "^0.21.38",
|
|
54
|
+
"@mysten/utils": "^0.4.2",
|
|
55
55
|
"@mysten/webcrypto-signer": "^0.2.38"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|