@sorandomains/holder 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -7
- package/dist/index.d.ts +35 -2
- package/dist/index.js +38 -2
- package/dist/native-allowlist.d.ts +21 -0
- package/dist/native-allowlist.js +34 -0
- package/dist/native-approver.d.ts +21 -0
- package/dist/native-approver.js +36 -0
- package/dist/native-auth.d.ts +31 -0
- package/dist/native-auth.js +85 -0
- package/dist/native-codec.d.ts +38 -0
- package/dist/native-codec.js +95 -0
- package/dist/native-holder.d.ts +42 -0
- package/dist/native-holder.js +221 -0
- package/dist/native-transport.d.ts +66 -0
- package/dist/native-transport.js +136 -0
- package/dist/native-types.d.ts +166 -0
- package/dist/native-types.js +98 -0
- package/package.json +2 -2
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { hash, scValToNative, xdr } from "@stellar/stellar-sdk";
|
|
2
|
+
import { destinationFromNative } from "./payment.js";
|
|
3
|
+
import { NativeClaimError, address, amount, bool, bytes32, exactObject, hex, hex32, label, paymentDestinationToScVal, sc, struct, u32, u64, unhex, utf8 } from "./native-codec.js";
|
|
4
|
+
const primitive = (validate, encode) => ({ encode: v => encode(validate(v)), decode: validate });
|
|
5
|
+
const U64 = primitive(v => u64(v, "u64 field"), sc.u64), U32 = primitive(v => u32(v, "u32 field"), sc.u32), AMOUNT = primitive(v => amount(v, "fee amount"), sc.i128), BOOL = primitive(v => bool(v, "boolean field"), sc.bool);
|
|
6
|
+
const ACCOUNT = primitive(v => address(v, "account", "account"), sc.address), CONTRACT = primitive(v => address(v, "contract", "contract"), sc.address), IDENTITY = primitive(v => address(v, "identity", "identity"), sc.address);
|
|
7
|
+
const HEX = { encode: v => sc.bytes(unhex(hex32(v, "bytes32 field"))), decode: v => hex(bytes32(v, "bytes32 field")) };
|
|
8
|
+
const LABEL = { encode: v => sc.bytes(utf8(label(v))), decode: v => { if (!(v instanceof Uint8Array))
|
|
9
|
+
throw new NativeClaimError("label must be on-chain Bytes"); return label(new TextDecoder("utf-8", { fatal: true }).decode(v)); } };
|
|
10
|
+
const DESTINATION = { encode: v => paymentDestinationToScVal(v), decode: destinationFromNative };
|
|
11
|
+
const optional = (inner) => ({ encode: v => v === null ? sc.option(null) : inner.encode(v), decode: v => v === null || v === undefined ? null : inner.decode(v) });
|
|
12
|
+
const enumeration = (mapping) => ({ encode: v => { if (typeof v !== "string" || !Object.hasOwn(mapping, v))
|
|
13
|
+
throw new NativeClaimError("unsupported enum value"); return xdr.ScVal.scvVec([sc.symbol(mapping[v])]); }, decode: v => { if (!Array.isArray(v) || v.length !== 1)
|
|
14
|
+
throw new NativeClaimError("invalid enum result"); const entry = Object.entries(mapping).find(([, tag]) => tag === v[0]); if (!entry)
|
|
15
|
+
throw new NativeClaimError("unknown enum tag"); return entry[0]; } });
|
|
16
|
+
const ADMISSION = { encode: v => { if (!v || typeof v !== "object")
|
|
17
|
+
throw new NativeClaimError("invalid admission"); const a = v; if (a.type === "open") {
|
|
18
|
+
exactObject(a, ["type"], "open admission");
|
|
19
|
+
return xdr.ScVal.scvVec([sc.symbol("Open")]);
|
|
20
|
+
} if (a.type === "allowlist") {
|
|
21
|
+
exactObject(a, ["type", "root"], "allowlist admission");
|
|
22
|
+
return xdr.ScVal.scvVec([sc.symbol("Allowlist"), HEX.encode(a.root)]);
|
|
23
|
+
} if (a.type === "approval") {
|
|
24
|
+
exactObject(a, ["type", "account"], "approval admission");
|
|
25
|
+
return xdr.ScVal.scvVec([sc.symbol("Approval"), ACCOUNT.encode(a.account)]);
|
|
26
|
+
} throw new NativeClaimError("unsupported admission"); }, decode: v => { if (!Array.isArray(v))
|
|
27
|
+
throw new NativeClaimError("invalid admission enum"); if (v.length === 1 && v[0] === "Open")
|
|
28
|
+
return { type: "open" }; if (v.length === 2 && v[0] === "Allowlist")
|
|
29
|
+
return { type: "allowlist", root: HEX.decode(v[1]) }; if (v.length === 2 && v[0] === "Approval")
|
|
30
|
+
return { type: "approval", account: ACCOUNT.decode(v[1]) }; throw new NativeClaimError("unknown admission enum"); } };
|
|
31
|
+
const snake = (s) => s.replace(/[A-Z]/g, c => "_" + c.toLowerCase());
|
|
32
|
+
function model(fields) { return { encode: v => { const value = exactObject(v, Object.keys(fields), "native input"); return struct(Object.fromEntries(Object.entries(fields).map(([name, codec]) => [snake(name), codec.encode(value[name])]))); }, decode: v => { const value = exactObject(v, Object.keys(fields).map(snake), "native result"); return Object.fromEntries(Object.entries(fields).map(([name, codec]) => [name, codec.decode(value[snake(name)])])); } }; }
|
|
33
|
+
const SETTINGS = model({ mode: enumeration({ manual: "Manual", public: "Public" }), enabled: BOOL, admission: ADMISSION, feeToken: CONTRACT, feeAmount: AMOUNT, feeRecipient: ACCOUNT, walletLimit: U64, approvalAllowance: U64, approvalRateLimit: U64, approvalWindowSecs: U64, approvalTtlSecs: U64 });
|
|
34
|
+
const CONFIG = model({ settings: SETTINGS, ownerEpoch: U64, policyVersion: U64, grantEpoch: U64 });
|
|
35
|
+
const CONTEXT = model({ network: HEX, registry: CONTRACT, registrar: CONTRACT, namespace: HEX, requestId: HEX, validAfter: U64, deadline: U64 });
|
|
36
|
+
const CLAIM = model({ context: CONTEXT, label: LABEL, claimant: ACCOUNT, destination: DESTINATION, resolver: CONTRACT, expectedGeneration: optional(U64), expectedExpiry: optional(U64), termSecs: U64, ownerEpoch: U64, policyVersion: U64, grantEpoch: U64, feeToken: CONTRACT, feeAmount: AMOUNT, feeRecipient: ACCOUNT });
|
|
37
|
+
const RESERVED = model({ context: CONTEXT, label: LABEL, holder: ACCOUNT, destination: DESTINATION, resolver: CONTRACT, expectedGeneration: optional(U64), expectedExpiry: optional(U64), termSecs: U64, ownerEpoch: U64, policyVersion: U64 });
|
|
38
|
+
const TRANSFER = model({ context: CONTEXT, label: LABEL, from: IDENTITY, to: ACCOUNT, expectedGeneration: U64, expectedExpiry: U64, proposalExpires: U64, destination: DESTINATION, resolver: CONTRACT });
|
|
39
|
+
const RENEW = model({ context: CONTEXT, label: LABEL, holder: ACCOUNT, expectedGeneration: U64, expectedExpiry: U64, termSecs: U64, minNewExpiry: U64, maxNewExpiry: U64 });
|
|
40
|
+
const USAGE = model({ publicClaims: U64, reservedIssues: U64 }), APPROVAL_USAGE = model({ total: U64, windowUsed: U64, windowEnds: U64 });
|
|
41
|
+
const CONFIG_STATE = { encode: v => v === null ? xdr.ScVal.scvVec([sc.symbol("Unconfigured")]) : xdr.ScVal.scvVec([sc.symbol("Configured"), CONFIG.encode(v)]), decode: v => { if (!Array.isArray(v))
|
|
42
|
+
throw new NativeClaimError("invalid claim config state"); if (v.length === 1 && v[0] === "Unconfigured")
|
|
43
|
+
return null; if (v.length === 2 && v[0] === "Configured")
|
|
44
|
+
return CONFIG.decode(v[1]); throw new NativeClaimError("invalid claim config state"); } };
|
|
45
|
+
const QUOTE = model({ network: HEX, registry: CONTRACT, registrar: CONTRACT, namespace: HEX, resolver: CONTRACT, label: LABEL, node: HEX, claimant: ACCOUNT, config: CONFIG_STATE, ownerEpoch: U64, policyVersion: U64, available: BOOL, reserved: BOOL, generation: optional(U64), expiresAt: optional(U64), termSecs: U64, now: U64, usage: USAGE, approvalUsage: APPROVAL_USAGE });
|
|
46
|
+
const RECEIPT = model({ operation: enumeration({ claim: "Claim", reserved: "Reserved", transfer: "Transfer", renew: "Renew" }), intentHash: HEX, node: HEX, holder: IDENTITY, generation: U64, expiresAt: U64, feeAmount: AMOUNT, feeToken: CONTRACT, feeRecipient: optional(ACCOUNT), ledger: U32, timestamp: U64 });
|
|
47
|
+
const PREVIEW = model({ node: HEX, holder: IDENTITY, generation: U64, expiresAt: U64, active: BOOL, destination: DESTINATION });
|
|
48
|
+
export function claimSettingsToScVal(value) {
|
|
49
|
+
const encoded = SETTINGS.encode(value);
|
|
50
|
+
if (value.admission.type === "approval") {
|
|
51
|
+
if (value.approvalAllowance === 0n || value.approvalRateLimit === 0n || value.approvalWindowSecs === 0n || value.approvalWindowSecs > 31536000n || value.approvalTtlSecs === 0n || value.approvalTtlSecs > 3600n)
|
|
52
|
+
throw new NativeClaimError("approval mode requires explicit bounded admission budgets, window and lifetime");
|
|
53
|
+
if (value.admission.account === value.feeRecipient)
|
|
54
|
+
throw new NativeClaimError("eligibility account must differ from fee treasury");
|
|
55
|
+
}
|
|
56
|
+
else if (value.approvalAllowance !== 0n || value.approvalRateLimit !== 0n || value.approvalWindowSecs !== 0n || value.approvalTtlSecs !== 0n)
|
|
57
|
+
throw new NativeClaimError("approval settings must be zero outside approval mode");
|
|
58
|
+
return encoded;
|
|
59
|
+
}
|
|
60
|
+
export const claimPolicyInputToScVal = claimSettingsToScVal;
|
|
61
|
+
export const claimLabelToScVal = (value) => LABEL.encode(value);
|
|
62
|
+
export const claimConfigFromNative = (value) => CONFIG.decode(value);
|
|
63
|
+
export const claimQuoteFromNative = (value) => QUOTE.decode(value);
|
|
64
|
+
export const claimUsageFromNative = (value) => USAGE.decode(value);
|
|
65
|
+
export const approvalUsageFromNative = (value) => APPROVAL_USAGE.decode(value);
|
|
66
|
+
export const destinationPreviewFromNative = (value) => PREVIEW.decode(value);
|
|
67
|
+
export const claimReceiptFromNative = (value) => RECEIPT.decode(value);
|
|
68
|
+
function validContext(c) { if (c.deadline <= c.validAfter || c.deadline - c.validAfter > 3600n)
|
|
69
|
+
throw new NativeClaimError("business intent lifetime must be positive and at most 3600 seconds"); }
|
|
70
|
+
export function claimIntentToScVal(value) { const result = CLAIM.encode(value); validContext(value.context); if ((value.expectedGeneration === null) !== (value.expectedExpiry === null))
|
|
71
|
+
throw new NativeClaimError("generation and expiry expectations must both be present or absent"); return result; }
|
|
72
|
+
export function reservedIntentToScVal(value) { const result = RESERVED.encode(value); validContext(value.context); if ((value.expectedGeneration === null) !== (value.expectedExpiry === null))
|
|
73
|
+
throw new NativeClaimError("generation and expiry expectations must both be present or absent"); return result; }
|
|
74
|
+
export function transferIntentToScVal(value) { const result = TRANSFER.encode(value); validContext(value.context); return result; }
|
|
75
|
+
export function renewIntentToScVal(value) { const result = RENEW.encode(value); validContext(value.context); if (value.termSecs === 0n || value.maxNewExpiry < value.minNewExpiry)
|
|
76
|
+
throw new NativeClaimError("invalid renewal bounds"); return result; }
|
|
77
|
+
export const claimIntentFromNative = (value) => CLAIM.decode(value);
|
|
78
|
+
export const transferIntentFromNative = (value) => TRANSFER.decode(value);
|
|
79
|
+
export const renewIntentFromNative = (value) => RENEW.decode(value);
|
|
80
|
+
export function claimResultFromNative(value) { if (!Array.isArray(value) || value.length !== 2 || !["Fresh", "Replayed"].includes(value[0]))
|
|
81
|
+
throw new NativeClaimError("invalid claim result"); return { status: value[0] === "Fresh" ? "fresh" : "replayed", receipt: claimReceiptFromNative(value[1]) }; }
|
|
82
|
+
export function nativeIntentHash(method, value) { return hex(hash(xdr.ScVal.scvVec([sc.symbol(method), value]).toXDR())); }
|
|
83
|
+
/** Lossless public recovery JSON; only declared bigint fields receive the tagged encoding. */
|
|
84
|
+
export function stringifyNativeIntent(value) { return JSON.stringify(value, (_key, v) => typeof v === "bigint" ? { $u64: v.toString() } : v); }
|
|
85
|
+
export function parseNativeClaimIntent(json) { if (json.length > 16384)
|
|
86
|
+
throw new NativeClaimError("claim recovery reference is too large"); const raw = JSON.parse(json, (_key, v) => { if (v && typeof v === "object" && !Array.isArray(v) && Object.keys(v).join(",") === "$u64") {
|
|
87
|
+
if (typeof v.$u64 !== "string" || !/^(0|[1-9][0-9]*)$/.test(v.$u64))
|
|
88
|
+
throw new NativeClaimError("invalid recovery integer");
|
|
89
|
+
return BigInt(v.$u64);
|
|
90
|
+
} return v; }); return claimIntentFromNative(scValToNative(claimIntentToScVal(raw))); }
|
|
91
|
+
export function parseNativeTransferIntent(json) { return parseLifecycleIntent(json, TRANSFER, transferIntentToScVal); }
|
|
92
|
+
export function parseNativeRenewIntent(json) { return parseLifecycleIntent(json, RENEW, renewIntentToScVal); }
|
|
93
|
+
function parseLifecycleIntent(json, codec, encode) { if (json.length > 16384)
|
|
94
|
+
throw new NativeClaimError("lifecycle intent is too large"); const raw = JSON.parse(json, (_key, v) => { if (v && typeof v === "object" && !Array.isArray(v) && Object.keys(v).join(",") === "$u64") {
|
|
95
|
+
if (typeof v.$u64 !== "string" || !/^(0|[1-9][0-9]*)$/.test(v.$u64))
|
|
96
|
+
throw new NativeClaimError("invalid recovery integer");
|
|
97
|
+
return BigInt(v.$u64);
|
|
98
|
+
} return v; }); return codec.decode(scValToNative(encode(raw))); }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sorandomains/holder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Manage your own Soran name on Stellar \u2014 records, profile, reverse, primary, and transfers, signed by your key.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"typecheck": "tsc --noEmit",
|
|
20
20
|
"prepublishOnly": "tsc",
|
|
21
21
|
"check:browser": "esbuild dist/index.js --bundle --platform=browser --external:@stellar/stellar-sdk --outfile=browser-check.js --legal-comments=none --log-level=error && node -e \"const fs=require('fs');const s=fs.readFileSync('browser-check.js','utf8');fs.unlinkSync('browser-check.js');if(/\\bBuffer\\b/.test(s)){console.error('FAIL: bare Buffer reference in browser bundle');process.exit(1)}console.log('browser bundle clean')\"",
|
|
22
|
-
"test": "node --import tsx test/decode-failure.test.mts && node --import tsx --test test/
|
|
22
|
+
"test": "node --import tsx test/decode-failure.test.mts && node --import tsx --test test/integration.test.mts test/payment.test.mts test/muxed.test.mts test/native-*.test.mts"
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"repository": {
|