@getpara/web-sdk 2.0.0-alpha.3 → 2.0.0-alpha.5

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.
@@ -0,0 +1,36 @@
1
+ "use client";
2
+ import "./chunk-M66XENHI.js";
3
+ class LocalStorage {
4
+ constructor() {
5
+ this.get = (key) => {
6
+ if (typeof window !== "undefined") {
7
+ return localStorage.getItem(key) || null;
8
+ }
9
+ return null;
10
+ };
11
+ this.set = (key, value) => {
12
+ if (typeof window !== "undefined") {
13
+ localStorage.setItem(key, value);
14
+ }
15
+ };
16
+ this.removeItem = (key) => {
17
+ if (typeof window !== "undefined") {
18
+ localStorage.removeItem(key);
19
+ }
20
+ };
21
+ this.clear = (prefix) => {
22
+ if (typeof window !== "undefined") {
23
+ for (let i = 0; i < localStorage.length; i++) {
24
+ const key = localStorage.key(i);
25
+ if (key && key.startsWith(prefix)) {
26
+ localStorage.removeItem(key);
27
+ i--;
28
+ }
29
+ }
30
+ }
31
+ };
32
+ }
33
+ }
34
+ export {
35
+ LocalStorage
36
+ };
@@ -0,0 +1,22 @@
1
+ "use client";
2
+ import "./chunk-M66XENHI.js";
3
+ import * as Sentry from "@sentry/browser";
4
+ import ParaCore, { Environment } from "@getpara/core-sdk";
5
+ import { WebUtils } from "./WebUtils.js";
6
+ class Para extends ParaCore {
7
+ constructor(env, apiKey, opts) {
8
+ super(env, apiKey, opts);
9
+ if (env !== Environment.PROD && env !== Environment.DEV) {
10
+ Sentry.init({
11
+ environment: env.toLowerCase(),
12
+ dsn: "https://38f27d4836da617ab9e95cf66b9611d9@o4504568036720640.ingest.us.sentry.io/4508850812944384"
13
+ });
14
+ }
15
+ }
16
+ getPlatformUtils() {
17
+ return new WebUtils();
18
+ }
19
+ }
20
+ export {
21
+ Para
22
+ };
@@ -0,0 +1,36 @@
1
+ "use client";
2
+ import "./chunk-M66XENHI.js";
3
+ class SessionStorage {
4
+ constructor() {
5
+ this.get = (key) => {
6
+ if (typeof window !== "undefined") {
7
+ return sessionStorage.getItem(key) || null;
8
+ }
9
+ return null;
10
+ };
11
+ this.set = (key, value) => {
12
+ if (typeof window !== "undefined") {
13
+ sessionStorage.setItem(key, value);
14
+ }
15
+ };
16
+ this.removeItem = (key) => {
17
+ if (typeof window !== "undefined") {
18
+ sessionStorage.removeItem(key);
19
+ }
20
+ };
21
+ this.clear = (prefix) => {
22
+ if (typeof window !== "undefined") {
23
+ for (let i = 0; i < sessionStorage.length; i++) {
24
+ const key = sessionStorage.key(i);
25
+ if (key && key.startsWith(prefix)) {
26
+ sessionStorage.removeItem(key);
27
+ i--;
28
+ }
29
+ }
30
+ }
31
+ };
32
+ }
33
+ }
34
+ export {
35
+ SessionStorage
36
+ };
@@ -2,11 +2,12 @@
2
2
  import { Ctx, PlatformUtils, SignatureRes, PopupType } from '@getpara/core-sdk';
3
3
  import { LocalStorage } from './LocalStorage.js';
4
4
  import { SessionStorage } from './SessionStorage.js';
5
- import { BackupKitEmailProps, WalletType } from '@getpara/user-management-client';
5
+ import { BackupKitEmailProps, TWalletType, SDKType } from '@getpara/user-management-client';
6
6
  import { TPregenIdentifierType } from '@getpara/core-sdk';
7
7
  export declare class WebUtils implements PlatformUtils {
8
+ sdkType: SDKType;
8
9
  getPrivateKey(ctx: Ctx, userId: string, walletId: string, share: string, sessionCookie: string): Promise<string>;
9
- keygen(ctx: Ctx, userId: string, type: Exclude<WalletType, WalletType.SOLANA>, secretKey: string | null, // should be acceptable as null in RN as we don't pre-gen them
10
+ keygen(ctx: Ctx, userId: string, type: Exclude<TWalletType, 'SOLANA'>, secretKey: string | null, // should be acceptable as null in RN as we don't pre-gen them
10
11
  sessionCookie: string, emailProps?: BackupKitEmailProps): Promise<{
11
12
  signer: string;
12
13
  walletId: string;
@@ -14,7 +15,7 @@ export declare class WebUtils implements PlatformUtils {
14
15
  refresh(ctx: Ctx, sessionCookie: string, userId: string, walletId: string, share: string, oldPartnerId?: string, newPartnerId?: string, keyShareProtocolId?: string): Promise<{
15
16
  signer: string;
16
17
  }>;
17
- preKeygen(ctx: Ctx, partnerId: string | undefined, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<WalletType, WalletType.SOLANA>, secretKey: string | null, // should be acceptable as null in RN as we don't pre-gen them
18
+ preKeygen(ctx: Ctx, partnerId: string | undefined, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<TWalletType, 'SOLANA'>, secretKey: string | null, // should be acceptable as null in RN as we don't pre-gen them
18
19
  sessionCookie: string): Promise<{
19
20
  signer: string;
20
21
  walletId: string;
@@ -0,0 +1,110 @@
1
+ "use client";
2
+ import "./chunk-M66XENHI.js";
3
+ import { PopupType } from "@getpara/core-sdk";
4
+ import { LocalStorage } from "./LocalStorage.js";
5
+ import { SessionStorage } from "./SessionStorage.js";
6
+ import { keygen, preKeygen, ed25519Keygen, ed25519PreKeygen, refresh } from "./wallet/keygen.js";
7
+ import { signMessage, sendTransaction, signTransaction, ed25519Sign } from "./wallet/signing.js";
8
+ import { getPrivateKey } from "./wallet/privateKey.js";
9
+ class WebUtils {
10
+ constructor() {
11
+ this.sdkType = "WEB";
12
+ this.localStorage = new LocalStorage();
13
+ this.sessionStorage = new SessionStorage();
14
+ this.secureStorage = null;
15
+ this.isSyncStorage = true;
16
+ this.disableProviderModal = false;
17
+ }
18
+ getPrivateKey(ctx, userId, walletId, share, sessionCookie) {
19
+ return getPrivateKey(ctx, userId, walletId, share, sessionCookie);
20
+ }
21
+ keygen(ctx, userId, type, secretKey, sessionCookie, emailProps = {}) {
22
+ return keygen(ctx, userId, type, secretKey, true, sessionCookie, emailProps);
23
+ }
24
+ refresh(ctx, sessionCookie, userId, walletId, share, oldPartnerId, newPartnerId, keyShareProtocolId) {
25
+ return refresh(ctx, sessionCookie, userId, walletId, share, oldPartnerId, newPartnerId, keyShareProtocolId);
26
+ }
27
+ preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey, sessionCookie) {
28
+ return preKeygen(ctx, pregenIdentifier, pregenIdentifierType, type, secretKey, false, partnerId, sessionCookie);
29
+ }
30
+ signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKLS, cosmosSignDoc) {
31
+ return signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKLS, cosmosSignDoc);
32
+ }
33
+ signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
34
+ return signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS);
35
+ }
36
+ sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
37
+ return sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS);
38
+ }
39
+ signHash(_address, _hash) {
40
+ throw new Error("not implemented");
41
+ }
42
+ ed25519Keygen(ctx, userId, sessionCookie, emailProps) {
43
+ return ed25519Keygen(ctx, userId, sessionCookie, emailProps);
44
+ }
45
+ ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType, sessionCookie) {
46
+ return ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType, sessionCookie);
47
+ }
48
+ ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie) {
49
+ return ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie);
50
+ }
51
+ openPopup(popupUrl, opts) {
52
+ if (typeof window === "undefined") {
53
+ return;
54
+ }
55
+ if (opts) {
56
+ const { type } = opts;
57
+ const popUpWidth = 550;
58
+ let popUpHeight;
59
+ switch (type) {
60
+ case PopupType.LOGIN_PASSKEY: {
61
+ popUpHeight = 798;
62
+ break;
63
+ }
64
+ case PopupType.CREATE_PASSKEY: {
65
+ popUpHeight = 464;
66
+ break;
67
+ }
68
+ case PopupType.SIGN_MESSAGE_REVIEW: {
69
+ popUpHeight = 585;
70
+ break;
71
+ }
72
+ case PopupType.SIGN_TRANSACTION_REVIEW: {
73
+ popUpHeight = 750;
74
+ break;
75
+ }
76
+ case PopupType.OAUTH:
77
+ default: {
78
+ popUpHeight = 768;
79
+ break;
80
+ }
81
+ }
82
+ const dualScreenLeft = window.screenLeft !== void 0 ? window.screenLeft : window.screenX;
83
+ const dualScreenTop = window.screenTop !== void 0 ? window.screenTop : window.screenY;
84
+ const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
85
+ const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
86
+ const left = (width - popUpWidth) / 2 + dualScreenLeft;
87
+ const top = (height - popUpHeight) / 2 + dualScreenTop;
88
+ const windowFeatures = `toolbar=no, menubar=no, width=${popUpWidth},
89
+ height=${popUpHeight}, top=${top}, left=${left}`;
90
+ let popupWindow = window.open(popupUrl, type.toString(), windowFeatures);
91
+ if (!popupWindow) {
92
+ setTimeout(() => {
93
+ popupWindow = window.open(popupUrl, "_blank");
94
+ }, 0);
95
+ }
96
+ return popupWindow;
97
+ } else {
98
+ const popupWindow = window.open(popupUrl, "popup", "popup=true,width=400,height=500");
99
+ if (!popupWindow) {
100
+ setTimeout(() => {
101
+ window.open(popupUrl, "_blank");
102
+ }, 0);
103
+ }
104
+ return popupWindow;
105
+ }
106
+ }
107
+ }
108
+ export {
109
+ WebUtils
110
+ };
@@ -0,0 +1,25 @@
1
+ "use client";
2
+ var __async = (__this, __arguments, generator) => {
3
+ return new Promise((resolve, reject) => {
4
+ var fulfilled = (value) => {
5
+ try {
6
+ step(generator.next(value));
7
+ } catch (e) {
8
+ reject(e);
9
+ }
10
+ };
11
+ var rejected = (value) => {
12
+ try {
13
+ step(generator.throw(value));
14
+ } catch (e) {
15
+ reject(e);
16
+ }
17
+ };
18
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
19
+ step((generator = generator.apply(__this, __arguments)).next());
20
+ });
21
+ };
22
+
23
+ export {
24
+ __async
25
+ };
@@ -0,0 +1,162 @@
1
+ "use client";
2
+ import {
3
+ __async
4
+ } from "../chunk-M66XENHI.js";
5
+ import base64url from "base64url";
6
+ import * as cbor from "cbor-web";
7
+ import forge from "node-forge";
8
+ import { getPortalDomain } from "@getpara/core-sdk";
9
+ const ES256_ALGORITHM = -7;
10
+ const RS256_ALGORITHM = -257;
11
+ function publicKeyCredentialToJSON(pubKeyCred) {
12
+ if (pubKeyCred instanceof ArrayBuffer || ArrayBuffer.isView(pubKeyCred)) {
13
+ return base64url.encode(pubKeyCred);
14
+ } else if (pubKeyCred instanceof Array) {
15
+ return pubKeyCred.map(publicKeyCredentialToJSON);
16
+ } else if (pubKeyCred instanceof Object) {
17
+ const obj = {};
18
+ for (const key in pubKeyCred) {
19
+ obj[key] = publicKeyCredentialToJSON(pubKeyCred[key]);
20
+ }
21
+ return obj;
22
+ } else return pubKeyCred;
23
+ }
24
+ function parseMakeCredAuthData(buffer) {
25
+ const rpIdHash = buffer.slice(0, 32);
26
+ buffer = buffer.slice(32);
27
+ const flagsBuf = buffer.slice(0, 1);
28
+ buffer = buffer.slice(1);
29
+ const flags = flagsBuf[0];
30
+ const counterBuf = buffer.slice(0, 4);
31
+ buffer = buffer.slice(4);
32
+ const counter = counterBuf.readUInt32BE(0);
33
+ const aaguid = buffer.slice(0, 16);
34
+ buffer = buffer.slice(16);
35
+ const credIDLenBuf = buffer.slice(0, 2);
36
+ buffer = buffer.slice(2);
37
+ const credIDLen = credIDLenBuf.readUInt16BE(0);
38
+ const credID = buffer.slice(0, credIDLen);
39
+ buffer = buffer.slice(credIDLen);
40
+ const COSEPublicKey = buffer;
41
+ return {
42
+ rpIdHash,
43
+ flagsBuf,
44
+ flags,
45
+ counter,
46
+ counterBuf,
47
+ aaguid,
48
+ credID,
49
+ COSEPublicKey
50
+ };
51
+ }
52
+ function parseAttestationObject(attestationObject) {
53
+ const attestationObjectBuffer = base64url.toBuffer(attestationObject);
54
+ return cbor.decodeAllSync(attestationObjectBuffer)[0];
55
+ }
56
+ function COSEECDSAtoPKCS(COSEPublicKey) {
57
+ const coseStruct = cbor.decodeAllSync(COSEPublicKey)[0];
58
+ const tag = Buffer.from([4]);
59
+ const x = coseStruct.get(-2);
60
+ const y = coseStruct.get(-3);
61
+ return Buffer.concat([tag, x, y]);
62
+ }
63
+ function COSERSAtoPKCS(COSEPublicKey) {
64
+ const coseStruct = cbor.decodeAllSync(COSEPublicKey)[0];
65
+ const n = coseStruct.get(-1);
66
+ const e = coseStruct.get(-2);
67
+ const nForge = forge.util.createBuffer(n.toString("binary"));
68
+ const eForge = forge.util.createBuffer(e.toString("binary"));
69
+ const publicKey = forge.pki.setRsaPublicKey(
70
+ new forge.jsbn.BigInteger(nForge.toHex(), 16),
71
+ new forge.jsbn.BigInteger(eForge.toHex(), 16)
72
+ );
73
+ return Buffer.from(forge.pki.publicKeyToPem(publicKey), "utf-8");
74
+ }
75
+ function parseCredentialCreationRes(creds, algorithm) {
76
+ const parsedAttestation = parseAttestationObject(creds.response.attestationObject);
77
+ const { COSEPublicKey, aaguid } = parseMakeCredAuthData(parsedAttestation.authData);
78
+ if (algorithm === RS256_ALGORITHM) {
79
+ return {
80
+ cosePublicKey: base64url.encode(COSERSAtoPKCS(COSEPublicKey)),
81
+ clientDataJSON: creds.response.clientDataJSON,
82
+ aaguid: aaguid.toString("hex")
83
+ };
84
+ }
85
+ return {
86
+ cosePublicKey: base64url.encode(COSEECDSAtoPKCS(COSEPublicKey)),
87
+ clientDataJSON: creds.response.clientDataJSON,
88
+ aaguid: aaguid.toString("hex")
89
+ };
90
+ }
91
+ function generateUserHandle() {
92
+ const userHandle = new Uint8Array(32);
93
+ window.crypto.getRandomValues(userHandle);
94
+ return userHandle;
95
+ }
96
+ function createCredential(env, userId, identifier, isE2E) {
97
+ return __async(this, null, function* () {
98
+ if (typeof navigator === "undefined") {
99
+ return;
100
+ }
101
+ const userHandle = generateUserHandle();
102
+ const createCredentialDefaultArgs = {
103
+ publicKey: {
104
+ authenticatorSelection: {
105
+ authenticatorAttachment: "platform",
106
+ requireResidentKey: true,
107
+ residentKey: "required",
108
+ userVerification: "required"
109
+ },
110
+ rp: {
111
+ id: getPortalDomain(env, isE2E),
112
+ name: "Para"
113
+ },
114
+ user: {
115
+ id: userHandle,
116
+ name: identifier,
117
+ displayName: identifier
118
+ },
119
+ pubKeyCredParams: [
120
+ { type: "public-key", alg: ES256_ALGORITHM },
121
+ // RS256_ALGORITHM should only be needed for windows hello
122
+ { type: "public-key", alg: RS256_ALGORITHM }
123
+ ],
124
+ attestation: "direct",
125
+ timeout: 6e4,
126
+ // TODO: don't think we really get value from verifying this, but should revisit
127
+ challenge: Buffer.from(userId, "utf-8")
128
+ }
129
+ };
130
+ const credential = yield navigator.credentials.create(createCredentialDefaultArgs);
131
+ const algorithm = credential.response.getPublicKeyAlgorithm ? credential.response.getPublicKeyAlgorithm() : ES256_ALGORITHM;
132
+ const userHandleEncoded = base64url.encode(Buffer.from(userHandle));
133
+ return {
134
+ creds: publicKeyCredentialToJSON(credential),
135
+ userHandle: userHandleEncoded,
136
+ algorithm
137
+ };
138
+ });
139
+ }
140
+ function generateSignature(env, challenge, allowedPublicKeys, isE2E) {
141
+ return __async(this, null, function* () {
142
+ const getCredentialDefaultArgs = {
143
+ publicKey: {
144
+ timeout: 6e4,
145
+ challenge: Buffer.from(challenge, "base64"),
146
+ allowCredentials: allowedPublicKeys.map((key) => ({
147
+ id: base64url.toBuffer(key),
148
+ type: "public-key"
149
+ })),
150
+ userVerification: "required",
151
+ rpId: getPortalDomain(env, isE2E)
152
+ }
153
+ };
154
+ const assertation = yield navigator.credentials.get(getCredentialDefaultArgs);
155
+ return publicKeyCredentialToJSON(assertation);
156
+ });
157
+ }
158
+ export {
159
+ createCredential,
160
+ generateSignature,
161
+ parseCredentialCreationRes
162
+ };
package/dist/errors.js ADDED
@@ -0,0 +1,12 @@
1
+ "use client";
2
+ import "./chunk-M66XENHI.js";
3
+ class TransactionReviewError extends Error {
4
+ constructor(transactionReviewUrl) {
5
+ super("transaction review error");
6
+ this.name = "TransactionReviewError";
7
+ this.transactionReviewUrl = transactionReviewUrl;
8
+ }
9
+ }
10
+ export {
11
+ TransactionReviewError
12
+ };