@dstorage-tech/dstorage-sdk 0.0.4
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/LICENSE +21 -0
- package/README.md +530 -0
- package/dist/KeypairEncryptionAdapter-AE75YC4W-AOWNSH2M.mjs +9 -0
- package/dist/KeypairEncryptionAdapter-AE75YC4W-BAVHRNNJ.mjs +1 -0
- package/dist/KeypairEncryptionAdapter-AE75YC4W-BEYO4RUO.mjs +1 -0
- package/dist/KeypairEncryptionAdapter-AE75YC4W-VAOH6AAL.mjs +9 -0
- package/dist/KeypairEncryptionAdapter-XUKCLRFU-6ERIZN2M.mjs +1 -0
- package/dist/KeypairEncryptionAdapter-XUKCLRFU-AYDAZ2TZ.mjs +9 -0
- package/dist/KeypairEncryptionAdapter-XUKCLRFU-ERPYK5S4.mjs +8 -0
- package/dist/MnemonicEncryptionAdapter-J6IE5OKA-6DFVUVD6.mjs +1 -0
- package/dist/MnemonicEncryptionAdapter-J6IE5OKA-O5AJPX42.mjs +1 -0
- package/dist/MnemonicEncryptionAdapter-J6IE5OKA-SI6TBLTQ.mjs +9 -0
- package/dist/MnemonicEncryptionAdapter-J6IE5OKA-WEPFOTSJ.mjs +9 -0
- package/dist/MnemonicEncryptionAdapter-RCHUETBP-DZPBGQG6.mjs +8 -0
- package/dist/MnemonicEncryptionAdapter-RCHUETBP-ELAMJMMO.mjs +1 -0
- package/dist/MnemonicEncryptionAdapter-RCHUETBP-M7NHGSHO.mjs +9 -0
- package/dist/PasswordEncryptionAdapter-CWOXUGR4-2YZ64B4A.mjs +1 -0
- package/dist/PasswordEncryptionAdapter-CWOXUGR4-DCEGC2NX.mjs +1 -0
- package/dist/PasswordEncryptionAdapter-CWOXUGR4-QEGGYPFJ.mjs +13 -0
- package/dist/PasswordEncryptionAdapter-CWOXUGR4-ZYUBUYUI.mjs +13 -0
- package/dist/PasswordEncryptionAdapter-ECHND672-3DEFO6FP.mjs +13 -0
- package/dist/PasswordEncryptionAdapter-ECHND672-MUTHAMZY.mjs +12 -0
- package/dist/PasswordEncryptionAdapter-ECHND672-XVBPHPPT.mjs +1 -0
- package/dist/browser.d.mts +2418 -0
- package/dist/browser.mjs +5175 -0
- package/dist/chunk-25I6ZXUC.mjs +9403 -0
- package/dist/chunk-4X5K43JF.mjs +62 -0
- package/dist/chunk-4YMF57VY.mjs +1 -0
- package/dist/chunk-5MZ7FPPY.mjs +1 -0
- package/dist/chunk-5P4EW2YL.mjs +1 -0
- package/dist/chunk-5QOKJEH3.mjs +8 -0
- package/dist/chunk-5RW7SGA7.mjs +1 -0
- package/dist/chunk-62RSKZ6T.mjs +118 -0
- package/dist/chunk-6IAQVMSM.mjs +102 -0
- package/dist/chunk-BBJCQFWP.mjs +275 -0
- package/dist/chunk-BWLEAAFW.mjs +2599 -0
- package/dist/chunk-EYF3263P.mjs +2049 -0
- package/dist/chunk-FJ7LSZOW.mjs +5537 -0
- package/dist/chunk-GUYENY5N.mjs +1 -0
- package/dist/chunk-HDD6SUU4.mjs +276 -0
- package/dist/chunk-JMPRZK5D.mjs +62 -0
- package/dist/chunk-L5QD6GHB.mjs +101 -0
- package/dist/chunk-O37NHERH.mjs +17 -0
- package/dist/chunk-OLGVVM5Y.mjs +102 -0
- package/dist/chunk-PKHRAYFD.mjs +1 -0
- package/dist/chunk-PLAOCKSS.mjs +101 -0
- package/dist/chunk-QKA2DCR6.mjs +118 -0
- package/dist/chunk-RPSRFFXN.mjs +275 -0
- package/dist/chunk-TESUOXRJ.mjs +1 -0
- package/dist/chunk-TMLXCRJU.mjs +1 -0
- package/dist/chunk-TNRKN4LB.mjs +1 -0
- package/dist/chunk-TSZ37JDY.mjs +2596 -0
- package/dist/chunk-U3MS6KMF.mjs +1 -0
- package/dist/chunk-UBALMYIY.mjs +2049 -0
- package/dist/chunk-UDYXMII2.mjs +121 -0
- package/dist/chunk-UJCSKKID.mjs +30 -0
- package/dist/chunk-UOOF4V2Y.mjs +1 -0
- package/dist/chunk-VDZ7F7MP.mjs +1329 -0
- package/dist/chunk-X4LEBNK6.mjs +276 -0
- package/dist/chunk-Z2TH6U4C.mjs +3 -0
- package/dist/chunk-Z6DUAI5B.mjs +121 -0
- package/dist/chunk-ZEPZ4QDD.mjs +1 -0
- package/dist/chunk-ZGUY5EIR.mjs +1 -0
- package/dist/cjs-RMZ3L2IO.mjs +5 -0
- package/dist/contract-api-AO4RGPLT-4HRCMSCE.mjs +279 -0
- package/dist/contract-api-AO4RGPLT-ADH6ZDKC.mjs +279 -0
- package/dist/contract-api-AO4RGPLT-ENC72WCG.mjs +280 -0
- package/dist/contract-api-AO4RGPLT-PAMIPEI3.mjs +1 -0
- package/dist/contract-api-AO4RGPLT-W3RSVP5A.mjs +1 -0
- package/dist/contract-api-AO4RGPLT-WENYDV6I.mjs +1 -0
- package/dist/contract-api-TJ7JPHZA-6FUBJ6DS.mjs +279 -0
- package/dist/contract-api-TJ7JPHZA-ZWP3EH7D.mjs +279 -0
- package/dist/contracts/dataregistry/managed/keys/removeReference.prover +0 -0
- package/dist/contracts/dataregistry/managed/keys/removeReference.verifier +0 -0
- package/dist/contracts/dataregistry/managed/keys/storeReference.prover +0 -0
- package/dist/contracts/dataregistry/managed/keys/storeReference.verifier +0 -0
- package/dist/contracts/dataregistry/managed/keys/updateReference.prover +0 -0
- package/dist/contracts/dataregistry/managed/keys/updateReference.verifier +0 -0
- package/dist/contracts/dataregistry/managed/zkir/removeReference.bzkir +0 -0
- package/dist/contracts/dataregistry/managed/zkir/removeReference.zkir +138 -0
- package/dist/contracts/dataregistry/managed/zkir/storeReference.bzkir +0 -0
- package/dist/contracts/dataregistry/managed/zkir/storeReference.zkir +158 -0
- package/dist/contracts/dataregistry/managed/zkir/updateReference.bzkir +0 -0
- package/dist/contracts/dataregistry/managed/zkir/updateReference.zkir +190 -0
- package/dist/dataregistry-simulator-E6RTGGWT-5YEGBW7P.mjs +108 -0
- package/dist/dataregistry-simulator-E6RTGGWT-DJS4MHBD.mjs +109 -0
- package/dist/dataregistry-simulator-E6RTGGWT-QGCYR3VF.mjs +1 -0
- package/dist/dist-655VYUPJ.mjs +1 -0
- package/dist/dist-INTAFXQ7.mjs +65 -0
- package/dist/dist-MEZZWKP4.mjs +1 -0
- package/dist/dist-MOLWJ4Y7.mjs +64 -0
- package/dist/dist-UD4WRMYJ.mjs +65 -0
- package/dist/errors.d.mts +117 -0
- package/dist/errors.d.ts +117 -0
- package/dist/errors.js +2308 -0
- package/dist/errors.mjs +2278 -0
- package/dist/esm5-AGBDHMNS.mjs +1 -0
- package/dist/esm5-GFOBLUFI.mjs +350 -0
- package/dist/in-memory-private-state-NGOP37V7-DXWZE42Q.mjs +48 -0
- package/dist/in-memory-private-state-NGOP37V7-QXT3RUEH.mjs +49 -0
- package/dist/in-memory-private-state-NGOP37V7-XFJFNFCQ.mjs +1 -0
- package/dist/index.d.mts +2843 -0
- package/dist/index.d.ts +2843 -0
- package/dist/index.js +19639 -0
- package/dist/index.mjs +5177 -0
- package/package.json +87 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EncryptionErrorCode
|
|
3
|
+
} from "./chunk-O37NHERH.mjs";
|
|
4
|
+
import {
|
|
5
|
+
DStorageError,
|
|
6
|
+
mlkemGenerateKeypair,
|
|
7
|
+
mlkemUnwrapDek,
|
|
8
|
+
mlkemWrapDek
|
|
9
|
+
} from "./chunk-BWLEAAFW.mjs";
|
|
10
|
+
|
|
11
|
+
// ../encryption/dist/chunk-55C752ON.mjs
|
|
12
|
+
var KeypairEncryptionAdapter = class _KeypairEncryptionAdapter {
|
|
13
|
+
constructor(pk, sk, context, variant) {
|
|
14
|
+
this.publicKey = pk;
|
|
15
|
+
this.secretKey = sk;
|
|
16
|
+
this.context = context;
|
|
17
|
+
this.variant = variant;
|
|
18
|
+
this.name = variant;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Upload-only adapter — holds only the Public Key.
|
|
22
|
+
* wrapDek() works normally; unwrapDek() throws (no SK available).
|
|
23
|
+
*/
|
|
24
|
+
static fromPublicKey(pk, context, variant = "mlkem768") {
|
|
25
|
+
return new _KeypairEncryptionAdapter(pk, void 0, context, variant);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Full adapter — holds both Public Key and Secret Key.
|
|
29
|
+
* Both wrapDek() and unwrapDek() work.
|
|
30
|
+
*/
|
|
31
|
+
static fromKeypair(pk, sk, context, variant = "mlkem768") {
|
|
32
|
+
return new _KeypairEncryptionAdapter(pk, sk, context, variant);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Generate a fresh random ML-KEM keypair.
|
|
36
|
+
*
|
|
37
|
+
* Returns the adapter (with both PK+SK) plus the raw keys so the caller
|
|
38
|
+
* can persist the secret key and share the public key separately.
|
|
39
|
+
*
|
|
40
|
+
* @param variant Default: "mlkem768".
|
|
41
|
+
*/
|
|
42
|
+
static generateKeypair(context, variant = "mlkem768") {
|
|
43
|
+
const { pk, sk } = mlkemGenerateKeypair(variant);
|
|
44
|
+
return {
|
|
45
|
+
adapter: new _KeypairEncryptionAdapter(pk, sk, context, variant),
|
|
46
|
+
publicKey: pk,
|
|
47
|
+
secretKey: sk
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Deterministically derive a ML-KEM keypair from a BIP-39 mnemonic phrase or hex seed.
|
|
52
|
+
*
|
|
53
|
+
* Delegates to MnemonicEncryptionAdapter.intoKeypairEncryptionAdapter() so all
|
|
54
|
+
* mnemonic/seed handling lives in a single place.
|
|
55
|
+
*
|
|
56
|
+
* Same inputs + variant always produce the same keypair.
|
|
57
|
+
*
|
|
58
|
+
* Note: 32-byte seedHex (64 hex characters) is not supported — use a mnemonic
|
|
59
|
+
* or a 64-byte seedHex (128 hex characters) instead.
|
|
60
|
+
*
|
|
61
|
+
* @param input { mnemonic } or { seedHex } (64-byte hex, 128 chars)
|
|
62
|
+
* @param variant Default: "mlkem768"
|
|
63
|
+
*/
|
|
64
|
+
static async fromMnemonic(input, context, variant = "mlkem768") {
|
|
65
|
+
const { MnemonicEncryptionAdapter } = await import("./MnemonicEncryptionAdapter-J6IE5OKA-SI6TBLTQ.mjs");
|
|
66
|
+
const ms = new MnemonicEncryptionAdapter(input);
|
|
67
|
+
return ms.intoKeypairEncryptionAdapter(context, variant);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Deterministically derive a ML-KEM keypair from a password and salt.
|
|
71
|
+
*
|
|
72
|
+
* Delegates to PasswordEncryptionAdapter.intoKeypairEncryptionAdapter() so all
|
|
73
|
+
* password handling (scrypt params, entropy validation, 64-byte seed derivation)
|
|
74
|
+
* lives in a single place.
|
|
75
|
+
*
|
|
76
|
+
* Same password + salt + variant always produces the same keypair.
|
|
77
|
+
*
|
|
78
|
+
* @param password User password (validated for entropy)
|
|
79
|
+
* @param salt Domain salt — must not be empty
|
|
80
|
+
* @param variant Default: "mlkem768"
|
|
81
|
+
* @param params Optional custom scrypt params
|
|
82
|
+
*/
|
|
83
|
+
static async fromPassword(password, salt, context, variant = "mlkem768", params) {
|
|
84
|
+
const { PasswordEncryptionAdapter } = await import("./PasswordEncryptionAdapter-CWOXUGR4-ZYUBUYUI.mjs");
|
|
85
|
+
const ps = new PasswordEncryptionAdapter({
|
|
86
|
+
password,
|
|
87
|
+
salt,
|
|
88
|
+
...params ? { params } : {}
|
|
89
|
+
});
|
|
90
|
+
return ps.intoKeypairEncryptionAdapter(context, variant);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Zero the Secret Key bytes and mark this adapter as read-only.
|
|
94
|
+
* Call when the adapter is no longer needed to minimise the window
|
|
95
|
+
* during which raw ML-KEM key material sits in GC-reachable memory.
|
|
96
|
+
* After destroy(), unwrapDek() will throw.
|
|
97
|
+
*/
|
|
98
|
+
destroy() {
|
|
99
|
+
this.secretKey?.fill(0);
|
|
100
|
+
this.secretKey = void 0;
|
|
101
|
+
}
|
|
102
|
+
async wrapDek(dek) {
|
|
103
|
+
return mlkemWrapDek(dek, this.publicKey, this.context, this.variant);
|
|
104
|
+
}
|
|
105
|
+
async unwrapDek(data) {
|
|
106
|
+
if (!this.secretKey) {
|
|
107
|
+
throw new DStorageError(
|
|
108
|
+
EncryptionErrorCode.KEYPAIR_MISSING_SECRET_KEY,
|
|
109
|
+
`[dStorage] KeypairEncryptionAdapter(${this.variant}): cannot unwrapDek \u2014 no Secret Key is available. Use fromKeypair() or fromPassword() to create an adapter with a SK.`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return mlkemUnwrapDek(data, this.secretKey, this.context);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export {
|
|
117
|
+
KeypairEncryptionAdapter
|
|
118
|
+
};
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EncryptionErrorCode
|
|
3
|
+
} from "./chunk-O37NHERH.mjs";
|
|
4
|
+
import {
|
|
5
|
+
DStorageError,
|
|
6
|
+
deriveKek,
|
|
7
|
+
unwrapKey,
|
|
8
|
+
wrapKey
|
|
9
|
+
} from "./chunk-TSZ37JDY.mjs";
|
|
10
|
+
|
|
11
|
+
// ../encryption/dist/chunk-PDWRI7YP.mjs
|
|
12
|
+
import { scrypt } from "@noble/hashes/scrypt.js";
|
|
13
|
+
var KDF_PRESETS = {
|
|
14
|
+
v1: { N: 131072, r: 8, p: 1 },
|
|
15
|
+
"v1-lite": { N: 65536, r: 8, p: 1 }
|
|
16
|
+
};
|
|
17
|
+
var PasswordEncryptionAdapter = class _PasswordEncryptionAdapter {
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.name = "password";
|
|
20
|
+
validatePassword(
|
|
21
|
+
config.password,
|
|
22
|
+
config.minPasswordLength ?? 12,
|
|
23
|
+
config.minEntropyBits ?? 60,
|
|
24
|
+
config.additionalValidators
|
|
25
|
+
);
|
|
26
|
+
this.salt = resolveSalt(config.salt);
|
|
27
|
+
const resolved = resolveParams(config.preset, config.params);
|
|
28
|
+
this.N = resolved.N;
|
|
29
|
+
this.r = resolved.r;
|
|
30
|
+
this.p = resolved.p;
|
|
31
|
+
const passwordBytes = new TextEncoder().encode(config.password);
|
|
32
|
+
const seed = scrypt(passwordBytes, this.salt, {
|
|
33
|
+
N: this.N,
|
|
34
|
+
r: this.r,
|
|
35
|
+
p: this.p,
|
|
36
|
+
dkLen: 64
|
|
37
|
+
});
|
|
38
|
+
this.kekMaterialPromise = globalThis.crypto.subtle.importKey("raw", seed, "HKDF", false, ["deriveBits"]).then(
|
|
39
|
+
(key) => {
|
|
40
|
+
seed.fill(0);
|
|
41
|
+
return key;
|
|
42
|
+
},
|
|
43
|
+
(err) => {
|
|
44
|
+
seed.fill(0);
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Reconstruct an adapter from parameters previously stored via `getKdfParams()`.
|
|
51
|
+
* Use this when loading existing encrypted data — ensures the exact same N/r/p
|
|
52
|
+
* are used regardless of what the current SDK defaults are.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* const adapter = PasswordEncryptionAdapter.fromKdfParams({
|
|
56
|
+
* password,
|
|
57
|
+
* salt: meta.salt,
|
|
58
|
+
* kdf: meta.kdf,
|
|
59
|
+
* N: meta.N,
|
|
60
|
+
* r: meta.r,
|
|
61
|
+
* p: meta.p,
|
|
62
|
+
* });
|
|
63
|
+
*/
|
|
64
|
+
static fromKdfParams(config) {
|
|
65
|
+
if (config.kdf !== "scrypt") {
|
|
66
|
+
throw new DStorageError(
|
|
67
|
+
EncryptionErrorCode.PASSWORD_UNSUPPORTED_KDF,
|
|
68
|
+
`[dStorage] PasswordEncryptionAdapter.fromKdfParams: unsupported kdf "${config.kdf}". Only "scrypt" is supported.`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return new _PasswordEncryptionAdapter({
|
|
72
|
+
password: config.password,
|
|
73
|
+
salt: config.salt,
|
|
74
|
+
params: { N: config.N, r: config.r, p: config.p },
|
|
75
|
+
...config.minPasswordLength !== void 0 && {
|
|
76
|
+
minPasswordLength: config.minPasswordLength
|
|
77
|
+
},
|
|
78
|
+
...config.minEntropyBits !== void 0 && {
|
|
79
|
+
minEntropyBits: config.minEntropyBits
|
|
80
|
+
},
|
|
81
|
+
...config.additionalValidators !== void 0 && {
|
|
82
|
+
additionalValidators: config.additionalValidators
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns the resolved KDF parameters to store alongside encrypted data.
|
|
88
|
+
*
|
|
89
|
+
* These values are language- and SDK-agnostic: any scrypt implementation
|
|
90
|
+
* can re-derive the key from `{ kdf, N, r, p }` + the original salt and
|
|
91
|
+
* password, without any knowledge of this SDK's preset names.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* const meta = {
|
|
95
|
+
* ...adapter.getKdfParams(), // { kdf: "scrypt", N: 131072, r: 8, p: 1 }
|
|
96
|
+
* salt: "myapp:alice@example.com",
|
|
97
|
+
* };
|
|
98
|
+
* await db.save({ encryptedData, meta });
|
|
99
|
+
*/
|
|
100
|
+
getKdfParams() {
|
|
101
|
+
return { kdf: "scrypt", N: this.N, r: this.r, p: this.p };
|
|
102
|
+
}
|
|
103
|
+
async wrapDek(dek) {
|
|
104
|
+
const kekMaterial = await this.kekMaterialPromise;
|
|
105
|
+
const kek = await deriveKek(kekMaterial);
|
|
106
|
+
try {
|
|
107
|
+
return {
|
|
108
|
+
algorithm: "xchacha20poly1305-v1",
|
|
109
|
+
kekDerivation: "hkdf-sha256-v1",
|
|
110
|
+
wrappedDek: await wrapKey(dek, kek)
|
|
111
|
+
};
|
|
112
|
+
} finally {
|
|
113
|
+
kek.fill(0);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async unwrapDek({ wrappedDek }) {
|
|
117
|
+
const kekMaterial = await this.kekMaterialPromise;
|
|
118
|
+
const kek = await deriveKek(kekMaterial);
|
|
119
|
+
try {
|
|
120
|
+
return unwrapKey(wrappedDek, kek);
|
|
121
|
+
} finally {
|
|
122
|
+
kek.fill(0);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Deterministically derive a ML-KEM keypair from this adapter's key material.
|
|
127
|
+
*
|
|
128
|
+
* Uses Web Crypto HKDF to derive a 64-byte ML-KEM seed from the stored
|
|
129
|
+
* CryptoKey, domain-separated from the symmetric KEK derivation path with
|
|
130
|
+
* info="dstorage:mlkem-seed:v1". This is equivalent to noble's
|
|
131
|
+
* hkdf(sha256, scryptOutput, undefined, info, 64) but avoids materialising
|
|
132
|
+
* the raw scrypt output in a JS-accessible variable.
|
|
133
|
+
*
|
|
134
|
+
* Same password + salt + variant always produces the same keypair.
|
|
135
|
+
*
|
|
136
|
+
* @param variant ML-KEM variant. Default: "mlkem768".
|
|
137
|
+
*/
|
|
138
|
+
async intoKeypairEncryptionAdapter(context, variant) {
|
|
139
|
+
const { KeypairEncryptionAdapter } = await import("./KeypairEncryptionAdapter-AE75YC4W-AOWNSH2M.mjs");
|
|
140
|
+
const { mlkemGenerateKeypairFromSeed } = await import("./dist-UD4WRMYJ.mjs");
|
|
141
|
+
const kekMaterial = await this.kekMaterialPromise;
|
|
142
|
+
const mlkemBits = await globalThis.crypto.subtle.deriveBits(
|
|
143
|
+
{
|
|
144
|
+
name: "HKDF",
|
|
145
|
+
hash: "SHA-256",
|
|
146
|
+
salt: new Uint8Array(32),
|
|
147
|
+
info: new TextEncoder().encode("dstorage:mlkem-seed:v1")
|
|
148
|
+
},
|
|
149
|
+
kekMaterial,
|
|
150
|
+
512
|
|
151
|
+
// 64 bytes
|
|
152
|
+
);
|
|
153
|
+
const mlkemSeed = new Uint8Array(mlkemBits);
|
|
154
|
+
try {
|
|
155
|
+
const { pk, sk } = mlkemGenerateKeypairFromSeed(mlkemSeed, variant);
|
|
156
|
+
return KeypairEncryptionAdapter.fromKeypair(pk, sk, context, variant);
|
|
157
|
+
} finally {
|
|
158
|
+
mlkemSeed.fill(0);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
function resolveSalt(salt) {
|
|
163
|
+
const bytes = typeof salt === "string" ? new TextEncoder().encode(salt) : salt;
|
|
164
|
+
if (bytes.length === 0) {
|
|
165
|
+
throw new DStorageError(
|
|
166
|
+
EncryptionErrorCode.PASSWORD_EMPTY_SALT,
|
|
167
|
+
'[dStorage] PasswordEncryptionAdapter: salt must not be empty. Provide an app-specific domain label, e.g. "myapp:v1:alice@example.com".'
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
return bytes;
|
|
171
|
+
}
|
|
172
|
+
function resolveParams(preset, params) {
|
|
173
|
+
if (preset !== void 0) {
|
|
174
|
+
if (!(preset in KDF_PRESETS)) {
|
|
175
|
+
throw new DStorageError(
|
|
176
|
+
EncryptionErrorCode.PASSWORD_UNKNOWN_PRESET,
|
|
177
|
+
`[dStorage] PasswordEncryptionAdapter: unknown preset "${preset}". Valid presets: ${Object.keys(KDF_PRESETS).join(", ")}.`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return KDF_PRESETS[preset];
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
N: params?.N ?? KDF_PRESETS["v1"].N,
|
|
184
|
+
r: params?.r ?? KDF_PRESETS["v1"].r,
|
|
185
|
+
p: params?.p ?? KDF_PRESETS["v1"].p
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function estimateEntropyBits(password) {
|
|
189
|
+
let pool = 0;
|
|
190
|
+
if (/[a-z]/.test(password)) pool += 26;
|
|
191
|
+
if (/[A-Z]/.test(password)) pool += 26;
|
|
192
|
+
if (/[0-9]/.test(password)) pool += 10;
|
|
193
|
+
if (/[^a-zA-Z0-9]/.test(password)) pool += 32;
|
|
194
|
+
return pool === 0 ? 0 : Math.log2(pool) * password.length;
|
|
195
|
+
}
|
|
196
|
+
function hasSequentialRun(pw, runLength = 5) {
|
|
197
|
+
for (let i = 0; i <= pw.length - runLength; i++) {
|
|
198
|
+
let ascending = true;
|
|
199
|
+
let descending = true;
|
|
200
|
+
for (let j = 1; j < runLength; j++) {
|
|
201
|
+
const diff = pw.charCodeAt(i + j) - pw.charCodeAt(i + j - 1);
|
|
202
|
+
if (diff !== 1) ascending = false;
|
|
203
|
+
if (diff !== -1) descending = false;
|
|
204
|
+
if (!ascending && !descending) break;
|
|
205
|
+
}
|
|
206
|
+
if (ascending || descending) return true;
|
|
207
|
+
}
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
var KEYBOARD_ROWS = ["qwertyuiop", "asdfghjkl", "zxcvbnm", "1234567890"];
|
|
211
|
+
function hasKeyboardWalk(pw, minLen = 5) {
|
|
212
|
+
const lower = pw.toLowerCase();
|
|
213
|
+
for (const row of KEYBOARD_ROWS) {
|
|
214
|
+
for (let i = 0; i <= row.length - minLen; i++) {
|
|
215
|
+
const fwd = row.slice(i, i + minLen);
|
|
216
|
+
const rev = fwd.split("").reverse().join("");
|
|
217
|
+
if (lower.includes(fwd) || lower.includes(rev)) return true;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
function generatePqsPassword() {
|
|
223
|
+
while (true) {
|
|
224
|
+
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(32));
|
|
225
|
+
const b64 = btoa(Array.from(bytes, (b) => String.fromCharCode(b)).join(""));
|
|
226
|
+
const password = b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
227
|
+
if (new Set(password).size >= 3 && !hasSequentialRun(password) && !hasKeyboardWalk(password)) {
|
|
228
|
+
return password;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function validatePassword(password, minLength, minEntropyBits, additionalValidators) {
|
|
233
|
+
if (password.length < minLength) {
|
|
234
|
+
throw new DStorageError(
|
|
235
|
+
EncryptionErrorCode.PASSWORD_TOO_SHORT,
|
|
236
|
+
`[dStorage] Password too short: ${password.length} character(s) (minimum ${minLength}). Use a longer passphrase.`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
if (new Set(password).size < 3) {
|
|
240
|
+
throw new DStorageError(
|
|
241
|
+
EncryptionErrorCode.PASSWORD_TOO_FEW_DISTINCT,
|
|
242
|
+
"[dStorage] Password is too weak: too few distinct characters."
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
if (hasSequentialRun(password)) {
|
|
246
|
+
throw new DStorageError(
|
|
247
|
+
EncryptionErrorCode.PASSWORD_SEQUENTIAL_RUN,
|
|
248
|
+
"[dStorage] Password is too weak: contains a sequential character run (e.g. abcde, 12345)."
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
if (hasKeyboardWalk(password)) {
|
|
252
|
+
throw new DStorageError(
|
|
253
|
+
EncryptionErrorCode.PASSWORD_KEYBOARD_WALK,
|
|
254
|
+
"[dStorage] Password is too weak: contains a keyboard-row pattern (e.g. qwerty, asdfg)."
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
const entropy = estimateEntropyBits(password);
|
|
258
|
+
if (entropy < minEntropyBits) {
|
|
259
|
+
throw new DStorageError(
|
|
260
|
+
EncryptionErrorCode.PASSWORD_ENTROPY_TOO_LOW,
|
|
261
|
+
`[dStorage] Password entropy too low: ~${Math.round(entropy)} bits (minimum ${minEntropyBits} bits). Use a longer password with a mix of uppercase, lowercase, digits, and symbols.`
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
if (additionalValidators) {
|
|
265
|
+
for (const validate of additionalValidators) {
|
|
266
|
+
validate(password);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export {
|
|
272
|
+
KDF_PRESETS,
|
|
273
|
+
PasswordEncryptionAdapter,
|
|
274
|
+
generatePqsPassword
|
|
275
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as m}from"./chunk-PKHRAYFD.mjs";import{A as c,B as p,a as o,y as s}from"./chunk-EYF3263P.mjs";var h=class n{constructor(e,t,r,i){this.publicKey=e,this.secretKey=t,this.context=r,this.variant=i,this.name=i}static fromPublicKey(e,t,r="mlkem768"){return new n(e,void 0,t,r)}static fromKeypair(e,t,r,i="mlkem768"){return new n(e,t,r,i)}static generateKeypair(e,t="mlkem768"){let{pk:r,sk:i}=s(t);return{adapter:new n(r,i,e,t),publicKey:r,secretKey:i}}static async fromMnemonic(e,t,r="mlkem768"){let{MnemonicEncryptionAdapter:i}=await import("./MnemonicEncryptionAdapter-RCHUETBP-ELAMJMMO.mjs");return new i(e).intoKeypairEncryptionAdapter(t,r)}static async fromPassword(e,t,r,i="mlkem768",a){let{PasswordEncryptionAdapter:y}=await import("./PasswordEncryptionAdapter-ECHND672-XVBPHPPT.mjs");return new y({password:e,salt:t,...a?{params:a}:{}}).intoKeypairEncryptionAdapter(r,i)}destroy(){this.secretKey?.fill(0),this.secretKey=void 0}async wrapDek(e){return c(e,this.publicKey,this.context,this.variant)}async unwrapDek(e){if(!this.secretKey)throw new o(m.KEYPAIR_MISSING_SECRET_KEY,`[dStorage] KeypairEncryptionAdapter(${this.variant}): cannot unwrapDek \u2014 no Secret Key is available. Use fromKeypair() or fromPassword() to create an adapter with a SK.`);return p(e,this.secretKey,this.context)}};export{h as a};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as n}from"./chunk-PKHRAYFD.mjs";import{a as i,q as p,r as w,s as h}from"./chunk-EYF3263P.mjs";import{scrypt as S}from"@noble/hashes/scrypt.js";var d={v1:{N:131072,r:8,p:1},"v1-lite":{N:65536,r:8,p:1}},N=class m{constructor(t){this.name="password",O(t.password,t.minPasswordLength??12,t.minEntropyBits??60,t.additionalValidators),this.salt=P(t.salt);let r=A(t.preset,t.params);this.N=r.N,this.r=r.r,this.p=r.p;let a=new TextEncoder().encode(t.password),s=S(a,this.salt,{N:this.N,r:this.r,p:this.p,dkLen:64});this.kekMaterialPromise=globalThis.crypto.subtle.importKey("raw",s,"HKDF",!1,["deriveBits"]).then(o=>(s.fill(0),o),o=>{throw s.fill(0),o})}static fromKdfParams(t){if(t.kdf!=="scrypt")throw new i(n.PASSWORD_UNSUPPORTED_KDF,`[dStorage] PasswordEncryptionAdapter.fromKdfParams: unsupported kdf "${t.kdf}". Only "scrypt" is supported.`);return new m({password:t.password,salt:t.salt,params:{N:t.N,r:t.r,p:t.p},...t.minPasswordLength!==void 0&&{minPasswordLength:t.minPasswordLength},...t.minEntropyBits!==void 0&&{minEntropyBits:t.minEntropyBits},...t.additionalValidators!==void 0&&{additionalValidators:t.additionalValidators}})}getKdfParams(){return{kdf:"scrypt",N:this.N,r:this.r,p:this.p}}async wrapDek(t){let r=await this.kekMaterialPromise,a=await p(r);try{return{algorithm:"xchacha20poly1305-v1",kekDerivation:"hkdf-sha256-v1",wrappedDek:await w(t,a)}}finally{a.fill(0)}}async unwrapDek({wrappedDek:t}){let r=await this.kekMaterialPromise,a=await p(r);try{return h(t,a)}finally{a.fill(0)}}async intoKeypairEncryptionAdapter(t,r){let{KeypairEncryptionAdapter:a}=await import("./KeypairEncryptionAdapter-AE75YC4W-BEYO4RUO.mjs"),{mlkemGenerateKeypairFromSeed:s}=await import("./dist-MEZZWKP4.mjs"),o=await this.kekMaterialPromise,l=await globalThis.crypto.subtle.deriveBits({name:"HKDF",hash:"SHA-256",salt:new Uint8Array(32),info:new TextEncoder().encode("dstorage:mlkem-seed:v1")},o,512),c=new Uint8Array(l);try{let{pk:u,sk:k}=s(c,r);return a.fromKeypair(u,k,t,r)}finally{c.fill(0)}}};function P(e){let t=typeof e=="string"?new TextEncoder().encode(e):e;if(t.length===0)throw new i(n.PASSWORD_EMPTY_SALT,'[dStorage] PasswordEncryptionAdapter: salt must not be empty. Provide an app-specific domain label, e.g. "myapp:v1:alice@example.com".');return t}function A(e,t){if(e!==void 0){if(!(e in d))throw new i(n.PASSWORD_UNKNOWN_PRESET,`[dStorage] PasswordEncryptionAdapter: unknown preset "${e}". Valid presets: ${Object.keys(d).join(", ")}.`);return d[e]}return{N:t?.N??d.v1.N,r:t?.r??d.v1.r,p:t?.p??d.v1.p}}function E(e){let t=0;return/[a-z]/.test(e)&&(t+=26),/[A-Z]/.test(e)&&(t+=26),/[0-9]/.test(e)&&(t+=10),/[^a-zA-Z0-9]/.test(e)&&(t+=32),t===0?0:Math.log2(t)*e.length}function y(e,t=5){for(let r=0;r<=e.length-t;r++){let a=!0,s=!0;for(let o=1;o<t;o++){let l=e.charCodeAt(r+o)-e.charCodeAt(r+o-1);if(l!==1&&(a=!1),l!==-1&&(s=!1),!a&&!s)break}if(a||s)return!0}return!1}var v=["qwertyuiop","asdfghjkl","zxcvbnm","1234567890"];function f(e,t=5){let r=e.toLowerCase();for(let a of v)for(let s=0;s<=a.length-t;s++){let o=a.slice(s,s+t),l=o.split("").reverse().join("");if(r.includes(o)||r.includes(l))return!0}return!1}function _(){for(;;){let e=globalThis.crypto.getRandomValues(new Uint8Array(32)),r=btoa(Array.from(e,a=>String.fromCharCode(a)).join("")).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"");if(new Set(r).size>=3&&!y(r)&&!f(r))return r}}function O(e,t,r,a){if(e.length<t)throw new i(n.PASSWORD_TOO_SHORT,`[dStorage] Password too short: ${e.length} character(s) (minimum ${t}). Use a longer passphrase.`);if(new Set(e).size<3)throw new i(n.PASSWORD_TOO_FEW_DISTINCT,"[dStorage] Password is too weak: too few distinct characters.");if(y(e))throw new i(n.PASSWORD_SEQUENTIAL_RUN,"[dStorage] Password is too weak: contains a sequential character run (e.g. abcde, 12345).");if(f(e))throw new i(n.PASSWORD_KEYBOARD_WALK,"[dStorage] Password is too weak: contains a keyboard-row pattern (e.g. qwerty, asdfg).");let s=E(e);if(s<r)throw new i(n.PASSWORD_ENTROPY_TOO_LOW,`[dStorage] Password entropy too low: ~${Math.round(s)} bits (minimum ${r} bits). Use a longer password with a mix of uppercase, lowercase, digits, and symbols.`);if(a)for(let o of a)o(e)}export{d as a,N as b,_ as c};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as C}from"./chunk-ZGUY5EIR.mjs";import*as e from"@midnight-ntwrk/compact-runtime";import{CompiledContract as E}from"@midnight-ntwrk/compact-js";e.checkRuntimeVersion("0.16.0");var o=new e.CompactTypeBytes(32),m=e.CompactTypeOpaqueString,g=new e.CompactTypeUnsignedInteger(18446744073709551615n,8),b=class{alignment(){return m.alignment().concat(m.alignment().concat(m.alignment().concat(m.alignment().concat(g.alignment().concat(m.alignment().concat(o.alignment().concat(o.alignment())))))))}fromValue(t){return{storageId:m.fromValue(t),encryptionScheme:m.fromValue(t),storageProvider:m.fromValue(t),metadata:m.fromValue(t),writtenAt:g.fromValue(t),keyEnvelope:m.fromValue(t),ownerCommitment:o.fromValue(t),contentHash:o.fromValue(t)}}toValue(t){return m.toValue(t.storageId).concat(m.toValue(t.encryptionScheme).concat(m.toValue(t.storageProvider).concat(m.toValue(t.metadata).concat(g.toValue(t.writtenAt).concat(m.toValue(t.keyEnvelope).concat(o.toValue(t.ownerCommitment).concat(o.toValue(t.contentHash))))))))}},y=new b,w=e.CompactTypeBoolean,q=class{alignment(){return o.alignment()}fromValue(t){return{bytes:o.fromValue(t)}}toValue(t){return o.toValue(t.bytes)}},V=new q,h=new e.CompactTypeUnsignedInteger(65535n,2),P=new e.CompactTypeVector(4,o),L=new e.CompactTypeVector(3,o),B=class{alignment(){return w.alignment().concat(o.alignment().concat(o.alignment()))}fromValue(t){return{is_left:w.fromValue(t),left:o.fromValue(t),right:o.fromValue(t)}}toValue(t){return w.toValue(t.is_left).concat(o.toValue(t.left).concat(o.toValue(t.right)))}},M=new B,N=new e.CompactTypeUnsignedInteger(340282366920938463463374607431768211455n,16),u=new e.CompactTypeUnsignedInteger(255n,1),T=class{constructor(...t){if(C(this,"witnesses"),t.length!==1)throw new e.CompactError(`Contract constructor: expected 1 argument, received ${t.length}`);let n=t[0];if(typeof n!="object")throw new e.CompactError("first (witnesses) argument to Contract constructor is not an object");if(typeof n.ownerSecret!="function")throw new e.CompactError("first (witnesses) argument to Contract constructor does not contain a function-valued field named ownerSecret");if(typeof n.newOwnerSecret!="function")throw new e.CompactError("first (witnesses) argument to Contract constructor does not contain a function-valued field named newOwnerSecret");this.witnesses=n,this.circuits={deriveRefId(a,...r){return{result:k.deriveRefId(...r),context:a}},storeReference:(...a)=>{if(a.length!==8)throw new e.CompactError(`storeReference: expected 8 arguments (as invoked from Typescript), received ${a.length}`);let r=a[0],c=a[1],i=a[2],l=a[3],p=a[4],_=a[5],s=a[6],f=a[7];typeof r=="object"&&r.currentQueryContext!=null||e.typeError("storeReference","argument 1 (as invoked from Typescript)","DataRegistry.compact line 184 char 1","CircuitContext",r),typeof _=="bigint"&&_>=0n&&_<=18446744073709551615n||e.typeError("storeReference","argument 5 (argument 6 as invoked from Typescript)","DataRegistry.compact line 184 char 1","Uint<0..18446744073709551616>",_),f.buffer instanceof ArrayBuffer&&f.BYTES_PER_ELEMENT===1&&f.length===32||e.typeError("storeReference","argument 7 (argument 8 as invoked from Typescript)","DataRegistry.compact line 184 char 1","Bytes<32>",f);let d={...r,gasCost:e.emptyRunningCost()},v={input:{value:m.toValue(c).concat(m.toValue(i).concat(m.toValue(l).concat(m.toValue(p).concat(g.toValue(_).concat(m.toValue(s).concat(o.toValue(f))))))),alignment:m.alignment().concat(m.alignment().concat(m.alignment().concat(m.alignment().concat(g.alignment().concat(m.alignment().concat(o.alignment()))))))},output:void 0,publicTranscript:[],privateTranscriptOutputs:[]},R=this._storeReference_0(d,v,c,i,l,p,_,s,f);return v.output={value:o.toValue(R),alignment:o.alignment()},{result:R,context:d,proofData:v,gasCost:d.gasCost}},removeReference:(...a)=>{if(a.length!==2)throw new e.CompactError(`removeReference: expected 2 arguments (as invoked from Typescript), received ${a.length}`);let r=a[0],c=a[1];typeof r=="object"&&r.currentQueryContext!=null||e.typeError("removeReference","argument 1 (as invoked from Typescript)","DataRegistry.compact line 224 char 1","CircuitContext",r),c.buffer instanceof ArrayBuffer&&c.BYTES_PER_ELEMENT===1&&c.length===32||e.typeError("removeReference","argument 1 (argument 2 as invoked from Typescript)","DataRegistry.compact line 224 char 1","Bytes<32>",c);let i={...r,gasCost:e.emptyRunningCost()},l={input:{value:o.toValue(c),alignment:o.alignment()},output:void 0,publicTranscript:[],privateTranscriptOutputs:[]},p=this._removeReference_0(i,l,c);return l.output={value:[],alignment:[]},{result:p,context:i,proofData:l,gasCost:i.gasCost}},updateReference:(...a)=>{if(a.length!==9)throw new e.CompactError(`updateReference: expected 9 arguments (as invoked from Typescript), received ${a.length}`);let r=a[0],c=a[1],i=a[2],l=a[3],p=a[4],_=a[5],s=a[6],f=a[7],d=a[8];typeof r=="object"&&r.currentQueryContext!=null||e.typeError("updateReference","argument 1 (as invoked from Typescript)","DataRegistry.compact line 251 char 1","CircuitContext",r),c.buffer instanceof ArrayBuffer&&c.BYTES_PER_ELEMENT===1&&c.length===32||e.typeError("updateReference","argument 1 (argument 2 as invoked from Typescript)","DataRegistry.compact line 251 char 1","Bytes<32>",c),typeof s=="bigint"&&s>=0n&&s<=18446744073709551615n||e.typeError("updateReference","argument 6 (argument 7 as invoked from Typescript)","DataRegistry.compact line 251 char 1","Uint<0..18446744073709551616>",s),d.buffer instanceof ArrayBuffer&&d.BYTES_PER_ELEMENT===1&&d.length===32||e.typeError("updateReference","argument 8 (argument 9 as invoked from Typescript)","DataRegistry.compact line 251 char 1","Bytes<32>",d);let v={...r,gasCost:e.emptyRunningCost()},R={input:{value:o.toValue(c).concat(m.toValue(i).concat(m.toValue(l).concat(m.toValue(p).concat(m.toValue(_).concat(g.toValue(s).concat(m.toValue(f).concat(o.toValue(d)))))))),alignment:o.alignment().concat(m.alignment().concat(m.alignment().concat(m.alignment().concat(m.alignment().concat(g.alignment().concat(m.alignment().concat(o.alignment())))))))},output:void 0,publicTranscript:[],privateTranscriptOutputs:[]},x=this._updateReference_0(v,R,c,i,l,p,_,s,f,d);return R.output={value:[],alignment:[]},{result:x,context:v,proofData:R,gasCost:v.gasCost}}},this.impureCircuits={storeReference:this.circuits.storeReference,removeReference:this.circuits.removeReference,updateReference:this.circuits.updateReference},this.provableCircuits={storeReference:this.circuits.storeReference,removeReference:this.circuits.removeReference,updateReference:this.circuits.updateReference}}initialState(...t){if(t.length!==1)throw new e.CompactError(`Contract state constructor: expected 1 argument (as invoked from Typescript), received ${t.length}`);let n=t[0];if(typeof n!="object")throw new e.CompactError("Contract state constructor: expected 'constructorContext' in argument 1 (as invoked from Typescript) to be an object");if(!("initialPrivateState"in n))throw new e.CompactError("Contract state constructor: expected 'initialPrivateState' in argument 1 (as invoked from Typescript)");if(!("initialZswapLocalState"in n))throw new e.CompactError("Contract state constructor: expected 'initialZswapLocalState' in argument 1 (as invoked from Typescript)");if(typeof n.initialZswapLocalState!="object")throw new e.CompactError("Contract state constructor: expected 'initialZswapLocalState' in argument 1 (as invoked from Typescript) to be an object");let a=new e.ContractState,r=e.StateValue.newArray();r=r.arrayPush(e.StateValue.newNull()),r=r.arrayPush(e.StateValue.newNull()),r=r.arrayPush(e.StateValue.newNull()),a.data=new e.ChargedState(r),a.setOperation("storeReference",new e.ContractOperation),a.setOperation("removeReference",new e.ContractOperation),a.setOperation("updateReference",new e.ContractOperation);let c=e.createCircuitContext(e.dummyContractAddress(),n.initialZswapLocalState.coinPublicKey,a.data,n.initialPrivateState),i={input:{value:[],alignment:[]},output:void 0,publicTranscript:[],privateTranscriptOutputs:[]};return e.queryLedgerState(c,i,[{push:{storage:!1,value:e.StateValue.newCell({value:u.toValue(0n),alignment:u.alignment()}).encode()}},{push:{storage:!0,value:e.StateValue.newMap(new e.StateMap).encode()}},{ins:{cached:!1,n:1}}]),e.queryLedgerState(c,i,[{push:{storage:!1,value:e.StateValue.newCell({value:u.toValue(1n),alignment:u.alignment()}).encode()}},{push:{storage:!0,value:e.StateValue.newCell({value:g.toValue(0n),alignment:g.alignment()}).encode()}},{ins:{cached:!1,n:1}}]),e.queryLedgerState(c,i,[{push:{storage:!1,value:e.StateValue.newCell({value:u.toValue(2n),alignment:u.alignment()}).encode()}},{push:{storage:!0,value:e.StateValue.newCell({value:h.toValue(0n),alignment:h.alignment()}).encode()}},{ins:{cached:!1,n:1}}]),e.queryLedgerState(c,i,[{idx:{cached:!1,pushPath:!0,path:[{tag:"value",value:{value:u.toValue(1n),alignment:u.alignment()}}]}},{addi:{immediate:parseInt(e.valueToBigInt({value:h.toValue(1n),alignment:h.alignment()}.value))}},{ins:{cached:!0,n:1}}]),e.queryLedgerState(c,i,[{push:{storage:!1,value:e.StateValue.newCell({value:u.toValue(2n),alignment:u.alignment()}).encode()}},{push:{storage:!0,value:e.StateValue.newCell({value:h.toValue(0n),alignment:h.alignment()}).encode()}},{ins:{cached:!1,n:1}}]),a.data=new e.ChargedState(c.currentQueryContext.state.state),{currentContractState:a,currentPrivateState:c.currentPrivateState,currentZswapLocalState:c.currentZswapLocalState}}_persistentHash_0(t){return e.persistentHash(L,t)}_persistentHash_1(t){return e.persistentHash(P,t)}_ownerSecret_0(t,n){let a=e.createWitnessContext(S(t.currentQueryContext.state),t.currentPrivateState,t.currentQueryContext.address),[r,c]=this.witnesses.ownerSecret(a);return t.currentPrivateState=r,c.buffer instanceof ArrayBuffer&&c.BYTES_PER_ELEMENT===1&&c.length===32||e.typeError("ownerSecret","return value","DataRegistry.compact line 26 char 1","Bytes<32>",c),n.privateTranscriptOutputs.push({value:o.toValue(c),alignment:o.alignment()}),c}_newOwnerSecret_0(t,n){let a=e.createWitnessContext(S(t.currentQueryContext.state),t.currentPrivateState,t.currentQueryContext.address),[r,c]=this.witnesses.newOwnerSecret(a);return t.currentPrivateState=r,c.buffer instanceof ArrayBuffer&&c.BYTES_PER_ELEMENT===1&&c.length===32||e.typeError("newOwnerSecret","return value","DataRegistry.compact line 28 char 1","Bytes<32>",c),n.privateTranscriptOutputs.push({value:o.toValue(c),alignment:o.alignment()}),c}_computeOwnerCommitment_0(t,n,a){return this._persistentHash_1([new Uint8Array([100,115,116,111,114,97,103,101,58,111,119,110,101,114,58,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),t,n,a])}_deriveRefId_0(t,n){return this._persistentHash_0([new Uint8Array([100,115,116,111,114,97,103,101,58,114,101,102,58,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),t,n])}_validatedOwnerCommitment_0(t,n,a){let r=this._ownerSecret_0(t,n);return e.assert(!this._equal_0(r,new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])),"ownerSecret must not be zero"),this._computeOwnerCommitment_0(r,a,V.fromValue(e.queryLedgerState(t,n,[{dup:{n:2}},{idx:{cached:!0,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{popeq:{cached:!0,result:void 0}}]).value).bytes)}_validatedNewOwnerCommitment_0(t,n,a){let r=this._newOwnerSecret_0(t,n);return e.assert(!this._equal_1(r,new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])),"newOwnerSecret must not be zero"),this._computeOwnerCommitment_0(r,a,V.fromValue(e.queryLedgerState(t,n,[{dup:{n:2}},{idx:{cached:!0,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{popeq:{cached:!0,result:void 0}}]).value).bytes)}_storeReference_0(t,n,a,r,c,i,l,p,_){let s=this._deriveRefId_0(e.convertFieldToBytes(32,g.fromValue(e.queryLedgerState(t,n,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(1n),alignment:u.alignment()}}]}},{popeq:{cached:!0,result:void 0}}]).value),"DataRegistry.compact line 193 char 29"),V.fromValue(e.queryLedgerState(t,n,[{dup:{n:2}},{idx:{cached:!0,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{popeq:{cached:!0,result:void 0}}]).value).bytes);e.assert(!w.fromValue(e.queryLedgerState(t,n,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{push:{storage:!1,value:e.StateValue.newCell({value:o.toValue(s),alignment:o.alignment()}).encode()}},"member",{popeq:{cached:!0,result:void 0}}]).value),"Reference already stored");let f=this._validatedOwnerCommitment_0(t,n,s),d={storageId:a,encryptionScheme:r,storageProvider:c,metadata:i,writtenAt:l,keyEnvelope:p,ownerCommitment:f,contentHash:_};return e.queryLedgerState(t,n,[{idx:{cached:!1,pushPath:!0,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{push:{storage:!1,value:e.StateValue.newCell({value:o.toValue(s),alignment:o.alignment()}).encode()}},{push:{storage:!0,value:e.StateValue.newCell({value:y.toValue(d),alignment:y.alignment()}).encode()}},{ins:{cached:!1,n:1}},{ins:{cached:!0,n:1}}]),e.queryLedgerState(t,n,[{idx:{cached:!1,pushPath:!0,path:[{tag:"value",value:{value:u.toValue(1n),alignment:u.alignment()}}]}},{addi:{immediate:parseInt(e.valueToBigInt({value:h.toValue(1n),alignment:h.alignment()}.value))}},{ins:{cached:!0,n:1}}]),s}_removeReference_0(t,n,a){e.assert(w.fromValue(e.queryLedgerState(t,n,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{push:{storage:!1,value:e.StateValue.newCell({value:o.toValue(a),alignment:o.alignment()}).encode()}},"member",{popeq:{cached:!0,result:void 0}}]).value),"Reference not found");let r=y.fromValue(e.queryLedgerState(t,n,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:o.toValue(a),alignment:o.alignment()}}]}},{popeq:{cached:!1,result:void 0}}]).value),c=this._validatedOwnerCommitment_0(t,n,a);return e.assert(this._equal_2(r.ownerCommitment,c),"Not the owner"),e.queryLedgerState(t,n,[{idx:{cached:!1,pushPath:!0,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{push:{storage:!1,value:e.StateValue.newCell({value:o.toValue(a),alignment:o.alignment()}).encode()}},{rem:{cached:!1}},{ins:{cached:!0,n:1}}]),[]}_updateReference_0(t,n,a,r,c,i,l,p,_,s){e.assert(w.fromValue(e.queryLedgerState(t,n,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{push:{storage:!1,value:e.StateValue.newCell({value:o.toValue(a),alignment:o.alignment()}).encode()}},"member",{popeq:{cached:!0,result:void 0}}]).value),"Reference not found");let f=y.fromValue(e.queryLedgerState(t,n,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:o.toValue(a),alignment:o.alignment()}}]}},{popeq:{cached:!1,result:void 0}}]).value),d=this._validatedOwnerCommitment_0(t,n,a);e.assert(this._equal_3(f.ownerCommitment,d),"Not the owner");let v=this._validatedNewOwnerCommitment_0(t,n,a),R={storageId:r,encryptionScheme:c,storageProvider:i,metadata:l,writtenAt:p,keyEnvelope:_,ownerCommitment:v,contentHash:s};return e.queryLedgerState(t,n,[{idx:{cached:!1,pushPath:!0,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{push:{storage:!1,value:e.StateValue.newCell({value:o.toValue(a),alignment:o.alignment()}).encode()}},{push:{storage:!0,value:e.StateValue.newCell({value:y.toValue(R),alignment:y.alignment()}).encode()}},{ins:{cached:!1,n:1}},{ins:{cached:!0,n:1}}]),[]}_equal_0(t,n){return!!t.every((a,r)=>n[r]===a)}_equal_1(t,n){return!!t.every((a,r)=>n[r]===a)}_equal_2(t,n){return!!t.every((a,r)=>n[r]===a)}_equal_3(t,n){return!!t.every((a,r)=>n[r]===a)}};function S(t){let n=t instanceof e.StateValue?t:t.state,a=t instanceof e.StateValue?new e.ChargedState(t):t,r={currentQueryContext:new e.QueryContext(a,e.dummyContractAddress()),costModel:e.CostModel.initialCostModel()},c={input:{value:[],alignment:[]},output:void 0,publicTranscript:[],privateTranscriptOutputs:[]};return{references:{isEmpty(...i){if(i.length!==0)throw new e.CompactError(`isEmpty: expected 0 arguments, received ${i.length}`);return w.fromValue(e.queryLedgerState(r,c,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},"size",{push:{storage:!1,value:e.StateValue.newCell({value:g.toValue(0n),alignment:g.alignment()}).encode()}},"eq",{popeq:{cached:!0,result:void 0}}]).value)},size(...i){if(i.length!==0)throw new e.CompactError(`size: expected 0 arguments, received ${i.length}`);return g.fromValue(e.queryLedgerState(r,c,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},"size",{popeq:{cached:!0,result:void 0}}]).value)},member(...i){if(i.length!==1)throw new e.CompactError(`member: expected 1 argument, received ${i.length}`);let l=i[0];return l.buffer instanceof ArrayBuffer&&l.BYTES_PER_ELEMENT===1&&l.length===32||e.typeError("member","argument 1","DataRegistry.compact line 80 char 1","Bytes<32>",l),w.fromValue(e.queryLedgerState(r,c,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{push:{storage:!1,value:e.StateValue.newCell({value:o.toValue(l),alignment:o.alignment()}).encode()}},"member",{popeq:{cached:!0,result:void 0}}]).value)},lookup(...i){if(i.length!==1)throw new e.CompactError(`lookup: expected 1 argument, received ${i.length}`);let l=i[0];return l.buffer instanceof ArrayBuffer&&l.BYTES_PER_ELEMENT===1&&l.length===32||e.typeError("lookup","argument 1","DataRegistry.compact line 80 char 1","Bytes<32>",l),y.fromValue(e.queryLedgerState(r,c,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(0n),alignment:u.alignment()}}]}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:o.toValue(l),alignment:o.alignment()}}]}},{popeq:{cached:!1,result:void 0}}]).value)},[Symbol.iterator](...i){if(i.length!==0)throw new e.CompactError(`iter: expected 0 arguments, received ${i.length}`);let l=n.asArray()[0];return l.asMap().keys().map(p=>{let _=l.asMap().get(p).asCell();return[o.fromValue(p.value),y.fromValue(_.value)]})[Symbol.iterator]()}},get refCounter(){return g.fromValue(e.queryLedgerState(r,c,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(1n),alignment:u.alignment()}}]}},{popeq:{cached:!0,result:void 0}}]).value)},get contractVersion(){return h.fromValue(e.queryLedgerState(r,c,[{dup:{n:0}},{idx:{cached:!1,pushPath:!1,path:[{tag:"value",value:{value:u.toValue(2n),alignment:u.alignment()}}]}},{popeq:{cached:!1,result:void 0}}]).value)}}}var D={currentQueryContext:new e.QueryContext(new e.ContractState().data,e.dummyContractAddress())},O=new T({ownerSecret:(...t)=>{},newOwnerSecret:(...t)=>{}}),k={deriveRefId:(...t)=>{if(t.length!==2)throw new e.CompactError(`deriveRefId: expected 2 arguments (as invoked from Typescript), received ${t.length}`);let n=t[0],a=t[1];return n.buffer instanceof ArrayBuffer&&n.BYTES_PER_ELEMENT===1&&n.length===32||e.typeError("deriveRefId","argument 1","DataRegistry.compact line 149 char 1","Bytes<32>",n),a.buffer instanceof ArrayBuffer&&a.BYTES_PER_ELEMENT===1&&a.length===32||e.typeError("deriveRefId","argument 2","DataRegistry.compact line 149 char 1","Bytes<32>",a),O._deriveRefId_0(n,a)}},A={ownerSecret:t=>{let n=t.privateState;if(!n.ownerSecret)throw new Error("[dStorage/midnight] ownerSecret is not set in private state. The SDK must call privateStateProvider.set() with ownerSecret before proof generation.");return[n,n.ownerSecret]},newOwnerSecret:t=>{let n=t.privateState;if(!n.newOwnerSecret)throw new Error("[dStorage/midnight] newOwnerSecret is not set in private state. The SDK must call privateStateProvider.set() with newOwnerSecret before proof generation.");return[n,n.newOwnerSecret]}},Q=()=>({}),$=E.make("DataRegistry",T).pipe(E.withWitnesses(A));export{T as a,S as b,A as c,Q as d,$ as e};
|