@oneaddress/partner-sdk 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +139 -2
- package/README.md +250 -17
- package/dist/{chunk-DUVPNFU6.mjs → chunk-LIHXDUUA.mjs} +295 -7
- package/dist/{chunk-UPJU4HFR.mjs → chunk-P55E2BQT.mjs} +376 -29
- package/dist/edge.js +554 -19
- package/dist/edge.mjs +2 -2
- package/dist/index.d.mts +357 -35
- package/dist/index.d.ts +357 -35
- package/dist/index.js +685 -33
- package/dist/index.mjs +6 -2
- package/dist/{src-UACTYQGX.mjs → src-ENJJ6YZU.mjs} +15 -1
- package/package.json +1 -1
|
@@ -3480,6 +3480,286 @@ async function headNamesStagedKey(doc, partnerId, keyId, publicKeyB64) {
|
|
|
3480
3480
|
}
|
|
3481
3481
|
}
|
|
3482
3482
|
|
|
3483
|
+
// ../crypto/src/verify-partner-keys.ts
|
|
3484
|
+
var ACCEPTABLE = /* @__PURE__ */ new Set(["ok", "exempt"]);
|
|
3485
|
+
var EMPTY_BOUND = /* @__PURE__ */ new Set();
|
|
3486
|
+
async function saysRootNotConfigured(res) {
|
|
3487
|
+
if (res.status !== 503) return false;
|
|
3488
|
+
try {
|
|
3489
|
+
const body = await res.json();
|
|
3490
|
+
return typeof body.error === "string" && body.error.includes("root not configured");
|
|
3491
|
+
} catch {
|
|
3492
|
+
return false;
|
|
3493
|
+
}
|
|
3494
|
+
}
|
|
3495
|
+
async function outcomeFor(partnerId, key, reg, bound) {
|
|
3496
|
+
if (!reg.head) return "head-unusable";
|
|
3497
|
+
if (!key.key_signature || !key.root_key_id) {
|
|
3498
|
+
if (!key.countersignature_exempt) return "unsigned";
|
|
3499
|
+
if (headNamesPartner(reg.head, partnerId)) return "exempt-in-head";
|
|
3500
|
+
return bound.has(partnerId) ? "exempt" : "exempt-unbound";
|
|
3501
|
+
}
|
|
3502
|
+
const attested = await verifyPartnerKeyAttestationWithRoots(
|
|
3503
|
+
{
|
|
3504
|
+
partnerId,
|
|
3505
|
+
keyId: key.key_id,
|
|
3506
|
+
algorithm: key.algorithm,
|
|
3507
|
+
publicKeyB64: key.public_key_b64
|
|
3508
|
+
},
|
|
3509
|
+
key.key_signature,
|
|
3510
|
+
key.root_key_id,
|
|
3511
|
+
reg.roots
|
|
3512
|
+
);
|
|
3513
|
+
if (!attested) return "bad-attestation";
|
|
3514
|
+
return headNamesKey(reg.head, partnerId, key.key_id) ? "ok" : "not-in-head";
|
|
3515
|
+
}
|
|
3516
|
+
async function crossoverOutcomeFor(partnerId, crossover, reg) {
|
|
3517
|
+
if (!reg.head) return "head-unusable";
|
|
3518
|
+
return await headNamesStagedKey(reg.head, partnerId, crossover.key_id, crossover.public_key_b64) ? "ok" : "not-authorised";
|
|
3519
|
+
}
|
|
3520
|
+
function createPartnerKeyVerifier(env = {}) {
|
|
3521
|
+
const baseUrl = env.baseUrl ?? "";
|
|
3522
|
+
const report = env.report ?? (() => {
|
|
3523
|
+
});
|
|
3524
|
+
let registryPromise = null;
|
|
3525
|
+
function worthCaching(load) {
|
|
3526
|
+
return load.ok && load.registry.head !== null;
|
|
3527
|
+
}
|
|
3528
|
+
async function loadRegistry() {
|
|
3529
|
+
try {
|
|
3530
|
+
const [rootsRes, headRes] = await Promise.all([
|
|
3531
|
+
fetch(baseUrl + "/.well-known/oneaddress-key-registry.json", { cache: "no-cache" }),
|
|
3532
|
+
fetch(baseUrl + "/.well-known/oneaddress-key-registry-head.json", { cache: "no-cache" })
|
|
3533
|
+
]);
|
|
3534
|
+
if (!rootsRes.ok) {
|
|
3535
|
+
const configured = await saysRootNotConfigured(rootsRes);
|
|
3536
|
+
return {
|
|
3537
|
+
ok: false,
|
|
3538
|
+
failure: {
|
|
3539
|
+
reason: configured ? "roots-not-configured" : "roots-http",
|
|
3540
|
+
status: rootsRes.status
|
|
3541
|
+
}
|
|
3542
|
+
};
|
|
3543
|
+
}
|
|
3544
|
+
let roots;
|
|
3545
|
+
try {
|
|
3546
|
+
const rootsBody = await rootsRes.json();
|
|
3547
|
+
roots = rootsBody.keys ?? [];
|
|
3548
|
+
} catch {
|
|
3549
|
+
return { ok: false, failure: { reason: "roots-unreadable", status: rootsRes.status } };
|
|
3550
|
+
}
|
|
3551
|
+
if (roots.length === 0) {
|
|
3552
|
+
return { ok: false, failure: { reason: "roots-empty", status: rootsRes.status } };
|
|
3553
|
+
}
|
|
3554
|
+
let head = null;
|
|
3555
|
+
let headVerdict = "absent";
|
|
3556
|
+
if (headRes.ok) {
|
|
3557
|
+
head = await headRes.json();
|
|
3558
|
+
headVerdict = await verifyRegistryHead(head, roots);
|
|
3559
|
+
if (headVerdict !== "ok" && headVerdict !== "expired") head = null;
|
|
3560
|
+
}
|
|
3561
|
+
return { ok: true, registry: { roots, head, headVerdict } };
|
|
3562
|
+
} catch {
|
|
3563
|
+
return { ok: false, failure: { reason: "fetch-failed" } };
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
async function verifyActivePartnerKeys(keys, context) {
|
|
3567
|
+
try {
|
|
3568
|
+
const pending = registryPromise ?? (registryPromise = loadRegistry());
|
|
3569
|
+
const load = await pending;
|
|
3570
|
+
if (!worthCaching(load)) registryPromise = null;
|
|
3571
|
+
if (!load.ok) {
|
|
3572
|
+
report(context, { registry: "unavailable", enforced: false, ...load.failure });
|
|
3573
|
+
return { blocked: {}, crossoverBlocked: {} };
|
|
3574
|
+
}
|
|
3575
|
+
const reg = load.registry;
|
|
3576
|
+
let bound = EMPTY_BOUND;
|
|
3577
|
+
if (env.bindSenderHeldKeys) {
|
|
3578
|
+
try {
|
|
3579
|
+
bound = await env.bindSenderHeldKeys(keys);
|
|
3580
|
+
} catch {
|
|
3581
|
+
report(context, { binder: "threw", enforced: true });
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
const tally = {
|
|
3585
|
+
"ok": [],
|
|
3586
|
+
"exempt": [],
|
|
3587
|
+
"exempt-in-head": [],
|
|
3588
|
+
"exempt-unbound": [],
|
|
3589
|
+
"unsigned": [],
|
|
3590
|
+
"bad-attestation": [],
|
|
3591
|
+
"not-in-head": [],
|
|
3592
|
+
"head-unusable": [],
|
|
3593
|
+
"error": []
|
|
3594
|
+
};
|
|
3595
|
+
const blocked = {};
|
|
3596
|
+
const crossoverBlocked = {};
|
|
3597
|
+
const crossoverTally = { "ok": [], "not-authorised": [], "head-unusable": [], "error": [] };
|
|
3598
|
+
for (const [partnerId, key] of Object.entries(keys)) {
|
|
3599
|
+
let outcome;
|
|
3600
|
+
try {
|
|
3601
|
+
outcome = await outcomeFor(partnerId, key, reg, bound);
|
|
3602
|
+
} catch {
|
|
3603
|
+
outcome = "error";
|
|
3604
|
+
}
|
|
3605
|
+
tally[outcome].push(partnerId);
|
|
3606
|
+
if (!ACCEPTABLE.has(outcome)) blocked[partnerId] = outcome;
|
|
3607
|
+
if (!key?.crossover) continue;
|
|
3608
|
+
let xo;
|
|
3609
|
+
try {
|
|
3610
|
+
xo = await crossoverOutcomeFor(partnerId, key.crossover, reg);
|
|
3611
|
+
} catch {
|
|
3612
|
+
xo = "error";
|
|
3613
|
+
}
|
|
3614
|
+
crossoverTally[xo].push(partnerId);
|
|
3615
|
+
if (xo !== "ok") crossoverBlocked[partnerId] = xo;
|
|
3616
|
+
}
|
|
3617
|
+
if (Object.keys(crossoverBlocked).length > 0) {
|
|
3618
|
+
const failedXo = Object.keys(crossoverTally).filter((o) => o !== "ok" && crossoverTally[o].length > 0);
|
|
3619
|
+
report(context, {
|
|
3620
|
+
headVerdict: reg.headVerdict,
|
|
3621
|
+
enforced: true,
|
|
3622
|
+
surface: "crossover",
|
|
3623
|
+
...Object.fromEntries(failedXo.map((o) => [`crossover-${o}`, crossoverTally[o]]))
|
|
3624
|
+
});
|
|
3625
|
+
}
|
|
3626
|
+
if (Object.keys(blocked).length > 0) {
|
|
3627
|
+
const failed = Object.keys(tally).filter((o) => !ACCEPTABLE.has(o) && tally[o].length > 0);
|
|
3628
|
+
report(context, {
|
|
3629
|
+
headVerdict: reg.headVerdict,
|
|
3630
|
+
enforced: true,
|
|
3631
|
+
total: Object.keys(keys).length,
|
|
3632
|
+
ok: tally.ok.length,
|
|
3633
|
+
// Counted, not listed: an operator reading a real finding needs to know
|
|
3634
|
+
// how many keys were skipped as exempt, without the ids adding noise.
|
|
3635
|
+
exempt: tally.exempt.length,
|
|
3636
|
+
...Object.fromEntries(failed.map((o) => [o, tally[o]]))
|
|
3637
|
+
});
|
|
3638
|
+
}
|
|
3639
|
+
return { blocked, crossoverBlocked };
|
|
3640
|
+
} catch {
|
|
3641
|
+
report(context, { registry: "verifier-error", enforced: false });
|
|
3642
|
+
return { blocked: {}, crossoverBlocked: {} };
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
function __resetRegistryCacheForTests() {
|
|
3646
|
+
registryPromise = null;
|
|
3647
|
+
}
|
|
3648
|
+
return { verifyActivePartnerKeys, __resetRegistryCacheForTests };
|
|
3649
|
+
}
|
|
3650
|
+
var PARTNER_KEY_REFUSED_MESSAGE = "Some services are temporarily unavailable and were not updated. Please try again in a moment.";
|
|
3651
|
+
|
|
3652
|
+
// ../crypto/src/sender-held-keys.ts
|
|
3653
|
+
var SENDER_HELD_KEY_ALGORITHM = "ECDH-P256-AES-GCM-256";
|
|
3654
|
+
function memorySenderHeldKeyStore() {
|
|
3655
|
+
const keys = /* @__PURE__ */ new Map();
|
|
3656
|
+
return {
|
|
3657
|
+
all: async () => [...keys.values()],
|
|
3658
|
+
put: async (key) => {
|
|
3659
|
+
keys.set(key.keyId, key);
|
|
3660
|
+
}
|
|
3661
|
+
};
|
|
3662
|
+
}
|
|
3663
|
+
function toB64(bytes) {
|
|
3664
|
+
let s = "";
|
|
3665
|
+
for (const b of bytes) s += String.fromCharCode(b);
|
|
3666
|
+
return btoa(s);
|
|
3667
|
+
}
|
|
3668
|
+
async function keyIdForPublicKey(spki) {
|
|
3669
|
+
const d = new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", spki)).slice(0, 16);
|
|
3670
|
+
d[6] = d[6] & 15 | 128;
|
|
3671
|
+
d[8] = d[8] & 63 | 128;
|
|
3672
|
+
const h = [...d].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
3673
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
3674
|
+
}
|
|
3675
|
+
function createSenderHeldKeyBinder(storeFor) {
|
|
3676
|
+
let pending = null;
|
|
3677
|
+
async function mint() {
|
|
3678
|
+
if (typeof globalThis.crypto === "undefined" || !globalThis.crypto.subtle) return null;
|
|
3679
|
+
let store = storeFor();
|
|
3680
|
+
try {
|
|
3681
|
+
const existing = await store.all();
|
|
3682
|
+
if (existing.length > 0) {
|
|
3683
|
+
return [...existing].sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0];
|
|
3684
|
+
}
|
|
3685
|
+
} catch {
|
|
3686
|
+
store = memorySenderHeldKeyStore();
|
|
3687
|
+
}
|
|
3688
|
+
const pair = await globalThis.crypto.subtle.generateKey(
|
|
3689
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3690
|
+
false,
|
|
3691
|
+
["deriveBits"]
|
|
3692
|
+
);
|
|
3693
|
+
const spki = new Uint8Array(await globalThis.crypto.subtle.exportKey("spki", pair.publicKey));
|
|
3694
|
+
const key = {
|
|
3695
|
+
keyId: await keyIdForPublicKey(spki),
|
|
3696
|
+
publicKeyB64: toB64(spki),
|
|
3697
|
+
privateKey: pair.privateKey,
|
|
3698
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
3699
|
+
};
|
|
3700
|
+
try {
|
|
3701
|
+
await store.put(key);
|
|
3702
|
+
} catch {
|
|
3703
|
+
}
|
|
3704
|
+
return key;
|
|
3705
|
+
}
|
|
3706
|
+
function getOrCreate() {
|
|
3707
|
+
pending ??= mint().catch(() => {
|
|
3708
|
+
pending = null;
|
|
3709
|
+
return null;
|
|
3710
|
+
});
|
|
3711
|
+
return pending;
|
|
3712
|
+
}
|
|
3713
|
+
const bind = async (keys) => {
|
|
3714
|
+
const bound = /* @__PURE__ */ new Set();
|
|
3715
|
+
const flagged = Object.entries(keys).filter(([, k]) => k?.countersignature_exempt === true);
|
|
3716
|
+
if (flagged.length === 0) return bound;
|
|
3717
|
+
const own = await getOrCreate();
|
|
3718
|
+
if (!own) return bound;
|
|
3719
|
+
for (const [partnerId, k] of flagged) {
|
|
3720
|
+
k.key_id = own.keyId;
|
|
3721
|
+
k.public_key_b64 = own.publicKeyB64;
|
|
3722
|
+
k.algorithm = SENDER_HELD_KEY_ALGORITHM;
|
|
3723
|
+
k.key_signature = null;
|
|
3724
|
+
k.root_key_id = null;
|
|
3725
|
+
delete k.crossover;
|
|
3726
|
+
bound.add(partnerId);
|
|
3727
|
+
}
|
|
3728
|
+
return bound;
|
|
3729
|
+
};
|
|
3730
|
+
return Object.assign(bind, { getOrCreate });
|
|
3731
|
+
}
|
|
3732
|
+
async function openSealedWithSenderHeldKey(share, envelope, partnerId, store) {
|
|
3733
|
+
let keys;
|
|
3734
|
+
try {
|
|
3735
|
+
keys = await store.all();
|
|
3736
|
+
} catch {
|
|
3737
|
+
return { ok: false, reason: "no-key" };
|
|
3738
|
+
}
|
|
3739
|
+
const key = keys.find((k) => k.keyId === share.key_id);
|
|
3740
|
+
if (!key) return { ok: false, reason: "no-key" };
|
|
3741
|
+
try {
|
|
3742
|
+
const data = await decryptSession(share, envelope, {
|
|
3743
|
+
name: "this device",
|
|
3744
|
+
async deriveSharedSecret(epkSpki) {
|
|
3745
|
+
const epk = await globalThis.crypto.subtle.importKey(
|
|
3746
|
+
"spki",
|
|
3747
|
+
epkSpki,
|
|
3748
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3749
|
+
false,
|
|
3750
|
+
[]
|
|
3751
|
+
);
|
|
3752
|
+
return new Uint8Array(
|
|
3753
|
+
await globalThis.crypto.subtle.deriveBits({ name: "ECDH", public: epk }, key.privateKey, 256)
|
|
3754
|
+
);
|
|
3755
|
+
}
|
|
3756
|
+
}, partnerId);
|
|
3757
|
+
return { ok: true, data };
|
|
3758
|
+
} catch {
|
|
3759
|
+
return { ok: false, reason: "unreadable" };
|
|
3760
|
+
}
|
|
3761
|
+
}
|
|
3762
|
+
|
|
3483
3763
|
// ../crypto/src/index.ts
|
|
3484
3764
|
function b64ToBytes3(b64) {
|
|
3485
3765
|
const binary = atob(b64);
|
|
@@ -3902,12 +4182,7 @@ async function decryptSession(share, envelopeB64, privateKeyB64, partnerId, expe
|
|
|
3902
4182
|
}
|
|
3903
4183
|
const ops = typeof privateKeyB64 === "string" ? null : privateKeyB64;
|
|
3904
4184
|
if (ops) {
|
|
3905
|
-
|
|
3906
|
-
throw new Error(
|
|
3907
|
-
`decryptSession: key source "${ops.name}" performs private-key operations without releasing the key, which no KMS offers for ML-KEM. Use an exportable key source for post-quantum shares.`
|
|
3908
|
-
);
|
|
3909
|
-
}
|
|
3910
|
-
const needed = share.alg === "RSA-OAEP-256+A256GCM" ? "decryptRsaOaep" : "deriveSharedSecret";
|
|
4185
|
+
const needed = share.alg === "RSA-OAEP-256+A256GCM" ? "decryptRsaOaep" : share.alg === "MLKEM768X25519+HKDF-SHA256+A256GCM" ? "decapsulateXWing" : "deriveSharedSecret";
|
|
3911
4186
|
if (typeof ops[needed] !== "function") {
|
|
3912
4187
|
throw new Error(
|
|
3913
4188
|
`decryptSession: key source "${ops.name}" cannot do ${needed}, which ${share.alg} requires.`
|
|
@@ -3919,7 +4194,13 @@ async function decryptSession(share, envelopeB64, privateKeyB64, partnerId, expe
|
|
|
3919
4194
|
if (!share.epk) {
|
|
3920
4195
|
throw new Error("decryptSession: hybrid share missing epk (KEM ciphertext)");
|
|
3921
4196
|
}
|
|
3922
|
-
const
|
|
4197
|
+
const kemCiphertext = b64UrlToBytes(share.epk);
|
|
4198
|
+
const sharedSecret = ops ? toArrayBuffer(await ops.decapsulateXWing(kemCiphertext)) : toArrayBuffer(XWing.decapsulate(kemCiphertext, b64ToBytes3(privateKeyB64)));
|
|
4199
|
+
if (ops && sharedSecret.byteLength !== 32) {
|
|
4200
|
+
throw new Error(
|
|
4201
|
+
`decryptSession: key source "${ops.name}" returned a ${sharedSecret.byteLength}-byte shared secret; X-Wing decapsulation is 32 bytes with no KDF applied.`
|
|
4202
|
+
);
|
|
4203
|
+
}
|
|
3923
4204
|
const wrappedBytes = b64UrlToBytes(share.wrapped_key);
|
|
3924
4205
|
if (wrappedBytes.length < WRAP_SALT_LENGTH + WRAP_IV_LENGTH + 16) {
|
|
3925
4206
|
throw new Error("decryptSession: wrapped_key too short");
|
|
@@ -4152,6 +4433,13 @@ export {
|
|
|
4152
4433
|
headNamesPartner,
|
|
4153
4434
|
partnerKeyFingerprint,
|
|
4154
4435
|
headNamesStagedKey,
|
|
4436
|
+
createPartnerKeyVerifier,
|
|
4437
|
+
PARTNER_KEY_REFUSED_MESSAGE,
|
|
4438
|
+
SENDER_HELD_KEY_ALGORITHM,
|
|
4439
|
+
memorySenderHeldKeyStore,
|
|
4440
|
+
keyIdForPublicKey,
|
|
4441
|
+
createSenderHeldKeyBinder,
|
|
4442
|
+
openSealedWithSenderHeldKey,
|
|
4155
4443
|
normaliseAddress,
|
|
4156
4444
|
generatePartnerKeyPair,
|
|
4157
4445
|
generateHybridPartnerKeyPair,
|