@mysten/enoki 0.0.2 → 0.0.3
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 +6 -0
- package/dist/cjs/EnokiClient/index.d.ts +2 -1
- package/dist/cjs/EnokiClient/index.js +13 -1
- package/dist/cjs/EnokiClient/index.js.map +2 -2
- package/dist/cjs/EnokiClient/type.d.ts +12 -0
- package/dist/cjs/EnokiClient/type.js.map +1 -1
- package/dist/cjs/EnokiFlow.d.ts +7 -0
- package/dist/cjs/EnokiFlow.js +46 -12
- package/dist/cjs/EnokiFlow.js.map +2 -2
- package/dist/cjs/react.d.ts +1 -0
- package/dist/cjs/react.js +25 -0
- package/dist/cjs/react.js.map +2 -2
- package/dist/esm/EnokiClient/index.d.ts +2 -1
- package/dist/esm/EnokiClient/index.js +13 -1
- package/dist/esm/EnokiClient/index.js.map +2 -2
- package/dist/esm/EnokiClient/type.d.ts +12 -0
- package/dist/esm/EnokiFlow.d.ts +7 -0
- package/dist/esm/EnokiFlow.js +47 -13
- package/dist/esm/EnokiFlow.js.map +2 -2
- package/dist/esm/react.d.ts +1 -0
- package/dist/esm/react.js +26 -1
- package/dist/esm/react.js.map +2 -2
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -3
- package/src/EnokiClient/index.ts +0 -97
- package/src/EnokiClient/type.ts +0 -42
- package/src/EnokiFlow.ts +0 -308
- package/src/EnokiKeypair.ts +0 -98
- package/src/encryption.ts +0 -105
- package/src/index.ts +0 -13
- package/src/react.tsx +0 -33
- package/src/stores.ts +0 -68
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, GetAppApiInput, GetAppApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse } from './type.js';
|
|
1
|
+
import type { CreateSponsoredTransactionBlockApiInput, CreateSponsoredTransactionBlockApiResponse, CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, GetAppApiInput, GetAppApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse } from './type.js';
|
|
2
2
|
export interface EnokiClientConfig {
|
|
3
3
|
/** The API key for the Enoki app, available in the Enoki Portal. */
|
|
4
4
|
apiKey: string;
|
|
@@ -15,4 +15,5 @@ export declare class EnokiClient {
|
|
|
15
15
|
getZkLogin(input: GetZkLoginApiInput): Promise<GetZkLoginApiResponse>;
|
|
16
16
|
createZkLoginNonce(input: CreateZkLoginNonceApiInput): Promise<CreateZkLoginNonceApiResponse>;
|
|
17
17
|
createZkLoginZkp(input: CreateZkLoginZkpApiInput): Promise<CreateZkLoginZkpApiResponse>;
|
|
18
|
+
createSponsoredTransactionBlock(input: CreateSponsoredTransactionBlockApiInput): Promise<CreateSponsoredTransactionBlockApiResponse>;
|
|
18
19
|
}
|
|
@@ -78,7 +78,7 @@ class EnokiClient {
|
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
80
|
createZkLoginZkp(input) {
|
|
81
|
-
return __privateMethod(this, _fetch, fetch_fn).call(this, "zklogin/
|
|
81
|
+
return __privateMethod(this, _fetch, fetch_fn).call(this, "zklogin/zkp", {
|
|
82
82
|
method: "POST",
|
|
83
83
|
headers: {
|
|
84
84
|
[ZKLOGIN_HEADER]: input.jwt
|
|
@@ -90,6 +90,18 @@ class EnokiClient {
|
|
|
90
90
|
})
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
|
+
createSponsoredTransactionBlock(input) {
|
|
94
|
+
return __privateMethod(this, _fetch, fetch_fn).call(this, "transaction-blocks/sponsor", {
|
|
95
|
+
method: "POST",
|
|
96
|
+
headers: {
|
|
97
|
+
[ZKLOGIN_HEADER]: input.jwt
|
|
98
|
+
},
|
|
99
|
+
body: JSON.stringify({
|
|
100
|
+
network: input.network,
|
|
101
|
+
transactionBlockKindBytes: input.transactionBlockKindBytes
|
|
102
|
+
})
|
|
103
|
+
});
|
|
104
|
+
}
|
|
93
105
|
}
|
|
94
106
|
_version = new WeakMap();
|
|
95
107
|
_apiUrl = new WeakMap();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/EnokiClient/index.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type {\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\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\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\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}\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\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\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateZkLoginZkp(input: CreateZkLoginZkpApiInput) {\n\t\treturn this.#fetch<CreateZkLoginZkpApiResponse>('zklogin/
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type {\n\tCreateSponsoredTransactionBlockApiInput,\n\tCreateSponsoredTransactionBlockApiResponse,\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\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\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\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}\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\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\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\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\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\tcreateSponsoredTransactionBlock(input: CreateSponsoredTransactionBlockApiInput) {\n\t\treturn this.#fetch<CreateSponsoredTransactionBlockApiResponse>('transaction-blocks/sponsor', {\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\ttransactionBlockKindBytes: input.transactionBlockKindBytes,\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 Error('Failed to fetch');\n\t\t}\n\n\t\tconst { data } = await res.json();\n\n\t\treturn data as T;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBA,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;AAahB,MAAM,YAAY;AAAA,EAKxB,YAAY,QAA2B;AAyDvC,uBAAM;AA7DN;AACA;AACA;AAGC,uBAAK,UAAW;AAChB,uBAAK,SAAU,OAAO,UAAU;AAChC,uBAAK,SAAU,OAAO;AAAA,EACvB;AAAA,EAEA,OAAO,QAAyB;AAC/B,WAAO,sBAAK,kBAAL,WAA+B,OAAO;AAAA,MAC5C,QAAQ;AAAA,IACT;AAAA,EACD;AAAA,EAEA,WAAW,OAA2B;AACrC,WAAO,sBAAK,kBAAL,WAAmC,WAAW;AAAA,MACpD,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,CAAC,cAAc,GAAG,MAAM;AAAA,MACzB;AAAA,IACD;AAAA,EACD;AAAA,EAEA,mBAAmB,OAAmC;AACrD,WAAO,sBAAK,kBAAL,WAA2C,iBAAiB;AAAA,MAClE,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU;AAAA,QACpB,oBAAoB,MAAM,mBAAmB,eAAe;AAAA,MAC7D,CAAC;AAAA,IACF;AAAA,EACD;AAAA,EAEA,iBAAiB,OAAiC;AACjD,WAAO,sBAAK,kBAAL,WAAyC,eAAe;AAAA,MAC9D,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,CAAC,cAAc,GAAG,MAAM;AAAA,MACzB;AAAA,MACA,MAAM,KAAK,UAAU;AAAA,QACpB,oBAAoB,MAAM,mBAAmB,eAAe;AAAA,QAC5D,UAAU,MAAM;AAAA,QAChB,YAAY,MAAM;AAAA,MACnB,CAAC;AAAA,IACF;AAAA,EACD;AAAA,EAEA,gCAAgC,OAAgD;AAC/E,WAAO,sBAAK,kBAAL,WAAwD,8BAA8B;AAAA,MAC5F,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,CAAC,cAAc,GAAG,MAAM;AAAA,MACzB;AAAA,MACA,MAAM,KAAK,UAAU;AAAA,QACpB,SAAS,MAAM;AAAA,QACf,2BAA2B,MAAM;AAAA,MAClC,CAAC;AAAA,IACF;AAAA,EACD;AAqBD;AAhFC;AACA;AACA;AA2DM;AAAA,WAAmB,eAAC,MAAc,MAA+B;AACtE,QAAM,MAAM,MAAM,MAAM,GAAG,mBAAK,YAAW,mBAAK,aAAY,QAAQ;AAAA,IACnE,GAAG;AAAA,IACH,SAAS;AAAA,MACR,GAAG,KAAK;AAAA,MACR,eAAe,UAAU,mBAAK;AAAA,MAC9B,gBAAgB;AAAA,MAChB,cAAc,OAAO,WAAW;AAAA,IACjC;AAAA,EACD,CAAC;AAED,MAAI,CAAC,IAAI,IAAI;AACZ,UAAM,IAAI,MAAM,iBAAiB;AAAA,EAClC;AAEA,QAAM,EAAE,KAAK,IAAI,MAAM,IAAI,KAAK;AAEhC,SAAO;AACR;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -34,3 +34,15 @@ export interface CreateZkLoginZkpApiInput {
|
|
|
34
34
|
}
|
|
35
35
|
export interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {
|
|
36
36
|
}
|
|
37
|
+
export interface CreateSponsoredTransactionBlockApiInput {
|
|
38
|
+
network?: 'mainnet' | 'testnet';
|
|
39
|
+
jwt: string;
|
|
40
|
+
transactionBlockKindBytes: string;
|
|
41
|
+
}
|
|
42
|
+
export interface CreateSponsoredTransactionBlockApiResponse {
|
|
43
|
+
bytes: string;
|
|
44
|
+
signature: string;
|
|
45
|
+
digest: string;
|
|
46
|
+
expireAtTime: number;
|
|
47
|
+
expireAfterEpoch: number;
|
|
48
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/EnokiClient/type.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { PublicKey } from '@mysten/sui.js/cryptography';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';\n\nimport type { AuthProvider } from '../EnokiFlow.js';\n\nexport interface GetAppApiInput {}\nexport interface GetAppApiResponse {\n\tauthenticationProviders: {\n\t\tproviderType: AuthProvider;\n\t\tclientId: string;\n\t}[];\n}\n\nexport interface GetZkLoginApiInput {\n\tjwt: string;\n}\nexport interface GetZkLoginApiResponse {\n\taddress: string;\n\tsalt: string;\n}\n\nexport interface CreateZkLoginNonceApiInput {\n\tephemeralPublicKey: PublicKey;\n}\nexport interface CreateZkLoginNonceApiResponse {\n\tnonce: string;\n\trandomness: string;\n\tepoch: number;\n\tmaxEpoch: number;\n\testimatedExpiration: number;\n}\n\nexport interface CreateZkLoginZkpApiInput {\n\tjwt: string;\n\tephemeralPublicKey: PublicKey;\n\trandomness: string;\n\tmaxEpoch: number;\n}\nexport interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {}\n"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { PublicKey } from '@mysten/sui.js/cryptography';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';\n\nimport type { AuthProvider } from '../EnokiFlow.js';\n\nexport interface GetAppApiInput {}\nexport interface GetAppApiResponse {\n\tauthenticationProviders: {\n\t\tproviderType: AuthProvider;\n\t\tclientId: string;\n\t}[];\n}\n\nexport interface GetZkLoginApiInput {\n\tjwt: string;\n}\nexport interface GetZkLoginApiResponse {\n\taddress: string;\n\tsalt: string;\n}\n\nexport interface CreateZkLoginNonceApiInput {\n\tephemeralPublicKey: PublicKey;\n}\nexport interface CreateZkLoginNonceApiResponse {\n\tnonce: string;\n\trandomness: string;\n\tepoch: number;\n\tmaxEpoch: number;\n\testimatedExpiration: number;\n}\n\nexport interface CreateZkLoginZkpApiInput {\n\tjwt: string;\n\tephemeralPublicKey: PublicKey;\n\trandomness: string;\n\tmaxEpoch: number;\n}\nexport interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {}\n\nexport interface CreateSponsoredTransactionBlockApiInput {\n\tnetwork?: 'mainnet' | 'testnet';\n\tjwt: string;\n\ttransactionBlockKindBytes: string;\n}\n\nexport interface CreateSponsoredTransactionBlockApiResponse {\n\tbytes: string;\n\tsignature: string;\n\tdigest: string;\n\texpireAtTime: number;\n\texpireAfterEpoch: number;\n}\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/EnokiFlow.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ExecuteTransactionBlockParams, SuiClient } from '@mysten/sui.js/client';
|
|
2
|
+
import type { TransactionBlock } from '@mysten/sui.js/transactions';
|
|
1
3
|
import type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';
|
|
2
4
|
import type { WritableAtom } from 'nanostores';
|
|
3
5
|
import type { Encryption } from './encryption.js';
|
|
@@ -47,4 +49,9 @@ export declare class EnokiFlow {
|
|
|
47
49
|
logout(): Promise<void>;
|
|
48
50
|
getProof(): Promise<import("./EnokiClient/type.js").CreateZkLoginZkpApiResponse>;
|
|
49
51
|
getKeypair(): Promise<EnokiKeypair>;
|
|
52
|
+
sponsorAndExecuteTransactionBlock({ network, transactionBlock, client, ...options }: {
|
|
53
|
+
network?: 'mainnet' | 'testnet';
|
|
54
|
+
transactionBlock: TransactionBlock;
|
|
55
|
+
client: SuiClient;
|
|
56
|
+
} & Omit<ExecuteTransactionBlockParams, 'signature' | 'transactionBlock'>): Promise<import("@mysten/sui.js/client").SuiTransactionBlockResponse>;
|
|
50
57
|
}
|
package/dist/cjs/EnokiFlow.js
CHANGED
|
@@ -51,14 +51,15 @@ var import_encryption = require("./encryption.js");
|
|
|
51
51
|
var import_EnokiClient = require("./EnokiClient/index.js");
|
|
52
52
|
var import_EnokiKeypair = require("./EnokiKeypair.js");
|
|
53
53
|
var import_stores = require("./stores.js");
|
|
54
|
-
var _enokiClient, _encryption, _encryptionKey, _store, _zkLoginSessionInitialized, _zkLoginSession, _setSession, setSession_fn;
|
|
55
|
-
const
|
|
56
|
-
STATE:
|
|
57
|
-
SESSION:
|
|
58
|
-
};
|
|
54
|
+
var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _zkLoginSessionInitialized, _zkLoginSession, _setSession, setSession_fn;
|
|
55
|
+
const createStorageKeys = (apiKey) => ({
|
|
56
|
+
STATE: `@enoki/flow/state/${apiKey}`,
|
|
57
|
+
SESSION: `@enoki/flow/session/${apiKey}`
|
|
58
|
+
});
|
|
59
59
|
class EnokiFlow {
|
|
60
60
|
constructor(config) {
|
|
61
61
|
__privateAdd(this, _setSession);
|
|
62
|
+
__privateAdd(this, _storageKeys, void 0);
|
|
62
63
|
__privateAdd(this, _enokiClient, void 0);
|
|
63
64
|
__privateAdd(this, _encryption, void 0);
|
|
64
65
|
__privateAdd(this, _encryptionKey, void 0);
|
|
@@ -72,9 +73,10 @@ class EnokiFlow {
|
|
|
72
73
|
__privateSet(this, _encryptionKey, config.apiKey);
|
|
73
74
|
__privateSet(this, _encryption, config.encryption ?? (0, import_encryption.createDefaultEncryption)());
|
|
74
75
|
__privateSet(this, _store, config.store ?? (0, import_stores.createSessionStorage)());
|
|
76
|
+
__privateSet(this, _storageKeys, createStorageKeys(config.apiKey));
|
|
75
77
|
let storedState = null;
|
|
76
78
|
try {
|
|
77
|
-
const rawStoredValue = __privateGet(this, _store).get(
|
|
79
|
+
const rawStoredValue = __privateGet(this, _store).get(__privateGet(this, _storageKeys).STATE);
|
|
78
80
|
if (rawStoredValue) {
|
|
79
81
|
storedState = JSON.parse(rawStoredValue);
|
|
80
82
|
}
|
|
@@ -84,7 +86,7 @@ class EnokiFlow {
|
|
|
84
86
|
__privateSet(this, _zkLoginSessionInitialized, false);
|
|
85
87
|
__privateSet(this, _zkLoginSession, null);
|
|
86
88
|
(0, import_nanostores.onSet)(this.$zkLoginState, ({ newValue }) => {
|
|
87
|
-
__privateGet(this, _store).set(
|
|
89
|
+
__privateGet(this, _store).set(__privateGet(this, _storageKeys).STATE, JSON.stringify(newValue));
|
|
88
90
|
});
|
|
89
91
|
}
|
|
90
92
|
get enokiClient() {
|
|
@@ -169,7 +171,7 @@ class EnokiFlow {
|
|
|
169
171
|
return __privateGet(this, _zkLoginSession);
|
|
170
172
|
}
|
|
171
173
|
try {
|
|
172
|
-
const storedValue = __privateGet(this, _store).get(
|
|
174
|
+
const storedValue = __privateGet(this, _store).get(__privateGet(this, _storageKeys).SESSION);
|
|
173
175
|
if (!storedValue)
|
|
174
176
|
return null;
|
|
175
177
|
const state = JSON.parse(
|
|
@@ -187,7 +189,7 @@ class EnokiFlow {
|
|
|
187
189
|
}
|
|
188
190
|
async logout() {
|
|
189
191
|
this.$zkLoginState.set({});
|
|
190
|
-
__privateGet(this, _store).delete(
|
|
192
|
+
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).STATE);
|
|
191
193
|
await __privateMethod(this, _setSession, setSession_fn).call(this, null);
|
|
192
194
|
}
|
|
193
195
|
// TODO: Should this return the proof if it already exists?
|
|
@@ -217,8 +219,8 @@ class EnokiFlow {
|
|
|
217
219
|
return proof;
|
|
218
220
|
}
|
|
219
221
|
async getKeypair() {
|
|
220
|
-
const zkp = await this.getSession();
|
|
221
222
|
await this.getProof();
|
|
223
|
+
const zkp = await this.getSession();
|
|
222
224
|
const { address } = this.$zkLoginState.get();
|
|
223
225
|
if (!address || !zkp || !zkp.proof) {
|
|
224
226
|
throw new Error("Missing required data for keypair generation.");
|
|
@@ -233,7 +235,39 @@ class EnokiFlow {
|
|
|
233
235
|
ephemeralKeypair: import_ed25519.Ed25519Keypair.fromSecretKey((0, import_utils.fromB64)(zkp.ephemeralKeyPair))
|
|
234
236
|
});
|
|
235
237
|
}
|
|
238
|
+
async sponsorAndExecuteTransactionBlock({
|
|
239
|
+
network,
|
|
240
|
+
transactionBlock,
|
|
241
|
+
client,
|
|
242
|
+
...options
|
|
243
|
+
}) {
|
|
244
|
+
const session = await this.getSession();
|
|
245
|
+
const keypair = await this.getKeypair();
|
|
246
|
+
const transactionBlockKindBytes = await transactionBlock.build({
|
|
247
|
+
onlyTransactionKind: true,
|
|
248
|
+
client,
|
|
249
|
+
// Theses limits will get verified during the final transaction construction, so we can safely ignore them here:
|
|
250
|
+
limits: {
|
|
251
|
+
maxGasObjects: Infinity,
|
|
252
|
+
maxPureArgumentSize: Infinity,
|
|
253
|
+
maxTxGas: Infinity,
|
|
254
|
+
maxTxSizeBytes: Infinity
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
const { bytes, signature: sponsorSignature } = await __privateGet(this, _enokiClient).createSponsoredTransactionBlock({
|
|
258
|
+
jwt: session.jwt,
|
|
259
|
+
network,
|
|
260
|
+
transactionBlockKindBytes: (0, import_utils.toB64)(transactionBlockKindBytes)
|
|
261
|
+
});
|
|
262
|
+
const userSignature = await keypair.signTransactionBlock((0, import_utils.fromB64)(bytes));
|
|
263
|
+
return client.executeTransactionBlock({
|
|
264
|
+
transactionBlock: bytes,
|
|
265
|
+
signature: [userSignature.signature, sponsorSignature],
|
|
266
|
+
...options
|
|
267
|
+
});
|
|
268
|
+
}
|
|
236
269
|
}
|
|
270
|
+
_storageKeys = new WeakMap();
|
|
237
271
|
_enokiClient = new WeakMap();
|
|
238
272
|
_encryption = new WeakMap();
|
|
239
273
|
_encryptionKey = new WeakMap();
|
|
@@ -247,9 +281,9 @@ setSession_fn = async function(newValue) {
|
|
|
247
281
|
__privateGet(this, _encryptionKey),
|
|
248
282
|
JSON.stringify(newValue)
|
|
249
283
|
);
|
|
250
|
-
__privateGet(this, _store).set(
|
|
284
|
+
__privateGet(this, _store).set(__privateGet(this, _storageKeys).SESSION, storedValue);
|
|
251
285
|
} else {
|
|
252
|
-
__privateGet(this, _store).delete(
|
|
286
|
+
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).SESSION);
|
|
253
287
|
}
|
|
254
288
|
__privateSet(this, _zkLoginSession, newValue);
|
|
255
289
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/EnokiFlow.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';\nimport { fromB64 } from '@mysten/sui.js/utils';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';\nimport { decodeJwt } from 'jose';\nimport type { WritableAtom } from 'nanostores';\nimport { atom, onSet } from 'nanostores';\n\nimport type { Encryption } from './encryption.js';\nimport { createDefaultEncryption } from './encryption.js';\nimport type { EnokiClientConfig } from './EnokiClient/index.js';\nimport { EnokiClient } from './EnokiClient/index.js';\nimport { EnokiKeypair } from './EnokiKeypair.js';\nimport type { SyncStore } from './stores.js';\nimport { createSessionStorage } from './stores.js';\n\nexport interface EnokiFlowConfig extends EnokiClientConfig {\n\t/**\n\t * The storage interface to persist Enoki data locally.\n\t * If not provided, it will use a sessionStorage-backed store.\n\t */\n\tstore?: SyncStore;\n\t/**\n\t * The encryption interface that will be used to encrypt data before storing it locally.\n\t * If not provided, it will use a default encryption interface.\n\t */\n\tencryption?: Encryption;\n}\n\n// State that is not bound to a session, and is encrypted.\nexport interface ZkLoginState {\n\tprovider?: AuthProvider;\n\taddress?: string;\n\tsalt?: string;\n}\n\n// State that session-bound, and is encrypted in storage.\nexport interface ZkLoginSession {\n\tephemeralKeyPair: string;\n\tmaxEpoch: number;\n\trandomness: string;\n\texpiresAt: number;\n\n\tjwt?: string;\n\tproof?: ZkLoginSignatureInputs;\n}\n\nexport type AuthProvider = 'google' | 'facebook' | 'twitch';\n\nconst STORAGE_KEYS = {\n\tSTATE: '@enoki/flow/state',\n\tSESSION: '@enoki/flow/session',\n};\n\nexport class EnokiFlow {\n\t#enokiClient: EnokiClient;\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#store: SyncStore;\n\n\t#zkLoginSessionInitialized: boolean;\n\t#zkLoginSession: ZkLoginSession | null;\n\n\t$zkLoginState: WritableAtom<ZkLoginState>;\n\n\tconstructor(config: EnokiFlowConfig) {\n\t\tthis.#enokiClient = new EnokiClient({\n\t\t\tapiKey: config.apiKey,\n\t\t\tapiUrl: config.apiUrl,\n\t\t});\n\t\tthis.#encryptionKey = config.apiKey;\n\t\tthis.#encryption = config.encryption ?? createDefaultEncryption();\n\t\tthis.#store = config.store ?? createSessionStorage();\n\n\t\tlet storedState = null;\n\t\ttry {\n\t\t\tconst rawStoredValue = this.#store.get(STORAGE_KEYS.STATE);\n\t\t\tif (rawStoredValue) {\n\t\t\t\tstoredState = JSON.parse(rawStoredValue);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\n\t\tthis.$zkLoginState = atom(storedState || {});\n\n\t\tthis.#zkLoginSessionInitialized = false;\n\t\tthis.#zkLoginSession = null;\n\n\t\tonSet(this.$zkLoginState, ({ newValue }) => {\n\t\t\tthis.#store.set(STORAGE_KEYS.STATE, JSON.stringify(newValue));\n\t\t});\n\t}\n\n\tget enokiClient() {\n\t\treturn this.#enokiClient;\n\t}\n\n\tasync createAuthorizationURL(input: {\n\t\tprovider: AuthProvider;\n\t\tclientId: string;\n\t\tredirectUrl: string;\n\t\textraParams?: Record<string, unknown>;\n\t}) {\n\t\tconst ephemeralKeyPair = new Ed25519Keypair();\n\t\tconst { nonce, randomness, maxEpoch, estimatedExpiration } =\n\t\t\tawait this.#enokiClient.createZkLoginNonce({\n\t\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t\t});\n\n\t\tconst params = new URLSearchParams({\n\t\t\t...input.extraParams,\n\t\t\tnonce,\n\t\t\tclient_id: input.clientId,\n\t\t\tredirect_uri: input.redirectUrl,\n\t\t\tresponse_type: 'id_token',\n\t\t\t// TODO: Eventually fetch the scopes for this client ID from the Enoki service:\n\t\t\tscope: [\n\t\t\t\t'openid',\n\t\t\t\t// Merge the requested scopes in with the required openid scopes:\n\t\t\t\t...(input.extraParams && 'scope' in input.extraParams\n\t\t\t\t\t? (input.extraParams.scope as string[])\n\t\t\t\t\t: []),\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(' '),\n\t\t});\n\n\t\tlet oauthUrl: string;\n\t\tswitch (input.provider) {\n\t\t\tcase 'google': {\n\t\t\t\toauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'facebook': {\n\t\t\t\toauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'twitch': {\n\t\t\t\tparams.set('force_verify', 'true');\n\t\t\t\toauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Invalid provider: ${input.provider}`);\n\t\t}\n\n\t\tthis.$zkLoginState.set({ provider: input.provider });\n\t\tawait this.#setSession({\n\t\t\texpiresAt: estimatedExpiration,\n\t\t\tmaxEpoch,\n\t\t\trandomness,\n\t\t\tephemeralKeyPair: ephemeralKeyPair.export().privateKey,\n\t\t});\n\n\t\treturn oauthUrl;\n\t}\n\n\t// TODO: Should our SDK manage this automatically in addition to exposing a method?\n\tasync handleAuthCallback(hash: string = window.location.hash) {\n\t\tconst params = new URLSearchParams(hash.startsWith('#') ? hash.slice(1) : hash);\n\n\t\t// Before we handle the auth redirect and get the state, we need to restore it:\n\t\tconst zkp = await this.getSession();\n\n\t\tif (!zkp || !zkp.ephemeralKeyPair || !zkp.maxEpoch || !zkp.randomness) {\n\t\t\tthrow new Error(\n\t\t\t\t'Start of sign-in flow could not be found. Ensure you have started the sign-in flow before calling this.',\n\t\t\t);\n\t\t}\n\n\t\tconst jwt = params.get('id_token');\n\t\tif (!jwt) {\n\t\t\tthrow new Error('Missing ID Token');\n\t\t}\n\n\t\tconst decodedJwt = decodeJwt(jwt);\n\t\tif (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== 'string') {\n\t\t\tthrow new Error('Missing JWT data');\n\t\t}\n\n\t\tconst { address, salt } = await this.#enokiClient.getZkLogin({ jwt });\n\n\t\tthis.$zkLoginState.set({\n\t\t\t...this.$zkLoginState.get(),\n\t\t\tsalt,\n\t\t\taddress,\n\t\t});\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tjwt,\n\t\t});\n\n\t\treturn params.get('state');\n\t}\n\n\tasync #setSession(newValue: ZkLoginSession | null) {\n\t\tif (newValue) {\n\t\t\tconst storedValue = await this.#encryption.encrypt(\n\t\t\t\tthis.#encryptionKey,\n\t\t\t\tJSON.stringify(newValue),\n\t\t\t);\n\n\t\t\tthis.#store.set(STORAGE_KEYS.SESSION, storedValue);\n\t\t} else {\n\t\t\tthis.#store.delete(STORAGE_KEYS.SESSION);\n\t\t}\n\n\t\tthis.#zkLoginSession = newValue;\n\t}\n\n\tasync getSession() {\n\t\tif (this.#zkLoginSessionInitialized) {\n\t\t\treturn this.#zkLoginSession;\n\t\t}\n\n\t\ttry {\n\t\t\tconst storedValue = this.#store.get(STORAGE_KEYS.SESSION);\n\t\t\tif (!storedValue) return null;\n\n\t\t\tconst state: ZkLoginSession = JSON.parse(\n\t\t\t\tawait this.#encryption.decrypt(this.#encryptionKey, storedValue),\n\t\t\t);\n\n\t\t\t// TODO: Rather than having expiration act as a logout, we should keep the state that still is relevant,\n\t\t\t// and just clear out the expired session, but keep the other zkLogin state.\n\t\t\tif (state?.expiresAt && Date.now() > state.expiresAt) {\n\t\t\t\tawait this.logout();\n\t\t\t} else {\n\t\t\t\tthis.#zkLoginSession = state;\n\t\t\t}\n\n\t\t\treturn this.#zkLoginSession;\n\t\t} finally {\n\t\t\tthis.#zkLoginSessionInitialized = true;\n\t\t}\n\t}\n\n\tasync logout() {\n\t\tthis.$zkLoginState.set({});\n\t\tthis.#store.delete(STORAGE_KEYS.STATE);\n\n\t\tawait this.#setSession(null);\n\t}\n\n\t// TODO: Should this return the proof if it already exists?\n\tasync getProof() {\n\t\tconst zkp = await this.getSession();\n\t\tconst { salt } = this.$zkLoginState.get();\n\n\t\tif (zkp?.proof) {\n\t\t\tif (zkp.expiresAt && Date.now() > zkp.expiresAt) {\n\t\t\t\tthrow new Error('Stored proof is expired.');\n\t\t\t}\n\n\t\t\treturn zkp.proof;\n\t\t}\n\n\t\tif (!salt || !zkp || !zkp.jwt) {\n\t\t\tthrow new Error('Missing required parameters for proof generation');\n\t\t}\n\n\t\tconst ephemeralKeyPair = Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair));\n\n\t\tconst proof = await this.#enokiClient.createZkLoginZkp({\n\t\t\tjwt: zkp.jwt!,\n\t\t\tmaxEpoch: zkp.maxEpoch!,\n\t\t\trandomness: zkp.randomness!,\n\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t});\n\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tproof,\n\t\t});\n\n\t\treturn proof;\n\t}\n\n\tasync getKeypair() {\n\t\tconst zkp = await this.getSession();\n\n\t\t// Get the proof, so that we ensure it exists in state:\n\t\tawait this.getProof();\n\n\t\t// Check to see if we have the essentials for a keypair:\n\t\tconst { address } = this.$zkLoginState.get();\n\t\tif (!address || !zkp || !zkp.proof) {\n\t\t\tthrow new Error('Missing required data for keypair generation.');\n\t\t}\n\n\t\tif (Date.now() > zkp.expiresAt) {\n\t\t\tthrow new Error('Stored proof is expired.');\n\t\t}\n\n\t\treturn new EnokiKeypair({\n\t\t\taddress,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\tproof: zkp.proof,\n\t\t\tephemeralKeypair: Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair)),\n\t\t});\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { ExecuteTransactionBlockParams, SuiClient } from '@mysten/sui.js/client';\nimport { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';\nimport type { TransactionBlock } from '@mysten/sui.js/transactions';\nimport { fromB64, toB64 } from '@mysten/sui.js/utils';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';\nimport { decodeJwt } from 'jose';\nimport type { WritableAtom } from 'nanostores';\nimport { atom, onSet } from 'nanostores';\n\nimport type { Encryption } from './encryption.js';\nimport { createDefaultEncryption } from './encryption.js';\nimport type { EnokiClientConfig } from './EnokiClient/index.js';\nimport { EnokiClient } from './EnokiClient/index.js';\nimport { EnokiKeypair } from './EnokiKeypair.js';\nimport type { SyncStore } from './stores.js';\nimport { createSessionStorage } from './stores.js';\n\nexport interface EnokiFlowConfig extends EnokiClientConfig {\n\t/**\n\t * The storage interface to persist Enoki data locally.\n\t * If not provided, it will use a sessionStorage-backed store.\n\t */\n\tstore?: SyncStore;\n\t/**\n\t * The encryption interface that will be used to encrypt data before storing it locally.\n\t * If not provided, it will use a default encryption interface.\n\t */\n\tencryption?: Encryption;\n}\n\n// State that is not bound to a session, and is encrypted.\nexport interface ZkLoginState {\n\tprovider?: AuthProvider;\n\taddress?: string;\n\tsalt?: string;\n}\n\n// State that session-bound, and is encrypted in storage.\nexport interface ZkLoginSession {\n\tephemeralKeyPair: string;\n\tmaxEpoch: number;\n\trandomness: string;\n\texpiresAt: number;\n\n\tjwt?: string;\n\tproof?: ZkLoginSignatureInputs;\n}\n\nexport type AuthProvider = 'google' | 'facebook' | 'twitch';\n\nconst createStorageKeys = (apiKey: string) => ({\n\tSTATE: `@enoki/flow/state/${apiKey}`,\n\tSESSION: `@enoki/flow/session/${apiKey}`,\n});\n\nexport class EnokiFlow {\n\t#storageKeys: { STATE: string; SESSION: string };\n\t#enokiClient: EnokiClient;\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#store: SyncStore;\n\n\t#zkLoginSessionInitialized: boolean;\n\t#zkLoginSession: ZkLoginSession | null;\n\n\t$zkLoginState: WritableAtom<ZkLoginState>;\n\n\tconstructor(config: EnokiFlowConfig) {\n\t\tthis.#enokiClient = new EnokiClient({\n\t\t\tapiKey: config.apiKey,\n\t\t\tapiUrl: config.apiUrl,\n\t\t});\n\t\tthis.#encryptionKey = config.apiKey;\n\t\tthis.#encryption = config.encryption ?? createDefaultEncryption();\n\t\tthis.#store = config.store ?? createSessionStorage();\n\t\tthis.#storageKeys = createStorageKeys(config.apiKey);\n\n\t\tlet storedState = null;\n\t\ttry {\n\t\t\tconst rawStoredValue = this.#store.get(this.#storageKeys.STATE);\n\t\t\tif (rawStoredValue) {\n\t\t\t\tstoredState = JSON.parse(rawStoredValue);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\n\t\tthis.$zkLoginState = atom(storedState || {});\n\n\t\tthis.#zkLoginSessionInitialized = false;\n\t\tthis.#zkLoginSession = null;\n\n\t\tonSet(this.$zkLoginState, ({ newValue }) => {\n\t\t\tthis.#store.set(this.#storageKeys.STATE, JSON.stringify(newValue));\n\t\t});\n\t}\n\n\tget enokiClient() {\n\t\treturn this.#enokiClient;\n\t}\n\n\tasync createAuthorizationURL(input: {\n\t\tprovider: AuthProvider;\n\t\tclientId: string;\n\t\tredirectUrl: string;\n\t\textraParams?: Record<string, unknown>;\n\t}) {\n\t\tconst ephemeralKeyPair = new Ed25519Keypair();\n\t\tconst { nonce, randomness, maxEpoch, estimatedExpiration } =\n\t\t\tawait this.#enokiClient.createZkLoginNonce({\n\t\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t\t});\n\n\t\tconst params = new URLSearchParams({\n\t\t\t...input.extraParams,\n\t\t\tnonce,\n\t\t\tclient_id: input.clientId,\n\t\t\tredirect_uri: input.redirectUrl,\n\t\t\tresponse_type: 'id_token',\n\t\t\t// TODO: Eventually fetch the scopes for this client ID from the Enoki service:\n\t\t\tscope: [\n\t\t\t\t'openid',\n\t\t\t\t// Merge the requested scopes in with the required openid scopes:\n\t\t\t\t...(input.extraParams && 'scope' in input.extraParams\n\t\t\t\t\t? (input.extraParams.scope as string[])\n\t\t\t\t\t: []),\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(' '),\n\t\t});\n\n\t\tlet oauthUrl: string;\n\t\tswitch (input.provider) {\n\t\t\tcase 'google': {\n\t\t\t\toauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'facebook': {\n\t\t\t\toauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'twitch': {\n\t\t\t\tparams.set('force_verify', 'true');\n\t\t\t\toauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Invalid provider: ${input.provider}`);\n\t\t}\n\n\t\tthis.$zkLoginState.set({ provider: input.provider });\n\t\tawait this.#setSession({\n\t\t\texpiresAt: estimatedExpiration,\n\t\t\tmaxEpoch,\n\t\t\trandomness,\n\t\t\tephemeralKeyPair: ephemeralKeyPair.export().privateKey,\n\t\t});\n\n\t\treturn oauthUrl;\n\t}\n\n\t// TODO: Should our SDK manage this automatically in addition to exposing a method?\n\tasync handleAuthCallback(hash: string = window.location.hash) {\n\t\tconst params = new URLSearchParams(hash.startsWith('#') ? hash.slice(1) : hash);\n\n\t\t// Before we handle the auth redirect and get the state, we need to restore it:\n\t\tconst zkp = await this.getSession();\n\n\t\tif (!zkp || !zkp.ephemeralKeyPair || !zkp.maxEpoch || !zkp.randomness) {\n\t\t\tthrow new Error(\n\t\t\t\t'Start of sign-in flow could not be found. Ensure you have started the sign-in flow before calling this.',\n\t\t\t);\n\t\t}\n\n\t\tconst jwt = params.get('id_token');\n\t\tif (!jwt) {\n\t\t\tthrow new Error('Missing ID Token');\n\t\t}\n\n\t\tconst decodedJwt = decodeJwt(jwt);\n\t\tif (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== 'string') {\n\t\t\tthrow new Error('Missing JWT data');\n\t\t}\n\n\t\tconst { address, salt } = await this.#enokiClient.getZkLogin({ jwt });\n\n\t\tthis.$zkLoginState.set({\n\t\t\t...this.$zkLoginState.get(),\n\t\t\tsalt,\n\t\t\taddress,\n\t\t});\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tjwt,\n\t\t});\n\n\t\treturn params.get('state');\n\t}\n\n\tasync #setSession(newValue: ZkLoginSession | null) {\n\t\tif (newValue) {\n\t\t\tconst storedValue = await this.#encryption.encrypt(\n\t\t\t\tthis.#encryptionKey,\n\t\t\t\tJSON.stringify(newValue),\n\t\t\t);\n\n\t\t\tthis.#store.set(this.#storageKeys.SESSION, storedValue);\n\t\t} else {\n\t\t\tthis.#store.delete(this.#storageKeys.SESSION);\n\t\t}\n\n\t\tthis.#zkLoginSession = newValue;\n\t}\n\n\tasync getSession() {\n\t\tif (this.#zkLoginSessionInitialized) {\n\t\t\treturn this.#zkLoginSession;\n\t\t}\n\n\t\ttry {\n\t\t\tconst storedValue = this.#store.get(this.#storageKeys.SESSION);\n\t\t\tif (!storedValue) return null;\n\n\t\t\tconst state: ZkLoginSession = JSON.parse(\n\t\t\t\tawait this.#encryption.decrypt(this.#encryptionKey, storedValue),\n\t\t\t);\n\n\t\t\t// TODO: Rather than having expiration act as a logout, we should keep the state that still is relevant,\n\t\t\t// and just clear out the expired session, but keep the other zkLogin state.\n\t\t\tif (state?.expiresAt && Date.now() > state.expiresAt) {\n\t\t\t\tawait this.logout();\n\t\t\t} else {\n\t\t\t\tthis.#zkLoginSession = state;\n\t\t\t}\n\n\t\t\treturn this.#zkLoginSession;\n\t\t} finally {\n\t\t\tthis.#zkLoginSessionInitialized = true;\n\t\t}\n\t}\n\n\tasync logout() {\n\t\tthis.$zkLoginState.set({});\n\t\tthis.#store.delete(this.#storageKeys.STATE);\n\n\t\tawait this.#setSession(null);\n\t}\n\n\t// TODO: Should this return the proof if it already exists?\n\tasync getProof() {\n\t\tconst zkp = await this.getSession();\n\t\tconst { salt } = this.$zkLoginState.get();\n\n\t\tif (zkp?.proof) {\n\t\t\tif (zkp.expiresAt && Date.now() > zkp.expiresAt) {\n\t\t\t\tthrow new Error('Stored proof is expired.');\n\t\t\t}\n\n\t\t\treturn zkp.proof;\n\t\t}\n\n\t\tif (!salt || !zkp || !zkp.jwt) {\n\t\t\tthrow new Error('Missing required parameters for proof generation');\n\t\t}\n\n\t\tconst ephemeralKeyPair = Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair));\n\n\t\tconst proof = await this.#enokiClient.createZkLoginZkp({\n\t\t\tjwt: zkp.jwt!,\n\t\t\tmaxEpoch: zkp.maxEpoch!,\n\t\t\trandomness: zkp.randomness!,\n\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t});\n\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tproof,\n\t\t});\n\n\t\treturn proof;\n\t}\n\n\tasync getKeypair() {\n\t\t// Get the proof, so that we ensure it exists in state:\n\t\tawait this.getProof();\n\n\t\tconst zkp = await this.getSession();\n\n\t\t// Check to see if we have the essentials for a keypair:\n\t\tconst { address } = this.$zkLoginState.get();\n\t\tif (!address || !zkp || !zkp.proof) {\n\t\t\tthrow new Error('Missing required data for keypair generation.');\n\t\t}\n\n\t\tif (Date.now() > zkp.expiresAt) {\n\t\t\tthrow new Error('Stored proof is expired.');\n\t\t}\n\n\t\treturn new EnokiKeypair({\n\t\t\taddress,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\tproof: zkp.proof,\n\t\t\tephemeralKeypair: Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair)),\n\t\t});\n\t}\n\n\tasync sponsorAndExecuteTransactionBlock({\n\t\tnetwork,\n\t\ttransactionBlock,\n\t\tclient,\n\t\t...options\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\ttransactionBlock: TransactionBlock;\n\t\tclient: SuiClient;\n\t} & Omit<ExecuteTransactionBlockParams, 'signature' | 'transactionBlock'>) {\n\t\tconst session = await this.getSession();\n\t\tconst keypair = await this.getKeypair();\n\n\t\tconst transactionBlockKindBytes = await transactionBlock.build({\n\t\t\tonlyTransactionKind: true,\n\t\t\tclient,\n\t\t\t// Theses limits will get verified during the final transaction construction, so we can safely ignore them here:\n\t\t\tlimits: {\n\t\t\t\tmaxGasObjects: Infinity,\n\t\t\t\tmaxPureArgumentSize: Infinity,\n\t\t\t\tmaxTxGas: Infinity,\n\t\t\t\tmaxTxSizeBytes: Infinity,\n\t\t\t},\n\t\t});\n\n\t\tconst { bytes, signature: sponsorSignature } =\n\t\t\tawait this.#enokiClient.createSponsoredTransactionBlock({\n\t\t\t\tjwt: session!.jwt!,\n\t\t\t\tnetwork,\n\t\t\t\ttransactionBlockKindBytes: toB64(transactionBlockKindBytes),\n\t\t\t});\n\n\t\tconst userSignature = await keypair.signTransactionBlock(fromB64(bytes));\n\n\t\treturn client.executeTransactionBlock({\n\t\t\ttransactionBlock: bytes,\n\t\t\tsignature: [userSignature.signature, sponsorSignature],\n\t\t\t...options,\n\t\t});\n\t}\n}\n"],
|
|
5
|
+
"mappings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AAA,IACA,GAAG;AAAA,EACJ,GAI2E;AAC1E,UAAM,UAAU,MAAM,KAAK,WAAW;AACtC,UAAM,UAAU,MAAM,KAAK,WAAW;AAEtC,UAAM,4BAA4B,MAAM,iBAAiB,MAAM;AAAA,MAC9D,qBAAqB;AAAA,MACrB;AAAA;AAAA,MAEA,QAAQ;AAAA,QACP,eAAe;AAAA,QACf,qBAAqB;AAAA,QACrB,UAAU;AAAA,QACV,gBAAgB;AAAA,MACjB;AAAA,IACD,CAAC;AAED,UAAM,EAAE,OAAO,WAAW,iBAAiB,IAC1C,MAAM,mBAAK,cAAa,gCAAgC;AAAA,MACvD,KAAK,QAAS;AAAA,MACd;AAAA,MACA,+BAA2B,oBAAM,yBAAyB;AAAA,IAC3D,CAAC;AAEF,UAAM,gBAAgB,MAAM,QAAQ,yBAAqB,sBAAQ,KAAK,CAAC;AAEvE,WAAO,OAAO,wBAAwB;AAAA,MACrC,kBAAkB;AAAA,MAClB,WAAW,CAAC,cAAc,WAAW,gBAAgB;AAAA,MACrD,GAAG;AAAA,IACJ,CAAC;AAAA,EACF;AACD;AArSC;AACA;AACA;AACA;AACA;AAEA;AACA;AA2IM;AAAA,gBAAW,eAAC,UAAiC;AAClD,MAAI,UAAU;AACb,UAAM,cAAc,MAAM,mBAAK,aAAY;AAAA,MAC1C,mBAAK;AAAA,MACL,KAAK,UAAU,QAAQ;AAAA,IACxB;AAEA,uBAAK,QAAO,IAAI,mBAAK,cAAa,SAAS,WAAW;AAAA,EACvD,OAAO;AACN,uBAAK,QAAO,OAAO,mBAAK,cAAa,OAAO;AAAA,EAC7C;AAEA,qBAAK,iBAAkB;AACxB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/react.d.ts
CHANGED
|
@@ -7,3 +7,4 @@ export interface EnokiFlowProviderProps extends EnokiFlowConfig {
|
|
|
7
7
|
export declare function EnokiFlowProvider({ children, ...config }: EnokiFlowProviderProps): import("react/jsx-runtime.js").JSX.Element;
|
|
8
8
|
export declare function useEnokiFlow(): EnokiFlow;
|
|
9
9
|
export declare function useZkLogin(): import("./EnokiFlow.js").ZkLoginState;
|
|
10
|
+
export declare function useAuthCallback(): boolean;
|
package/dist/cjs/react.js
CHANGED
|
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var react_exports = {};
|
|
20
20
|
__export(react_exports, {
|
|
21
21
|
EnokiFlowProvider: () => EnokiFlowProvider,
|
|
22
|
+
useAuthCallback: () => useAuthCallback,
|
|
22
23
|
useEnokiFlow: () => useEnokiFlow,
|
|
23
24
|
useZkLogin: () => useZkLogin
|
|
24
25
|
});
|
|
@@ -43,4 +44,28 @@ function useZkLogin() {
|
|
|
43
44
|
const flow = useEnokiFlow();
|
|
44
45
|
return (0, import_react.useStore)(flow.$zkLoginState);
|
|
45
46
|
}
|
|
47
|
+
function useAuthCallback() {
|
|
48
|
+
const flow = useEnokiFlow();
|
|
49
|
+
const [handled, setHandled] = (0, import_react2.useState)(false);
|
|
50
|
+
const [hash, setHash] = (0, import_react2.useState)(null);
|
|
51
|
+
(0, import_react2.useEffect)(() => {
|
|
52
|
+
const listener = () => setHash(window.location.hash.slice(1).trim());
|
|
53
|
+
listener();
|
|
54
|
+
window.addEventListener("hashchange", listener);
|
|
55
|
+
return () => window.removeEventListener("hashchange", listener);
|
|
56
|
+
}, []);
|
|
57
|
+
(0, import_react2.useEffect)(() => {
|
|
58
|
+
if (!hash)
|
|
59
|
+
return;
|
|
60
|
+
(async () => {
|
|
61
|
+
try {
|
|
62
|
+
await flow.handleAuthCallback(hash);
|
|
63
|
+
window.location.hash = "";
|
|
64
|
+
} finally {
|
|
65
|
+
setHandled(true);
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
}, [hash, flow]);
|
|
69
|
+
return handled;
|
|
70
|
+
}
|
|
46
71
|
//# sourceMappingURL=react.js.map
|
package/dist/cjs/react.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/react.tsx"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useStore } from '@nanostores/react';\nimport type { ReactNode } from 'react';\nimport { createContext, useContext, useState } from 'react';\n\nimport type { EnokiFlowConfig } from './EnokiFlow.js';\nimport { EnokiFlow } from './EnokiFlow.js';\n\nconst EnokiFlowContext = createContext<EnokiFlow | null>(null);\n\nexport interface EnokiFlowProviderProps extends EnokiFlowConfig {\n\tchildren: ReactNode;\n}\n\nexport function EnokiFlowProvider({ children, ...config }: EnokiFlowProviderProps) {\n\tconst [enokiFlow] = useState(() => new EnokiFlow(config));\n\treturn <EnokiFlowContext.Provider value={enokiFlow}>{children}</EnokiFlowContext.Provider>;\n}\n\nexport function useEnokiFlow() {\n\tconst context = useContext(EnokiFlowContext);\n\tif (!context) {\n\t\tthrow new Error('Missing `EnokiFlowContext` provider');\n\t}\n\treturn context;\n}\n\nexport function useZkLogin() {\n\tconst flow = useEnokiFlow();\n\treturn useStore(flow.$zkLoginState);\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBQ;AAfR,mBAAyB;AAEzB,IAAAA,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useStore } from '@nanostores/react';\nimport type { ReactNode } from 'react';\nimport { createContext, useContext, useEffect, useState } from 'react';\n\nimport type { EnokiFlowConfig } from './EnokiFlow.js';\nimport { EnokiFlow } from './EnokiFlow.js';\n\nconst EnokiFlowContext = createContext<EnokiFlow | null>(null);\n\nexport interface EnokiFlowProviderProps extends EnokiFlowConfig {\n\tchildren: ReactNode;\n}\n\nexport function EnokiFlowProvider({ children, ...config }: EnokiFlowProviderProps) {\n\tconst [enokiFlow] = useState(() => new EnokiFlow(config));\n\treturn <EnokiFlowContext.Provider value={enokiFlow}>{children}</EnokiFlowContext.Provider>;\n}\n\nexport function useEnokiFlow() {\n\tconst context = useContext(EnokiFlowContext);\n\tif (!context) {\n\t\tthrow new Error('Missing `EnokiFlowContext` provider');\n\t}\n\treturn context;\n}\n\nexport function useZkLogin() {\n\tconst flow = useEnokiFlow();\n\treturn useStore(flow.$zkLoginState);\n}\n\nexport function useAuthCallback() {\n\tconst flow = useEnokiFlow();\n\tconst [handled, setHandled] = useState(false);\n\tconst [hash, setHash] = useState<string | null>(null);\n\n\tuseEffect(() => {\n\t\tconst listener = () => setHash(window.location.hash.slice(1).trim());\n\t\tlistener();\n\n\t\twindow.addEventListener('hashchange', listener);\n\t\treturn () => window.removeEventListener('hashchange', listener);\n\t}, []);\n\n\tuseEffect(() => {\n\t\tif (!hash) return;\n\n\t\t(async () => {\n\t\t\ttry {\n\t\t\t\tawait flow.handleAuthCallback(hash);\n\n\t\t\t\twindow.location.hash = '';\n\t\t\t} finally {\n\t\t\t\tsetHandled(true);\n\t\t\t}\n\t\t})();\n\t}, [hash, flow]);\n\n\treturn handled;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBQ;AAfR,mBAAyB;AAEzB,IAAAA,gBAA+D;AAG/D,uBAA0B;AAE1B,MAAM,uBAAmB,6BAAgC,IAAI;AAMtD,SAAS,kBAAkB,EAAE,UAAU,GAAG,OAAO,GAA2B;AAClF,QAAM,CAAC,SAAS,QAAI,wBAAS,MAAM,IAAI,2BAAU,MAAM,CAAC;AACxD,SAAO,4CAAC,iBAAiB,UAAjB,EAA0B,OAAO,WAAY,UAAS;AAC/D;AAEO,SAAS,eAAe;AAC9B,QAAM,cAAU,0BAAW,gBAAgB;AAC3C,MAAI,CAAC,SAAS;AACb,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACtD;AACA,SAAO;AACR;AAEO,SAAS,aAAa;AAC5B,QAAM,OAAO,aAAa;AAC1B,aAAO,uBAAS,KAAK,aAAa;AACnC;AAEO,SAAS,kBAAkB;AACjC,QAAM,OAAO,aAAa;AAC1B,QAAM,CAAC,SAAS,UAAU,QAAI,wBAAS,KAAK;AAC5C,QAAM,CAAC,MAAM,OAAO,QAAI,wBAAwB,IAAI;AAEpD,+BAAU,MAAM;AACf,UAAM,WAAW,MAAM,QAAQ,OAAO,SAAS,KAAK,MAAM,CAAC,EAAE,KAAK,CAAC;AACnE,aAAS;AAET,WAAO,iBAAiB,cAAc,QAAQ;AAC9C,WAAO,MAAM,OAAO,oBAAoB,cAAc,QAAQ;AAAA,EAC/D,GAAG,CAAC,CAAC;AAEL,+BAAU,MAAM;AACf,QAAI,CAAC;AAAM;AAEX,KAAC,YAAY;AACZ,UAAI;AACH,cAAM,KAAK,mBAAmB,IAAI;AAElC,eAAO,SAAS,OAAO;AAAA,MACxB,UAAE;AACD,mBAAW,IAAI;AAAA,MAChB;AAAA,IACD,GAAG;AAAA,EACJ,GAAG,CAAC,MAAM,IAAI,CAAC;AAEf,SAAO;AACR;",
|
|
6
6
|
"names": ["import_react"]
|
|
7
7
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, GetAppApiInput, GetAppApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse } from './type.js';
|
|
1
|
+
import type { CreateSponsoredTransactionBlockApiInput, CreateSponsoredTransactionBlockApiResponse, CreateZkLoginNonceApiInput, CreateZkLoginNonceApiResponse, CreateZkLoginZkpApiInput, CreateZkLoginZkpApiResponse, GetAppApiInput, GetAppApiResponse, GetZkLoginApiInput, GetZkLoginApiResponse } from './type.js';
|
|
2
2
|
export interface EnokiClientConfig {
|
|
3
3
|
/** The API key for the Enoki app, available in the Enoki Portal. */
|
|
4
4
|
apiKey: string;
|
|
@@ -15,4 +15,5 @@ export declare class EnokiClient {
|
|
|
15
15
|
getZkLogin(input: GetZkLoginApiInput): Promise<GetZkLoginApiResponse>;
|
|
16
16
|
createZkLoginNonce(input: CreateZkLoginNonceApiInput): Promise<CreateZkLoginNonceApiResponse>;
|
|
17
17
|
createZkLoginZkp(input: CreateZkLoginZkpApiInput): Promise<CreateZkLoginZkpApiResponse>;
|
|
18
|
+
createSponsoredTransactionBlock(input: CreateSponsoredTransactionBlockApiInput): Promise<CreateSponsoredTransactionBlockApiResponse>;
|
|
18
19
|
}
|
|
@@ -55,7 +55,7 @@ class EnokiClient {
|
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
createZkLoginZkp(input) {
|
|
58
|
-
return __privateMethod(this, _fetch, fetch_fn).call(this, "zklogin/
|
|
58
|
+
return __privateMethod(this, _fetch, fetch_fn).call(this, "zklogin/zkp", {
|
|
59
59
|
method: "POST",
|
|
60
60
|
headers: {
|
|
61
61
|
[ZKLOGIN_HEADER]: input.jwt
|
|
@@ -67,6 +67,18 @@ class EnokiClient {
|
|
|
67
67
|
})
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
|
+
createSponsoredTransactionBlock(input) {
|
|
71
|
+
return __privateMethod(this, _fetch, fetch_fn).call(this, "transaction-blocks/sponsor", {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: {
|
|
74
|
+
[ZKLOGIN_HEADER]: input.jwt
|
|
75
|
+
},
|
|
76
|
+
body: JSON.stringify({
|
|
77
|
+
network: input.network,
|
|
78
|
+
transactionBlockKindBytes: input.transactionBlockKindBytes
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
}
|
|
70
82
|
}
|
|
71
83
|
_version = new WeakMap();
|
|
72
84
|
_apiUrl = new WeakMap();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/EnokiClient/index.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type {\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\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\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\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}\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\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\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateZkLoginZkp(input: CreateZkLoginZkpApiInput) {\n\t\treturn this.#fetch<CreateZkLoginZkpApiResponse>('zklogin/
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type {\n\tCreateSponsoredTransactionBlockApiInput,\n\tCreateSponsoredTransactionBlockApiResponse,\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\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\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\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}\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\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\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\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\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\tcreateSponsoredTransactionBlock(input: CreateSponsoredTransactionBlockApiInput) {\n\t\treturn this.#fetch<CreateSponsoredTransactionBlockApiResponse>('transaction-blocks/sponsor', {\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\ttransactionBlockKindBytes: input.transactionBlockKindBytes,\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 Error('Failed to fetch');\n\t\t}\n\n\t\tconst { data } = await res.json();\n\n\t\treturn data as T;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAgBA,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;AAahB,MAAM,YAAY;AAAA,EAKxB,YAA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mB,eAAC,MAAc,MAA+B;AACtE,QAAM,MAAM,MAAM,MAAM,GAAG,mBAAK,YAAW,mBAAK,aAAY,QAAQ;AAAA,IACnE,GAAG;AAAA,IACH,SAAS;AAAA,MACR,GAAG,KAAK;AAAA,MACR,eAAe,UAAU,mBAAK;AAAA,MAC9B,gBAAgB;AAAA,MAChB,cAAc,OAAO,WAAW;AAAA,IACjC;AAAA,EACD,CAAC;AAED,MAAI,CAAC,IAAI,IAAI;AACZ,UAAM,IAAI,MAAM,iBAAiB;AAAA,EAClC;AAEA,QAAM,EAAE,KAAK,IAAI,MAAM,IAAI,KAAK;AAEhC,SAAO;AACR;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -34,3 +34,15 @@ export interface CreateZkLoginZkpApiInput {
|
|
|
34
34
|
}
|
|
35
35
|
export interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {
|
|
36
36
|
}
|
|
37
|
+
export interface CreateSponsoredTransactionBlockApiInput {
|
|
38
|
+
network?: 'mainnet' | 'testnet';
|
|
39
|
+
jwt: string;
|
|
40
|
+
transactionBlockKindBytes: string;
|
|
41
|
+
}
|
|
42
|
+
export interface CreateSponsoredTransactionBlockApiResponse {
|
|
43
|
+
bytes: string;
|
|
44
|
+
signature: string;
|
|
45
|
+
digest: string;
|
|
46
|
+
expireAtTime: number;
|
|
47
|
+
expireAfterEpoch: number;
|
|
48
|
+
}
|
package/dist/esm/EnokiFlow.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ExecuteTransactionBlockParams, SuiClient } from '@mysten/sui.js/client';
|
|
2
|
+
import type { TransactionBlock } from '@mysten/sui.js/transactions';
|
|
1
3
|
import type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';
|
|
2
4
|
import type { WritableAtom } from 'nanostores';
|
|
3
5
|
import type { Encryption } from './encryption.js';
|
|
@@ -47,4 +49,9 @@ export declare class EnokiFlow {
|
|
|
47
49
|
logout(): Promise<void>;
|
|
48
50
|
getProof(): Promise<import("./EnokiClient/type.js").CreateZkLoginZkpApiResponse>;
|
|
49
51
|
getKeypair(): Promise<EnokiKeypair>;
|
|
52
|
+
sponsorAndExecuteTransactionBlock({ network, transactionBlock, client, ...options }: {
|
|
53
|
+
network?: 'mainnet' | 'testnet';
|
|
54
|
+
transactionBlock: TransactionBlock;
|
|
55
|
+
client: SuiClient;
|
|
56
|
+
} & Omit<ExecuteTransactionBlockParams, 'signature' | 'transactionBlock'>): Promise<import("@mysten/sui.js/client").SuiTransactionBlockResponse>;
|
|
50
57
|
}
|
package/dist/esm/EnokiFlow.js
CHANGED
|
@@ -20,22 +20,23 @@ var __privateMethod = (obj, member, method) => {
|
|
|
20
20
|
__accessCheck(obj, member, "access private method");
|
|
21
21
|
return method;
|
|
22
22
|
};
|
|
23
|
-
var _enokiClient, _encryption, _encryptionKey, _store, _zkLoginSessionInitialized, _zkLoginSession, _setSession, setSession_fn;
|
|
23
|
+
var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _zkLoginSessionInitialized, _zkLoginSession, _setSession, setSession_fn;
|
|
24
24
|
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
|
|
25
|
-
import { fromB64 } from "@mysten/sui.js/utils";
|
|
25
|
+
import { fromB64, toB64 } from "@mysten/sui.js/utils";
|
|
26
26
|
import { decodeJwt } from "jose";
|
|
27
27
|
import { atom, onSet } from "nanostores";
|
|
28
28
|
import { createDefaultEncryption } from "./encryption.js";
|
|
29
29
|
import { EnokiClient } from "./EnokiClient/index.js";
|
|
30
30
|
import { EnokiKeypair } from "./EnokiKeypair.js";
|
|
31
31
|
import { createSessionStorage } from "./stores.js";
|
|
32
|
-
const
|
|
33
|
-
STATE:
|
|
34
|
-
SESSION:
|
|
35
|
-
};
|
|
32
|
+
const createStorageKeys = (apiKey) => ({
|
|
33
|
+
STATE: `@enoki/flow/state/${apiKey}`,
|
|
34
|
+
SESSION: `@enoki/flow/session/${apiKey}`
|
|
35
|
+
});
|
|
36
36
|
class EnokiFlow {
|
|
37
37
|
constructor(config) {
|
|
38
38
|
__privateAdd(this, _setSession);
|
|
39
|
+
__privateAdd(this, _storageKeys, void 0);
|
|
39
40
|
__privateAdd(this, _enokiClient, void 0);
|
|
40
41
|
__privateAdd(this, _encryption, void 0);
|
|
41
42
|
__privateAdd(this, _encryptionKey, void 0);
|
|
@@ -49,9 +50,10 @@ class EnokiFlow {
|
|
|
49
50
|
__privateSet(this, _encryptionKey, config.apiKey);
|
|
50
51
|
__privateSet(this, _encryption, config.encryption ?? createDefaultEncryption());
|
|
51
52
|
__privateSet(this, _store, config.store ?? createSessionStorage());
|
|
53
|
+
__privateSet(this, _storageKeys, createStorageKeys(config.apiKey));
|
|
52
54
|
let storedState = null;
|
|
53
55
|
try {
|
|
54
|
-
const rawStoredValue = __privateGet(this, _store).get(
|
|
56
|
+
const rawStoredValue = __privateGet(this, _store).get(__privateGet(this, _storageKeys).STATE);
|
|
55
57
|
if (rawStoredValue) {
|
|
56
58
|
storedState = JSON.parse(rawStoredValue);
|
|
57
59
|
}
|
|
@@ -61,7 +63,7 @@ class EnokiFlow {
|
|
|
61
63
|
__privateSet(this, _zkLoginSessionInitialized, false);
|
|
62
64
|
__privateSet(this, _zkLoginSession, null);
|
|
63
65
|
onSet(this.$zkLoginState, ({ newValue }) => {
|
|
64
|
-
__privateGet(this, _store).set(
|
|
66
|
+
__privateGet(this, _store).set(__privateGet(this, _storageKeys).STATE, JSON.stringify(newValue));
|
|
65
67
|
});
|
|
66
68
|
}
|
|
67
69
|
get enokiClient() {
|
|
@@ -146,7 +148,7 @@ class EnokiFlow {
|
|
|
146
148
|
return __privateGet(this, _zkLoginSession);
|
|
147
149
|
}
|
|
148
150
|
try {
|
|
149
|
-
const storedValue = __privateGet(this, _store).get(
|
|
151
|
+
const storedValue = __privateGet(this, _store).get(__privateGet(this, _storageKeys).SESSION);
|
|
150
152
|
if (!storedValue)
|
|
151
153
|
return null;
|
|
152
154
|
const state = JSON.parse(
|
|
@@ -164,7 +166,7 @@ class EnokiFlow {
|
|
|
164
166
|
}
|
|
165
167
|
async logout() {
|
|
166
168
|
this.$zkLoginState.set({});
|
|
167
|
-
__privateGet(this, _store).delete(
|
|
169
|
+
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).STATE);
|
|
168
170
|
await __privateMethod(this, _setSession, setSession_fn).call(this, null);
|
|
169
171
|
}
|
|
170
172
|
// TODO: Should this return the proof if it already exists?
|
|
@@ -194,8 +196,8 @@ class EnokiFlow {
|
|
|
194
196
|
return proof;
|
|
195
197
|
}
|
|
196
198
|
async getKeypair() {
|
|
197
|
-
const zkp = await this.getSession();
|
|
198
199
|
await this.getProof();
|
|
200
|
+
const zkp = await this.getSession();
|
|
199
201
|
const { address } = this.$zkLoginState.get();
|
|
200
202
|
if (!address || !zkp || !zkp.proof) {
|
|
201
203
|
throw new Error("Missing required data for keypair generation.");
|
|
@@ -210,7 +212,39 @@ class EnokiFlow {
|
|
|
210
212
|
ephemeralKeypair: Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair))
|
|
211
213
|
});
|
|
212
214
|
}
|
|
215
|
+
async sponsorAndExecuteTransactionBlock({
|
|
216
|
+
network,
|
|
217
|
+
transactionBlock,
|
|
218
|
+
client,
|
|
219
|
+
...options
|
|
220
|
+
}) {
|
|
221
|
+
const session = await this.getSession();
|
|
222
|
+
const keypair = await this.getKeypair();
|
|
223
|
+
const transactionBlockKindBytes = await transactionBlock.build({
|
|
224
|
+
onlyTransactionKind: true,
|
|
225
|
+
client,
|
|
226
|
+
// Theses limits will get verified during the final transaction construction, so we can safely ignore them here:
|
|
227
|
+
limits: {
|
|
228
|
+
maxGasObjects: Infinity,
|
|
229
|
+
maxPureArgumentSize: Infinity,
|
|
230
|
+
maxTxGas: Infinity,
|
|
231
|
+
maxTxSizeBytes: Infinity
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
const { bytes, signature: sponsorSignature } = await __privateGet(this, _enokiClient).createSponsoredTransactionBlock({
|
|
235
|
+
jwt: session.jwt,
|
|
236
|
+
network,
|
|
237
|
+
transactionBlockKindBytes: toB64(transactionBlockKindBytes)
|
|
238
|
+
});
|
|
239
|
+
const userSignature = await keypair.signTransactionBlock(fromB64(bytes));
|
|
240
|
+
return client.executeTransactionBlock({
|
|
241
|
+
transactionBlock: bytes,
|
|
242
|
+
signature: [userSignature.signature, sponsorSignature],
|
|
243
|
+
...options
|
|
244
|
+
});
|
|
245
|
+
}
|
|
213
246
|
}
|
|
247
|
+
_storageKeys = new WeakMap();
|
|
214
248
|
_enokiClient = new WeakMap();
|
|
215
249
|
_encryption = new WeakMap();
|
|
216
250
|
_encryptionKey = new WeakMap();
|
|
@@ -224,9 +258,9 @@ setSession_fn = async function(newValue) {
|
|
|
224
258
|
__privateGet(this, _encryptionKey),
|
|
225
259
|
JSON.stringify(newValue)
|
|
226
260
|
);
|
|
227
|
-
__privateGet(this, _store).set(
|
|
261
|
+
__privateGet(this, _store).set(__privateGet(this, _storageKeys).SESSION, storedValue);
|
|
228
262
|
} else {
|
|
229
|
-
__privateGet(this, _store).delete(
|
|
263
|
+
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).SESSION);
|
|
230
264
|
}
|
|
231
265
|
__privateSet(this, _zkLoginSession, newValue);
|
|
232
266
|
};
|