SHA256
Разделить среды и закрепить лицензии в HEADER 1.0
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// CORE v2 / CONTENT v1. Uses the existing ANS-104 signer; no new key store or transport.
|
||||
import { bytesToBase64, base64ToBytes } from './crypto-utils.js';
|
||||
import { createAns104DataItem, parseAns104DataItem, verifyAns104DataItem, serializeAns104Tags } from './ans104-data-item.js';
|
||||
import { SHINE_CORE_NAMESPACE, SHINE_CORE_MAX_BLOCK_NUMBER, SHINE_CORE_LINE_PROFILE,
|
||||
import { SHINE_CORE_NAMESPACE, SHINE_TEST_CORE_NAMESPACE, isArweaveAppTag, SHINE_CORE_MAX_BLOCK_NUMBER, SHINE_CORE_LINE_PROFILE,
|
||||
SHINE_CORE_LINE_CONNECTION, SHINE_CORE_LINE_TECH } from './protocol-tags.js';
|
||||
|
||||
export const CORE_MAX_ITEM_BYTES = 8 * 1024 * 1024;
|
||||
@@ -67,8 +67,9 @@ export function decodeCore(bytes) {
|
||||
encodeCore(result); // identical structural validation on read and write
|
||||
return result;
|
||||
}
|
||||
export function coreTags(core) {
|
||||
const tags = [{ name: 'App', value: SHINE_CORE_NAMESPACE }, { name: 'n', value: String(core.blockNumber) }];
|
||||
export function coreTags(core, arweaveAppTag) {
|
||||
if (!isArweaveAppTag(arweaveAppTag)) throw new Error('Server environment unavailable');
|
||||
const tags = [{ name: 'App', value: arweaveAppTag }, { name: 'n', value: String(core.blockNumber) }];
|
||||
if (core.line) {
|
||||
const code = core.line.lineCode;
|
||||
const special = { [SHINE_CORE_LINE_PROFILE]: 'P', [SHINE_CORE_LINE_CONNECTION]: 'C', [SHINE_CORE_LINE_TECH]: 'T' };
|
||||
@@ -80,7 +81,7 @@ export function coreTags(core) {
|
||||
function checkEventLine(content, core) {
|
||||
const { type, subType, version } = content;
|
||||
const header = type === 0 && subType === 0;
|
||||
if (version !== 1 && !(header && version === 2)) throw new Error('Unsupported CONTENT version for creation');
|
||||
if (version !== 1) throw new Error('Unsupported CONTENT version for creation');
|
||||
if (header !== (core.blockNumber === 0)) throw new Error('HEADER must be CORE #0');
|
||||
const code = core.line?.lineCode;
|
||||
if (type === 0 && !header) { if (code !== SHINE_CORE_LINE_TECH) throw new Error('Wrong TECH line'); }
|
||||
@@ -107,7 +108,8 @@ export function splitEventBody({ msgType, msgSubType, msgVersion = 1, bodyBytes
|
||||
}
|
||||
return { content: { type: msgType, subType: msgSubType, version: msgVersion, body: bodyBytes.slice(prefix) }, line };
|
||||
}
|
||||
export async function signCoreContentPair({ owner32, privateKey, content, prevCoreHash32, blockNumber, timestamp, line = null }) {
|
||||
export async function signCoreContentPair({ owner32, privateKey, content, prevCoreHash32, blockNumber, timestamp, line = null, arweaveAppTag }) {
|
||||
if (!isArweaveAppTag(arweaveAppTag)) throw new Error('Server environment unavailable');
|
||||
bytes32(owner32);
|
||||
checkEventLine(content, { blockNumber, line });
|
||||
const contentBytes = encodeContent(content);
|
||||
@@ -117,18 +119,20 @@ export async function signCoreContentPair({ owner32, privateKey, content, prevCo
|
||||
const contentDataItemId32 = await sha256(parseAns104DataItem(contentDataItem).signature);
|
||||
const core = { prevCoreHash32, blockNumber, timestamp, contentHash32, contentDataItemId32, line };
|
||||
const coreBytes = encodeCore(core);
|
||||
const coreDataItem = await createAns104DataItem({ owner32, privateKey, data: coreBytes, tags: coreTags(core) });
|
||||
const coreDataItem = await createAns104DataItem({ owner32, privateKey, data: coreBytes, tags: coreTags(core, arweaveAppTag) });
|
||||
return { coreBytes, contentBytes, coreDataItem, contentDataItem, coreHash32: await sha256(coreBytes), contentHash32, contentDataItemId32 };
|
||||
}
|
||||
/** Verifies signed pair and owner binding, not history, semantics or authorization of the owner. */
|
||||
export async function verifyCoreContentPair(coreItem, contentItem, expectedOwner32) {
|
||||
export async function verifyCoreContentPair(coreItem, contentItem, expectedOwner32, arweaveAppTag = null) {
|
||||
bytes32(expectedOwner32);
|
||||
for (const item of [coreItem, contentItem]) if (!(item instanceof Uint8Array) || item.length > CORE_MAX_ITEM_BYTES) throw new Error('Invalid ANS-104 size');
|
||||
const [c, p] = await Promise.all([verifyAns104DataItem(coreItem), verifyAns104DataItem(contentItem)]);
|
||||
if (!c.signatureValid || !p.signatureValid || !equal(c.owner32, expectedOwner32) || !equal(p.owner32, expectedOwner32)) throw new Error('Invalid pair signature or owner');
|
||||
const core = decodeCore(c.data), content = decodeContent(p.data);
|
||||
// This is the canonical encoding produced by the UI. General Arweave import validation is D.
|
||||
if (c.target.length || c.anchor.length || p.target.length || p.anchor.length || c.tagsCount !== coreTags(core).length || p.tagsCount !== 0 || !(equal(c.rawTags, serializeAns104Tags(coreTags(core))) || equal(c.rawTags, serializeAns104Tags(coreTags(core).map(t => t.name === 'App' ? { ...t, value: 'test5599' } : t)))) || !equal(p.rawTags, serializeAns104Tags([]))) throw new Error('Invalid pair tags');
|
||||
// Offline proofs may identify either current network; server-bound callers can require one.
|
||||
const allowedApps = arweaveAppTag == null ? [SHINE_CORE_NAMESPACE, SHINE_TEST_CORE_NAMESPACE] : [arweaveAppTag];
|
||||
if (c.target.length || c.anchor.length || p.target.length || p.anchor.length || p.tagsCount !== 0 || !allowedApps.some(app => equal(c.rawTags, serializeAns104Tags(coreTags(core, app)))) || !equal(p.rawTags, serializeAns104Tags([]))) throw new Error('Invalid pair tags');
|
||||
if (!equal(core.contentHash32, p.frameHashBytes) || !equal(core.contentDataItemId32, p.dataItemIdBytes)) throw new Error('CONTENT does not match CORE');
|
||||
return { core, content, coreHash32: c.frameHashBytes, coreDataItemId32: c.dataItemIdBytes };
|
||||
}
|
||||
@@ -155,9 +159,9 @@ export function unpackCoreContent(bytes) {
|
||||
}
|
||||
|
||||
/** Adapter from existing semantic body factories to the wire format. */
|
||||
export async function signEventContainer({ owner32, privateKey, blockNumber, prevBlockHash, timestamp, ...event }) {
|
||||
export async function signEventContainer({ owner32, privateKey, blockNumber, prevBlockHash, timestamp, arweaveAppTag, ...event }) {
|
||||
const { content, line } = splitEventBody(event);
|
||||
const pair = await signCoreContentPair({ owner32, privateKey, blockNumber, prevCoreHash32: coreHashBytes(prevBlockHash), timestamp, content, line });
|
||||
const pair = await signCoreContentPair({ owner32, privateKey, blockNumber, prevCoreHash32: coreHashBytes(prevBlockHash), timestamp, content, line, arweaveAppTag });
|
||||
return { bytes: packCoreContent(pair.coreDataItem, pair.contentDataItem), hash: coreHex(pair.coreHash32) };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user