PageSourceSearch

https://1keep.com/_app/immutable/chunks/MagicNumberProof2022.7e76fe4d.js

js 1keep.com collected 2026-09-28 06:38:18 UTC 6,398 bytes, 175 lines download raw bytes

1import { w as wallet } from './wallet.f9c29244.js';
2import { Z as get_store_value } from './index.ce768326.js';
3import { c as buffer, y as jsonld } from './pbkdf2.2a33b8e7.js';
4import { s as sha256 } from './sha256.4d501cbe.js';
5
6const getMagicProofVal = (verifiableData) => {
7  const proof = getMagicProof(verifiableData);
8  return proof?.proofValue;
9};
10const getMagicProof = (verifiableData) => {
11  if (verifiableData === null || typeof verifiableData === "undefined") {
12    return null;
13  }
14  const proof = Array.isArray(verifiableData.proof) ? verifiableData.proof : [verifiableData.proof];
15  const index = proof.findIndex((p) => p.type === "MagicNumberProof2022");
16  if (index >= 0) {
17    return proof[index];
18  }
19  return null;
20};
21const followMagic = (vc) => {
22  if (vc === null || typeof vc === "undefined") {
23    return null;
24  }
25  const proof = getMagicProofVal(vc);
26  if (proof) {
27    let next = get_store_value(wallet).proofs.find((p) => p.id === proof);
28    if (next) {
29      return followMagic(next);
30    }
31    return vc;
32  }
33  return null;
34};
35
36const sha256buffer = (val) => {
37    return buffer.Buffer.from(sha256.hash(buffer.Buffer.from(val)));
38};
39
40class LinkedDataProof {
41    constructor(type, proofPurpose, verificationMethod, challenge, domain, created) {
42        this.type = type;
43        this.proofPurpose = proofPurpose;
44        if (!created) {
45            let date = new Date().toISOString();
46            this.created = date.slice(0, date.length - 5) + 'Z';
47        }
48        else {
49            this.created = created;
50        }
51        if (typeof challenge !== 'undefined' && challenge !== null) {
52            this.challenge = challenge;
53        }
54        if (typeof domain !== 'undefined' && domain !== null) {
55            this.domain = domain;
56        }
57        if (typeof verificationMethod !== 'undefined' && verificationMethod !== null) {
58            this.verificationMethod = verificationMethod;
59        }
60    }
61    validate(maxTimestampDelta) {
62        if (maxTimestampDelta && maxTimestampDelta !== Infinity) {
63            const expected = new Date().getTime();
64            const delta = maxTimestampDelta * 1000;
65            const created = new Date(this.created).getTime();
66            // comparing this way handles NaN case where `created` is invalid
67            if (!(created >= expected - delta && created <= expected + delta)) {
68                console.error("The proof's created timestamp is out of range.");
69                return false;
70            }
71        }
72        return true;
73    }
74    toJSON() {
75        return JSON.parse(JSON.stringify(this));
76    }
77}
78
79class MagicNumberProof2022 extends LinkedDataProof {
80    constructor(proofPurpose, target, difficulty, challenge) {
81        super('MagicNumberProof2022', proofPurpose, null, challenge);
82        this.target = target;
83        this.difficulty = difficulty;
84    }
85    toJSON() {
86        let resp = {
87            type: 'MagicNumberProof2022',
88            proofPurpose: this.proofPurpose,
89            target: this.target,
90            difficulty: this.difficulty,
91            proofValue: this.proofValue,
92            nonce: this.nonce,
93            created: this.created
94        };
95        if (this.challenge) {
96            resp['challenge'] = this.challenge;
97        }
98        return resp;
99    }
100}
101class MagicNumberProof2022Suite {
102    constructor(options) {
103        this.type = 'MagicNumberProof2022';
104        this.context = 'https://aviary.directory/magic';
105        this.mineMagicNumber = async (inputData, target, difficulty) => {
106            return await this.miner.mine(inputData, target, difficulty);
107        };
108        this.createProof = async (document, proofPurpose, documentLoader, proofOptions) => {
109            const { target, difficulty, challenge } = proofOptions;
110            if (target.length < difficulty) {
111                throw new Error(`mining target (${target}) too short for difficulty ${difficulty}`);
112            }
113            const { proof, ...proofless } = document;
114            let magicProof = new MagicNumberProof2022(proofPurpose, target, difficulty, challenge);
115            const verifyData = await this.createVerifyData({
116                document: proofless,
117                proof: magicProof,
118                documentLoader
119            });
120            const { proofValue, nonce } = await this.mineMagicNumber(verifyData, target, difficulty);
121            magicProof.proofValue = proofValue;
122            magicProof.nonce = nonce;
123            return magicProof.toJSON();
124        };
125        this.verifyProof = async (document, documentLoader) => {
126            let errors = [];
127            const { proof, ...doc } = document;
128            const verifyData = await this.createVerifyData({
129                document: doc,
130                proof,
131                documentLoader
132            });
133            if (sha256buffer(verifyData + proof.nonce).toString('hex') !== proof.proofValue) {
134                errors.push(`magic number proof: ${proof.proofValue} failed verification`);
135            }
136            if (errors.length > 0) {
137                return { verified: false, errors };
138            }
139            return { verified: true, errors };
140        };
141        if (options) {
142            this.date = options.date;
143            if (options.miner) {
144                this.miner = options.miner;
145            }
146        }
147    }
148    async canonize(input, { documentLoader }) {
149        return await jsonld.canonize(input, {
150            algorithm: 'URDNA2015',
151            format: 'application/n-quads',
152            documentLoader,
153            useNative: false
154        });
155    }
156    async canonizeProof(proof, { documentLoader }) {
157        const { proofValue, nonce, ...rest } = proof;
158        return await this.canonize(rest, {
159            documentLoader,
160            skipExpansion: false
161        });
162    }
163    async createVerifyData({ document, proof, documentLoader }) {
164        // concatenate hash of c14n proof options and hash of c14n document
165        const c14nProofOptions = await this.canonizeProof({ '@context': document['@context'], ...proof }, {
166            documentLoader
167        });
168        const c14nDocument = await this.canonize(document, {
169            documentLoader
170        });
171        return buffer.Buffer.concat([sha256buffer(c14nDocument), sha256buffer(c14nProofOptions)]);
172    }
173}
174
175export { MagicNumberProof2022Suite as M, followMagic as f, getMagicProofVal as g };

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.