@givelotus/compiler 0.1.0 → 0.2.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/dist/packages.js +24 -7
- package/dist/std.js +57 -0
- package/dist/token-cli.d.ts +2 -0
- package/dist/token-cli.js +82 -0
- package/dist/token-metadata.d.ts +43 -0
- package/dist/token-metadata.js +166 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -3
- package/std/tokens/lrc20.lotus +26 -0
- package/std/tokens/stateful-token-merge.lotus +132 -0
- package/std/tokens/upgradable-token-rules.lotus +70 -0
- package/std-pkg/lotus.toml +9 -0
- package/std-pkg/src/lrc20.lotus +38 -0
- package/std-pkg/src/stateful_token.lotus +131 -0
- package/std-pkg/src/upgradable.lotus +68 -0
package/dist/packages.js
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
* clock, VM call (`OP_DEFINE` / `OP_INVOKE`) or environment input.
|
|
13
13
|
*/
|
|
14
14
|
import { lstatSync, readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs';
|
|
15
|
-
import { join, relative, sep } from 'node:path';
|
|
15
|
+
import { dirname, join, relative, sep } from 'node:path';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
16
17
|
import { bytes, concat, cs, hex, tagged } from './compiler.js';
|
|
17
18
|
import { CompileError, SourceManager, diagnostic } from './diagnostics.js';
|
|
18
19
|
import { DEFAULT_COMPILER_EDITION, DEFAULT_OPTIMIZER_VERSION, SUPPORTED_COMPILER_EDITIONS, SUPPORTED_OPTIMIZER_VERSIONS, compileContract, compileLinkedProgramAsync, linkedProgramSource, sourceBindings, isSupportedCompilerEdition, isSupportedOptimizerVersion, parseModuleSource, resolveCompilerEdition, resolveOptimizerVersion, } from './generic.js';
|
|
@@ -405,6 +406,8 @@ function finishDistributed(project, root, limits, budget, online) {
|
|
|
405
406
|
const resolved = resolveDistributedSources(project, root, limits, budget, online);
|
|
406
407
|
return { project, root, packages: resolved.packages, lockText: resolved.lockText, modules: resolved.modules };
|
|
407
408
|
}
|
|
409
|
+
const BUILTIN_STD_DIR = join(dirname(fileURLToPath(import.meta.url)), '..', 'std-pkg');
|
|
410
|
+
const BUILTIN_STD_LOCATOR = 'builtin:std';
|
|
408
411
|
export function resolveWorkspace(projectDir, limits, online) {
|
|
409
412
|
let project;
|
|
410
413
|
try {
|
|
@@ -434,12 +437,26 @@ export function resolveWorkspace(projectDir, limits, online) {
|
|
|
434
437
|
continue;
|
|
435
438
|
}
|
|
436
439
|
// Locators come only from the root manifest; dependency [sources] are never inherited.
|
|
440
|
+
// Built-in `std` resolves directly from the bundled compiler `std-pkg/` when no override is given.
|
|
437
441
|
const locator = root.manifest.sources.get(name);
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
442
|
+
let info;
|
|
443
|
+
if (locator === undefined) {
|
|
444
|
+
if (name === 'std') {
|
|
445
|
+
if (packages.size >= limits.maxPackages)
|
|
446
|
+
throw new PackageError('limit', `package graph exceeds ${limits.maxPackages} packages`, from.manifestFile);
|
|
447
|
+
const stdParent = realpathSync(dirname(BUILTIN_STD_DIR));
|
|
448
|
+
const scanned = scanPackage(stdParent, 'std-pkg', false, budget);
|
|
449
|
+
info = { ...scanned, root: BUILTIN_STD_LOCATOR };
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
throw new PackageError('manifest', `no [sources] locator for ${name} in the root manifest`, root.manifestFile);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
if (packages.size >= limits.maxPackages)
|
|
457
|
+
throw new PackageError('limit', `package graph exceeds ${limits.maxPackages} packages`, from.manifestFile);
|
|
458
|
+
info = scanPackage(project, locator, false, budget);
|
|
459
|
+
}
|
|
443
460
|
if (info.id !== id)
|
|
444
461
|
throw new PackageError('version-conflict', `locator ${locator} contains ${info.id}, expected ${id}`, info.manifestFile);
|
|
445
462
|
if (info.manifest.kind !== 'library')
|
|
@@ -480,7 +497,7 @@ function renderLock(root, packages) {
|
|
|
480
497
|
dependencies: sorted(root.manifest.dependencies),
|
|
481
498
|
},
|
|
482
499
|
packages: [...packages.values()].map(pkg => ({ name: pkg.manifest.name, version: pkg.manifest.version,
|
|
483
|
-
locator: { kind: 'path', path: root.manifest.sources.get(pkg.manifest.name) }, root: pkg.root,
|
|
500
|
+
locator: { kind: 'path', path: root.manifest.sources.get(pkg.manifest.name) ?? BUILTIN_STD_LOCATOR }, root: pkg.root,
|
|
484
501
|
dependencies: sorted(pkg.manifest.dependencies), files: pkg.files.map(f => ({ path: f.path, digest: hex(f.digest) })), treeDigest: hex(pkg.treeDigest) })).sort(byName),
|
|
485
502
|
};
|
|
486
503
|
return JSON.stringify(lock, null, 2) + '\n';
|
package/dist/std.js
CHANGED
|
@@ -67,6 +67,63 @@ export const STANDARD_CONTRACTS = Object.freeze([
|
|
|
67
67
|
walletBadge: 'Issuer-Authorized Voucher Token',
|
|
68
68
|
actions: { mint_with_voucher: 'Mint tokens using an issuer-signed outpoint-bound voucher' },
|
|
69
69
|
},
|
|
70
|
+
{
|
|
71
|
+
id: 'std/tokens/lrc20',
|
|
72
|
+
contract: 'MetadataFungibleToken',
|
|
73
|
+
kind: 'token',
|
|
74
|
+
category: 'tokens',
|
|
75
|
+
file: 'tokens/lrc20.lotus',
|
|
76
|
+
summary: 'Standard LRC-20 fungible token with immutable BLAKE3 metadata commitment.',
|
|
77
|
+
walletBadge: 'LRC-20 Fungible Token (Verified Metadata)',
|
|
78
|
+
actions: { issue: 'Mint LRC-20 tokens committed to canonical metadata hash' },
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 'std/tokens/stateful-token-rules',
|
|
82
|
+
contract: 'StatefulTokenRules',
|
|
83
|
+
kind: 'contract',
|
|
84
|
+
category: 'tokens',
|
|
85
|
+
file: 'tokens/stateful-token-merge.lotus',
|
|
86
|
+
summary: 'Stateful token rules supporting transfer, 1-to-2 split, and 2-to-1 UTXO merge.',
|
|
87
|
+
walletBadge: 'Stateful Fungible Token (Split & Merge)',
|
|
88
|
+
actions: {
|
|
89
|
+
transfer: 'Transfer stateful token preserving state commitment',
|
|
90
|
+
split: 'Split stateful token conserving value and additive weight',
|
|
91
|
+
merge: 'Merge two stateful token UTXOs summing weight and preserving max lock',
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
id: 'std/tokens/stateful-token',
|
|
96
|
+
contract: 'StatefulToken',
|
|
97
|
+
kind: 'token',
|
|
98
|
+
category: 'tokens',
|
|
99
|
+
file: 'tokens/stateful-token-merge.lotus',
|
|
100
|
+
summary: 'Mint tree for StatefulTokenRules initializing per-UTXO TokenState.',
|
|
101
|
+
walletBadge: 'Stateful Fungible Token Mint',
|
|
102
|
+
actions: { issue: 'Mint stateful tokens with initial weight and lock height' },
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
id: 'std/tokens/upgradable-rules',
|
|
106
|
+
contract: 'UpgradableRules',
|
|
107
|
+
kind: 'contract',
|
|
108
|
+
category: 'tokens',
|
|
109
|
+
file: 'tokens/upgradable-token-rules.lotus',
|
|
110
|
+
summary: 'In-place upgradable token rules migrated asynchronously via governance checkdatasig.',
|
|
111
|
+
walletBadge: 'Upgradable Token Rules (Governance Migration)',
|
|
112
|
+
actions: {
|
|
113
|
+
transfer: 'Transfer token preserving current rules contract',
|
|
114
|
+
upgrade_contract: 'Migrate token UTXOs in-place to governance-signed new rules root',
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
id: 'std/tokens/upgradable-token',
|
|
119
|
+
contract: 'UpgradableToken',
|
|
120
|
+
kind: 'token',
|
|
121
|
+
category: 'tokens',
|
|
122
|
+
file: 'tokens/upgradable-token-rules.lotus',
|
|
123
|
+
summary: 'Mint tree for in-place upgradable tokens bound to initial_rules.',
|
|
124
|
+
walletBadge: 'Upgradable Token Mint',
|
|
125
|
+
actions: { issue: 'Mint tokens bound to the initial upgradable rules contract' },
|
|
126
|
+
},
|
|
70
127
|
// NFTs & Collections
|
|
71
128
|
{
|
|
72
129
|
id: 'std/nft/unique-nft',
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `lotus-token` CLI: instantiate and verify LRC-20 token bundles with
|
|
4
|
+
* cryptographically committed `lotus/token-metadata/1` manifests.
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { createLrc20TokenBundle, verifyTokenMetadataBundle, } from './token-metadata.js';
|
|
8
|
+
function usage() {
|
|
9
|
+
console.error([
|
|
10
|
+
'Usage:',
|
|
11
|
+
' lotus-token init --name <name> --symbol <SYM> --decimals <0..18> --issuer <64-hex> [--rules <64-hex>] [--description <text>] [--website <https://...>] [--icon-uri <uri>] [--out <file.json>]',
|
|
12
|
+
' lotus-token verify <bundle.json>',
|
|
13
|
+
].join('\n'));
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
function flag(args, name) {
|
|
17
|
+
const idx = args.indexOf(name);
|
|
18
|
+
if (idx === -1)
|
|
19
|
+
return undefined;
|
|
20
|
+
const val = args[idx + 1];
|
|
21
|
+
if (val === undefined || val.startsWith('--'))
|
|
22
|
+
throw new Error(`missing value for ${name}`);
|
|
23
|
+
return val;
|
|
24
|
+
}
|
|
25
|
+
const [cmd, ...rest] = process.argv.slice(2);
|
|
26
|
+
if (!cmd || cmd === '--help' || cmd === '-h')
|
|
27
|
+
usage();
|
|
28
|
+
try {
|
|
29
|
+
if (cmd === 'init') {
|
|
30
|
+
const name = flag(rest, '--name');
|
|
31
|
+
const symbol = flag(rest, '--symbol');
|
|
32
|
+
const decimalsRaw = flag(rest, '--decimals');
|
|
33
|
+
const issuer = flag(rest, '--issuer');
|
|
34
|
+
if (!name || !symbol || decimalsRaw === undefined || !issuer)
|
|
35
|
+
usage();
|
|
36
|
+
const decimals = Number(decimalsRaw);
|
|
37
|
+
const metadata = {
|
|
38
|
+
name,
|
|
39
|
+
symbol,
|
|
40
|
+
decimals,
|
|
41
|
+
...(flag(rest, '--description') ? { description: flag(rest, '--description') } : {}),
|
|
42
|
+
...(flag(rest, '--website') ? { website: flag(rest, '--website') } : {}),
|
|
43
|
+
...(flag(rest, '--icon-uri') ? { iconUri: flag(rest, '--icon-uri') } : {}),
|
|
44
|
+
...(flag(rest, '--icon-blake3') ? { iconBlake3: flag(rest, '--icon-blake3') } : {}),
|
|
45
|
+
};
|
|
46
|
+
const bundle = createLrc20TokenBundle({
|
|
47
|
+
metadata,
|
|
48
|
+
issuer,
|
|
49
|
+
transferRulesRoot: flag(rest, '--rules'),
|
|
50
|
+
});
|
|
51
|
+
const json = `${JSON.stringify(bundle, null, 2)}\n`;
|
|
52
|
+
const outPath = flag(rest, '--out');
|
|
53
|
+
if (outPath) {
|
|
54
|
+
writeFileSync(outPath, json, 'utf8');
|
|
55
|
+
console.log(`Wrote LRC-20 token bundle to ${outPath}`);
|
|
56
|
+
console.log(`token_id: ${bundle.tokenId}`);
|
|
57
|
+
console.log(`metadata_hash: ${bundle.metadataHash}`);
|
|
58
|
+
console.log(`rules_root: ${bundle.transferRulesRoot}`);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
process.stdout.write(json);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else if (cmd === 'verify') {
|
|
65
|
+
const file = rest[0];
|
|
66
|
+
if (!file)
|
|
67
|
+
usage();
|
|
68
|
+
const raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
69
|
+
const res = verifyTokenMetadataBundle(raw);
|
|
70
|
+
console.log(`OK ${res.metadata.symbol} (${res.metadata.name}, decimals=${res.metadata.decimals})`);
|
|
71
|
+
console.log(`token_id: ${res.tokenId}`);
|
|
72
|
+
console.log(`metadata_hash: ${res.metadataHash}`);
|
|
73
|
+
console.log(`rules_root: ${res.transferRulesRoot}`);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
usage();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
console.error(`lotus-token error: ${err.message}`);
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type ContractArtifact } from './generic.js';
|
|
2
|
+
export declare const TOKEN_METADATA_SCHEMA: "lotus/token-metadata/1";
|
|
3
|
+
export declare const TOKEN_BUNDLE_SCHEMA: "lotus/token-bundle/1";
|
|
4
|
+
export interface TokenMetadataInput {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly symbol: string;
|
|
7
|
+
readonly decimals: number;
|
|
8
|
+
readonly standard?: 'LRC-20' | 'LRC-721';
|
|
9
|
+
readonly description?: string;
|
|
10
|
+
readonly website?: string;
|
|
11
|
+
readonly iconUri?: string;
|
|
12
|
+
readonly iconBlake3?: string;
|
|
13
|
+
readonly authorityPubkey?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface TokenMetadata extends TokenMetadataInput {
|
|
16
|
+
readonly schema: typeof TOKEN_METADATA_SCHEMA;
|
|
17
|
+
readonly standard: 'LRC-20' | 'LRC-721';
|
|
18
|
+
}
|
|
19
|
+
export interface Lrc20TokenBundle {
|
|
20
|
+
readonly schema: typeof TOKEN_BUNDLE_SCHEMA;
|
|
21
|
+
readonly tokenId: string;
|
|
22
|
+
readonly metadataHash: string;
|
|
23
|
+
readonly transferRulesRoot: string;
|
|
24
|
+
readonly metadata: TokenMetadata;
|
|
25
|
+
readonly canonicalMetadataJson: string;
|
|
26
|
+
readonly mintArtifact: ContractArtifact;
|
|
27
|
+
}
|
|
28
|
+
export declare function validateTokenMetadata(input: TokenMetadataInput): TokenMetadata;
|
|
29
|
+
export declare function canonicalizeTokenMetadata(input: TokenMetadataInput): string;
|
|
30
|
+
export declare function hashTokenMetadata(input: TokenMetadataInput): string;
|
|
31
|
+
export interface CreateLrc20TokenOptions {
|
|
32
|
+
readonly metadata: TokenMetadataInput;
|
|
33
|
+
readonly issuer: string;
|
|
34
|
+
readonly transferRulesRoot?: string;
|
|
35
|
+
}
|
|
36
|
+
export declare function createLrc20TokenBundle(options: CreateLrc20TokenOptions): Lrc20TokenBundle;
|
|
37
|
+
export declare function verifyTokenMetadataBundle(bundle: Lrc20TokenBundle): {
|
|
38
|
+
readonly ok: true;
|
|
39
|
+
readonly tokenId: string;
|
|
40
|
+
readonly metadataHash: string;
|
|
41
|
+
readonly transferRulesRoot: string;
|
|
42
|
+
readonly metadata: TokenMetadata;
|
|
43
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical Lotus Token Metadata & Publishing Standard (`lotus/token-metadata/1`).
|
|
3
|
+
*
|
|
4
|
+
* Binds human-readable token metadata (name, symbol, decimals, icon, URI)
|
|
5
|
+
* cryptographically into the token's `token_id` via `BLAKE3(canonical_json)`
|
|
6
|
+
* committed in the mint contract's `metadata_hash` constructor parameter.
|
|
7
|
+
*/
|
|
8
|
+
import { blake3 } from '@noble/hashes/blake3';
|
|
9
|
+
import { hex } from './compiler.js';
|
|
10
|
+
import { assertContractArtifact } from './call-runtime.js';
|
|
11
|
+
import { verifyArtifactSource } from './generic.js';
|
|
12
|
+
import { compileStandardContract, UNIVERSAL_STANDARD_TRANSFER_RULES_ROOT, } from './std.js';
|
|
13
|
+
export const TOKEN_METADATA_SCHEMA = 'lotus/token-metadata/1';
|
|
14
|
+
export const TOKEN_BUNDLE_SCHEMA = 'lotus/token-bundle/1';
|
|
15
|
+
const HEX_32_RE = /^[0-9a-f]{64}$/;
|
|
16
|
+
const SYMBOL_RE = /^[A-Z0-9][A-Z0-9._-]{0,15}$/;
|
|
17
|
+
const CONTROL_CHAR_RE = /[\u0000-\u001f\u007f]/;
|
|
18
|
+
function assertCleanString(value, field, minLen, maxLen) {
|
|
19
|
+
if (typeof value !== 'string')
|
|
20
|
+
throw new Error(`${field} must be a string`);
|
|
21
|
+
if (value !== value.trim())
|
|
22
|
+
throw new Error(`${field} must not have leading or trailing whitespace`);
|
|
23
|
+
if (CONTROL_CHAR_RE.test(value))
|
|
24
|
+
throw new Error(`${field} must not contain control characters`);
|
|
25
|
+
const len = [...value].length;
|
|
26
|
+
if (len < minLen || len > maxLen)
|
|
27
|
+
throw new Error(`${field} length must be ${minLen}..${maxLen} characters`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
export function validateTokenMetadata(input) {
|
|
31
|
+
if (!input || typeof input !== 'object')
|
|
32
|
+
throw new Error('token metadata must be an object');
|
|
33
|
+
const name = assertCleanString(input.name, 'name', 1, 64);
|
|
34
|
+
const symbol = assertCleanString(input.symbol, 'symbol', 1, 16);
|
|
35
|
+
if (!SYMBOL_RE.test(symbol)) {
|
|
36
|
+
throw new Error('symbol must match /^[A-Z0-9][A-Z0-9._-]{0,15}$/');
|
|
37
|
+
}
|
|
38
|
+
if (!Number.isInteger(input.decimals) || input.decimals < 0 || input.decimals > 18) {
|
|
39
|
+
throw new Error('decimals must be an integer between 0 and 18');
|
|
40
|
+
}
|
|
41
|
+
const standard = input.standard ?? 'LRC-20';
|
|
42
|
+
if (standard !== 'LRC-20' && standard !== 'LRC-721') {
|
|
43
|
+
throw new Error('standard must be "LRC-20" or "LRC-721"');
|
|
44
|
+
}
|
|
45
|
+
if (standard === 'LRC-721' && input.decimals !== 0) {
|
|
46
|
+
throw new Error('LRC-721 metadata requires decimals === 0');
|
|
47
|
+
}
|
|
48
|
+
const out = {
|
|
49
|
+
schema: TOKEN_METADATA_SCHEMA,
|
|
50
|
+
standard,
|
|
51
|
+
name,
|
|
52
|
+
symbol,
|
|
53
|
+
decimals: input.decimals,
|
|
54
|
+
};
|
|
55
|
+
if (input.description !== undefined) {
|
|
56
|
+
out.description = assertCleanString(input.description, 'description', 1, 512);
|
|
57
|
+
}
|
|
58
|
+
if (input.website !== undefined) {
|
|
59
|
+
const uri = assertCleanString(input.website, 'website', 8, 256);
|
|
60
|
+
if (!uri.startsWith('https://'))
|
|
61
|
+
throw new Error('website must use https://');
|
|
62
|
+
out.website = uri;
|
|
63
|
+
}
|
|
64
|
+
if (input.iconUri !== undefined) {
|
|
65
|
+
const uri = assertCleanString(input.iconUri, 'iconUri', 7, 256);
|
|
66
|
+
if (!uri.startsWith('https://') && !uri.startsWith('ipfs://')) {
|
|
67
|
+
throw new Error('iconUri must use https:// or ipfs://');
|
|
68
|
+
}
|
|
69
|
+
out.iconUri = uri;
|
|
70
|
+
}
|
|
71
|
+
if (input.iconBlake3 !== undefined) {
|
|
72
|
+
if (typeof input.iconBlake3 !== 'string' || !HEX_32_RE.test(input.iconBlake3)) {
|
|
73
|
+
throw new Error('iconBlake3 must be 64 lowercase hex characters');
|
|
74
|
+
}
|
|
75
|
+
out.iconBlake3 = input.iconBlake3;
|
|
76
|
+
}
|
|
77
|
+
if (input.authorityPubkey !== undefined) {
|
|
78
|
+
if (typeof input.authorityPubkey !== 'string' || !HEX_32_RE.test(input.authorityPubkey)) {
|
|
79
|
+
throw new Error('authorityPubkey must be 64 lowercase hex characters (32-byte x-only pubkey)');
|
|
80
|
+
}
|
|
81
|
+
out.authorityPubkey = input.authorityPubkey;
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
function canonicalJsonValue(value) {
|
|
86
|
+
if (value === null || typeof value !== 'object')
|
|
87
|
+
return JSON.stringify(value);
|
|
88
|
+
if (Array.isArray(value))
|
|
89
|
+
return `[${value.map(canonicalJsonValue).join(',')}]`;
|
|
90
|
+
const entries = Object.entries(value)
|
|
91
|
+
.filter(([, v]) => v !== undefined)
|
|
92
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
93
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJsonValue(v)}`).join(',')}}`;
|
|
94
|
+
}
|
|
95
|
+
export function canonicalizeTokenMetadata(input) {
|
|
96
|
+
const validated = validateTokenMetadata(input);
|
|
97
|
+
return canonicalJsonValue(validated);
|
|
98
|
+
}
|
|
99
|
+
export function hashTokenMetadata(input) {
|
|
100
|
+
const canonical = canonicalizeTokenMetadata(input);
|
|
101
|
+
return hex(blake3(new TextEncoder().encode(canonical)));
|
|
102
|
+
}
|
|
103
|
+
export function createLrc20TokenBundle(options) {
|
|
104
|
+
if (typeof options.issuer !== 'string' || !HEX_32_RE.test(options.issuer)) {
|
|
105
|
+
throw new Error('issuer must be 64 lowercase hex characters (32-byte x-only pubkey)');
|
|
106
|
+
}
|
|
107
|
+
const transferRulesRoot = (options.transferRulesRoot ?? UNIVERSAL_STANDARD_TRANSFER_RULES_ROOT).toLowerCase();
|
|
108
|
+
if (!HEX_32_RE.test(transferRulesRoot)) {
|
|
109
|
+
throw new Error('transferRulesRoot must be 64 lowercase hex characters');
|
|
110
|
+
}
|
|
111
|
+
const metadata = validateTokenMetadata({ ...options.metadata, standard: options.metadata.standard ?? 'LRC-20' });
|
|
112
|
+
const canonicalMetadataJson = canonicalizeTokenMetadata(metadata);
|
|
113
|
+
const metadataHash = hex(blake3(new TextEncoder().encode(canonicalMetadataJson)));
|
|
114
|
+
const { artifact: mintArtifact } = compileStandardContract('std/tokens/lrc20', {
|
|
115
|
+
issuer: options.issuer,
|
|
116
|
+
metadata_hash: metadataHash,
|
|
117
|
+
transfer_rules: transferRulesRoot,
|
|
118
|
+
});
|
|
119
|
+
const tokenId = mintArtifact.methods[0].root;
|
|
120
|
+
return {
|
|
121
|
+
schema: TOKEN_BUNDLE_SCHEMA,
|
|
122
|
+
tokenId,
|
|
123
|
+
metadataHash,
|
|
124
|
+
transferRulesRoot,
|
|
125
|
+
metadata,
|
|
126
|
+
canonicalMetadataJson,
|
|
127
|
+
mintArtifact,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export function verifyTokenMetadataBundle(bundle) {
|
|
131
|
+
if (!bundle || typeof bundle !== 'object')
|
|
132
|
+
throw new Error('bundle must be an object');
|
|
133
|
+
if (bundle.schema !== TOKEN_BUNDLE_SCHEMA) {
|
|
134
|
+
throw new Error(`expected bundle schema ${TOKEN_BUNDLE_SCHEMA}, got ${String(bundle.schema)}`);
|
|
135
|
+
}
|
|
136
|
+
const metadata = validateTokenMetadata(bundle.metadata);
|
|
137
|
+
const canonical = canonicalizeTokenMetadata(metadata);
|
|
138
|
+
if (bundle.canonicalMetadataJson !== canonical) {
|
|
139
|
+
throw new Error('canonicalMetadataJson does not match canonicalized metadata');
|
|
140
|
+
}
|
|
141
|
+
const computedHash = hex(blake3(new TextEncoder().encode(canonical)));
|
|
142
|
+
if (bundle.metadataHash !== computedHash) {
|
|
143
|
+
throw new Error(`metadataHash mismatch: expected ${computedHash}, got ${bundle.metadataHash}`);
|
|
144
|
+
}
|
|
145
|
+
assertContractArtifact(bundle.mintArtifact);
|
|
146
|
+
verifyArtifactSource(bundle.mintArtifact);
|
|
147
|
+
const mintRoot = bundle.mintArtifact.methods[0]?.root;
|
|
148
|
+
if (!mintRoot || mintRoot !== bundle.tokenId) {
|
|
149
|
+
throw new Error(`tokenId mismatch: artifact root ${String(mintRoot)} !== ${bundle.tokenId}`);
|
|
150
|
+
}
|
|
151
|
+
const boundHash = bundle.mintArtifact.bindings.find((b) => b.name === 'metadata_hash')?.value;
|
|
152
|
+
if (boundHash !== computedHash) {
|
|
153
|
+
throw new Error(`mintArtifact metadata_hash binding (${String(boundHash)}) does not match computed metadataHash (${computedHash})`);
|
|
154
|
+
}
|
|
155
|
+
const boundRules = bundle.mintArtifact.bindings.find((b) => b.name === 'transfer_rules')?.value;
|
|
156
|
+
if (boundRules !== bundle.transferRulesRoot) {
|
|
157
|
+
throw new Error(`mintArtifact transfer_rules binding (${String(boundRules)}) does not match transferRulesRoot (${bundle.transferRulesRoot})`);
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
ok: true,
|
|
161
|
+
tokenId: bundle.tokenId,
|
|
162
|
+
metadataHash: computedHash,
|
|
163
|
+
transferRulesRoot: bundle.transferRulesRoot,
|
|
164
|
+
metadata,
|
|
165
|
+
};
|
|
166
|
+
}
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@givelotus/compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Lotus (XPI) smart-contract compiler featuring a Pratt parser, Forth IR, nominal hash types, static compute-unit (cu) bounds, and the Edition 2026.2 std/ library.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://lotus.net/developers/",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"files": [
|
|
28
28
|
"dist/",
|
|
29
29
|
"std/",
|
|
30
|
+
"std-pkg/",
|
|
30
31
|
"*.schema.json",
|
|
31
32
|
"README.md"
|
|
32
33
|
],
|
|
@@ -43,7 +44,8 @@
|
|
|
43
44
|
"./semantic-ir": "./dist/semantic-ir.js",
|
|
44
45
|
"./ecash": "./dist/ecash.js",
|
|
45
46
|
"./target-compiler": "./dist/target-compiler.js",
|
|
46
|
-
"./std": "./dist/std.js"
|
|
47
|
+
"./std": "./dist/std.js",
|
|
48
|
+
"./token-metadata": "./dist/token-metadata.js"
|
|
47
49
|
},
|
|
48
50
|
"bin": {
|
|
49
51
|
"lotus-compile": "dist/cli.js",
|
|
@@ -51,7 +53,8 @@
|
|
|
51
53
|
"lotus-contract-compile": "dist/generic-cli.js",
|
|
52
54
|
"lotus-package-lock": "dist/package-lock-cli.js",
|
|
53
55
|
"lotus-package-fetch": "dist/package-fetch-cli.js",
|
|
54
|
-
"lotus-package-update": "dist/package-update-cli.js"
|
|
56
|
+
"lotus-package-update": "dist/package-update-cli.js",
|
|
57
|
+
"lotus-token": "dist/token-cli.js"
|
|
55
58
|
},
|
|
56
59
|
"scripts": {
|
|
57
60
|
"build": "tsc -p .",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
pragma edition "2026.2";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// Lotus Standard Fungible Token (LRC-20) with immutable BLAKE3 metadata commitment.
|
|
5
|
+
//
|
|
6
|
+
// `metadata_hash` commits the canonical `lotus/token-metadata/1` JSON manifest
|
|
7
|
+
// directly into the `token_id` (the MAST root of this mint tree).
|
|
8
|
+
// `transfer_rules` defaults to `UNIVERSAL_STANDARD_TRANSFER_RULES_ROOT` for
|
|
9
|
+
// unrestricted fungible tokens, or any custom/upgradable rules BranchRoot.
|
|
10
|
+
|
|
11
|
+
token MetadataFungibleToken<
|
|
12
|
+
issuer: PublicKey,
|
|
13
|
+
metadata_hash: Bytes32,
|
|
14
|
+
transfer_rules: BranchRoot
|
|
15
|
+
> {
|
|
16
|
+
function issue(amount: Amount, commitment: Bytes32, signature: Signature) {
|
|
17
|
+
require(checksig(signature, issuer));
|
|
18
|
+
require(commitment == metadata_hash);
|
|
19
|
+
for output in tx.outputs {
|
|
20
|
+
if output.token_id == this.root {
|
|
21
|
+
require(output.requires_branch(transfer_rules));
|
|
22
|
+
require(!output.exclusive_branch(transfer_rules));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
pragma edition "2026.2";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// Stateful fungible token supporting transfer, 1-to-2 split, and 2-to-1 UTXO merge
|
|
5
|
+
// while conserving per-UTXO TokenState { weight, min_lock_height }.
|
|
6
|
+
|
|
7
|
+
state inline TokenState {
|
|
8
|
+
weight: Int,
|
|
9
|
+
min_lock_height: Int,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
contract StatefulTokenRules {
|
|
13
|
+
function transfer(weight: Int, lock_h: Int) {
|
|
14
|
+
require(weight >= 0);
|
|
15
|
+
require(lock_h >= 0);
|
|
16
|
+
require(TokenState.verify(weight, lock_h));
|
|
17
|
+
|
|
18
|
+
in_count = 0;
|
|
19
|
+
for input in tx.inputs {
|
|
20
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
21
|
+
in_count += 1;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
require(in_count == 1);
|
|
25
|
+
|
|
26
|
+
out_count = 0;
|
|
27
|
+
for output in tx.outputs {
|
|
28
|
+
if output.token_id == this.coin.token_id {
|
|
29
|
+
out_count += 1;
|
|
30
|
+
require(output.value == this.coin.value);
|
|
31
|
+
require(output.requires_branch(this.root));
|
|
32
|
+
require(!output.exclusive_branch(this.root));
|
|
33
|
+
require(TokenState.matches(output, weight, lock_h));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
require(out_count == 1);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function split(weight_in: Int, weight_0: Int, weight_1: Int, lock_h: Int) {
|
|
40
|
+
require(weight_0 >= 0);
|
|
41
|
+
require(weight_1 >= 0);
|
|
42
|
+
require(weight_0 + weight_1 == weight_in);
|
|
43
|
+
require(lock_h >= 0);
|
|
44
|
+
require(TokenState.verify(weight_in, lock_h));
|
|
45
|
+
|
|
46
|
+
in_count = 0;
|
|
47
|
+
for input in tx.inputs {
|
|
48
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
49
|
+
in_count += 1;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
require(in_count == 1);
|
|
53
|
+
|
|
54
|
+
out_count = 0;
|
|
55
|
+
out_value = 0;
|
|
56
|
+
for output in tx.outputs {
|
|
57
|
+
if output.token_id == this.coin.token_id {
|
|
58
|
+
require(output.requires_branch(this.root));
|
|
59
|
+
require(!output.exclusive_branch(this.root));
|
|
60
|
+
out_value += output.value;
|
|
61
|
+
if out_count == 0 {
|
|
62
|
+
require(TokenState.matches(output, weight_0, lock_h));
|
|
63
|
+
} else {
|
|
64
|
+
require(TokenState.matches(output, weight_1, lock_h));
|
|
65
|
+
}
|
|
66
|
+
out_count += 1;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
require(out_count == 2);
|
|
70
|
+
require(out_value == this.coin.value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function merge(weight_a: Int, lock_a: Int, weight_b: Int, lock_b: Int) {
|
|
74
|
+
require(weight_a >= 0);
|
|
75
|
+
require(weight_b >= 0);
|
|
76
|
+
require(lock_a >= 0);
|
|
77
|
+
require(lock_b >= 0);
|
|
78
|
+
|
|
79
|
+
in_count = 0;
|
|
80
|
+
total_value = 0;
|
|
81
|
+
for input in tx.inputs {
|
|
82
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
83
|
+
require(input.coin.requires_branch(this.root));
|
|
84
|
+
if in_count == 0 {
|
|
85
|
+
require(input.coin.state_root == TokenState.hash(weight_a, lock_a));
|
|
86
|
+
} else {
|
|
87
|
+
require(input.coin.state_root == TokenState.hash(weight_b, lock_b));
|
|
88
|
+
}
|
|
89
|
+
total_value += input.coin.value;
|
|
90
|
+
in_count += 1;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
require(in_count == 2);
|
|
94
|
+
|
|
95
|
+
merged_lock = lock_a;
|
|
96
|
+
if lock_b > lock_a {
|
|
97
|
+
merged_lock = lock_b;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
out_count = 0;
|
|
101
|
+
for output in tx.outputs {
|
|
102
|
+
if output.token_id == this.coin.token_id {
|
|
103
|
+
out_count += 1;
|
|
104
|
+
require(output.value == total_value);
|
|
105
|
+
require(output.requires_branch(this.root));
|
|
106
|
+
require(!output.exclusive_branch(this.root));
|
|
107
|
+
require(TokenState.matches(output, weight_a + weight_b, merged_lock));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
require(out_count == 1);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
token StatefulToken<issuer: PublicKey> {
|
|
115
|
+
function issue(amount: Amount, init_weight: Int, init_lock: Int, signature: Signature) {
|
|
116
|
+
require(checksig(signature, issuer));
|
|
117
|
+
require(init_weight >= 0);
|
|
118
|
+
require(init_lock >= 0);
|
|
119
|
+
|
|
120
|
+
out_count = 0;
|
|
121
|
+
for output in tx.outputs {
|
|
122
|
+
if output.token_id == this.root {
|
|
123
|
+
out_count += 1;
|
|
124
|
+
require(output.value == amount);
|
|
125
|
+
require(output.requires_branch(contract_root(StatefulTokenRules)));
|
|
126
|
+
require(!output.exclusive_branch(contract_root(StatefulTokenRules)));
|
|
127
|
+
require(TokenState.matches(output, init_weight, init_lock));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
require(out_count == 1);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
pragma edition "2026.2";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// In-place token contract upgrade keeping the exact same token_id.
|
|
5
|
+
// Governance publishes one reusable checkdatasig attestation over
|
|
6
|
+
// blake3(token_id || new_rules || upgrade_version).
|
|
7
|
+
|
|
8
|
+
contract UpgradableRules<governance: PublicKey> {
|
|
9
|
+
function transfer() {
|
|
10
|
+
for output in tx.outputs {
|
|
11
|
+
if output.token_id == this.coin.token_id {
|
|
12
|
+
require(output.requires_branch(this.root));
|
|
13
|
+
require(!output.exclusive_branch(this.root));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function upgrade_contract(
|
|
19
|
+
new_rules: ContractRootHash,
|
|
20
|
+
upgrade_version: Int,
|
|
21
|
+
gov_sig: Signature
|
|
22
|
+
) {
|
|
23
|
+
require(upgrade_version > 0);
|
|
24
|
+
require(new_rules != this.root);
|
|
25
|
+
|
|
26
|
+
require(
|
|
27
|
+
checkdatasig(
|
|
28
|
+
gov_sig,
|
|
29
|
+
blake3(
|
|
30
|
+
cat(
|
|
31
|
+
bytes32(this.coin.token_id),
|
|
32
|
+
cat(bytes32(new_rules), num2bin(upgrade_version, 8))
|
|
33
|
+
)
|
|
34
|
+
),
|
|
35
|
+
governance,
|
|
36
|
+
0
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
in_total = 0;
|
|
41
|
+
for input in tx.inputs {
|
|
42
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
43
|
+
require(input.coin.requires_branch(this.root));
|
|
44
|
+
in_total += input.coin.value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
out_total = 0;
|
|
49
|
+
for output in tx.outputs {
|
|
50
|
+
if output.token_id == this.coin.token_id {
|
|
51
|
+
require(output.requires_branch(new_rules));
|
|
52
|
+
require(!output.exclusive_branch(new_rules));
|
|
53
|
+
out_total += output.value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
require(out_total == in_total);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
token UpgradableToken<issuer: PublicKey, initial_rules: ContractRootHash> {
|
|
61
|
+
function issue(amount: Amount, signature: Signature) {
|
|
62
|
+
require(checksig(signature, issuer));
|
|
63
|
+
for output in tx.outputs {
|
|
64
|
+
if output.token_id == this.root {
|
|
65
|
+
require(output.requires_branch(initial_rules));
|
|
66
|
+
require(!output.exclusive_branch(initial_rules));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
pragma edition "2026.2";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// Standard Lotus fungible token (LRC-20) templates.
|
|
5
|
+
|
|
6
|
+
export contract TokenWallet<owner: PublicKey> {
|
|
7
|
+
function spend(signature: Signature) {
|
|
8
|
+
require(checksig(signature, owner));
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export contract StandardTransferRules {
|
|
13
|
+
function transfer() {
|
|
14
|
+
for output in tx.outputs {
|
|
15
|
+
if output.token_id == this.coin.token_id {
|
|
16
|
+
require(output.requires_branch(this.root));
|
|
17
|
+
require(!output.exclusive_branch(this.root));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export token MetadataFungibleToken<
|
|
24
|
+
issuer: PublicKey,
|
|
25
|
+
metadata_hash: Bytes32,
|
|
26
|
+
transfer_rules: BranchRoot
|
|
27
|
+
> {
|
|
28
|
+
function issue(amount: Amount, commitment: Bytes32, signature: Signature) {
|
|
29
|
+
require(checksig(signature, issuer));
|
|
30
|
+
require(commitment == metadata_hash);
|
|
31
|
+
for output in tx.outputs {
|
|
32
|
+
if output.token_id == this.root {
|
|
33
|
+
require(output.requires_branch(transfer_rules));
|
|
34
|
+
require(!output.exclusive_branch(transfer_rules));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
pragma edition "2026.2";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// Stateful fungible token with transfer, 1-to-2 split, and 2-to-1 UTXO merge.
|
|
5
|
+
|
|
6
|
+
state inline TokenState {
|
|
7
|
+
weight: Int,
|
|
8
|
+
min_lock_height: Int,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export contract StatefulTokenRules {
|
|
12
|
+
function transfer(weight: Int, lock_h: Int) {
|
|
13
|
+
require(weight >= 0);
|
|
14
|
+
require(lock_h >= 0);
|
|
15
|
+
require(TokenState.verify(weight, lock_h));
|
|
16
|
+
|
|
17
|
+
in_count = 0;
|
|
18
|
+
for input in tx.inputs {
|
|
19
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
20
|
+
in_count += 1;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
require(in_count == 1);
|
|
24
|
+
|
|
25
|
+
out_count = 0;
|
|
26
|
+
for output in tx.outputs {
|
|
27
|
+
if output.token_id == this.coin.token_id {
|
|
28
|
+
out_count += 1;
|
|
29
|
+
require(output.value == this.coin.value);
|
|
30
|
+
require(output.requires_branch(this.root));
|
|
31
|
+
require(!output.exclusive_branch(this.root));
|
|
32
|
+
require(TokenState.matches(output, weight, lock_h));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
require(out_count == 1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function split(weight_in: Int, weight_0: Int, weight_1: Int, lock_h: Int) {
|
|
39
|
+
require(weight_0 >= 0);
|
|
40
|
+
require(weight_1 >= 0);
|
|
41
|
+
require(weight_0 + weight_1 == weight_in);
|
|
42
|
+
require(lock_h >= 0);
|
|
43
|
+
require(TokenState.verify(weight_in, lock_h));
|
|
44
|
+
|
|
45
|
+
in_count = 0;
|
|
46
|
+
for input in tx.inputs {
|
|
47
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
48
|
+
in_count += 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
require(in_count == 1);
|
|
52
|
+
|
|
53
|
+
out_count = 0;
|
|
54
|
+
out_value = 0;
|
|
55
|
+
for output in tx.outputs {
|
|
56
|
+
if output.token_id == this.coin.token_id {
|
|
57
|
+
require(output.requires_branch(this.root));
|
|
58
|
+
require(!output.exclusive_branch(this.root));
|
|
59
|
+
out_value += output.value;
|
|
60
|
+
if out_count == 0 {
|
|
61
|
+
require(TokenState.matches(output, weight_0, lock_h));
|
|
62
|
+
} else {
|
|
63
|
+
require(TokenState.matches(output, weight_1, lock_h));
|
|
64
|
+
}
|
|
65
|
+
out_count += 1;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
require(out_count == 2);
|
|
69
|
+
require(out_value == this.coin.value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function merge(weight_a: Int, lock_a: Int, weight_b: Int, lock_b: Int) {
|
|
73
|
+
require(weight_a >= 0);
|
|
74
|
+
require(weight_b >= 0);
|
|
75
|
+
require(lock_a >= 0);
|
|
76
|
+
require(lock_b >= 0);
|
|
77
|
+
|
|
78
|
+
in_count = 0;
|
|
79
|
+
total_value = 0;
|
|
80
|
+
for input in tx.inputs {
|
|
81
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
82
|
+
require(input.coin.requires_branch(this.root));
|
|
83
|
+
if in_count == 0 {
|
|
84
|
+
require(input.coin.state_root == TokenState.hash(weight_a, lock_a));
|
|
85
|
+
} else {
|
|
86
|
+
require(input.coin.state_root == TokenState.hash(weight_b, lock_b));
|
|
87
|
+
}
|
|
88
|
+
total_value += input.coin.value;
|
|
89
|
+
in_count += 1;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
require(in_count == 2);
|
|
93
|
+
|
|
94
|
+
merged_lock = lock_a;
|
|
95
|
+
if lock_b > lock_a {
|
|
96
|
+
merged_lock = lock_b;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
out_count = 0;
|
|
100
|
+
for output in tx.outputs {
|
|
101
|
+
if output.token_id == this.coin.token_id {
|
|
102
|
+
out_count += 1;
|
|
103
|
+
require(output.value == total_value);
|
|
104
|
+
require(output.requires_branch(this.root));
|
|
105
|
+
require(!output.exclusive_branch(this.root));
|
|
106
|
+
require(TokenState.matches(output, weight_a + weight_b, merged_lock));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
require(out_count == 1);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export token StatefulToken<issuer: PublicKey> {
|
|
114
|
+
function issue(amount: Amount, init_weight: Int, init_lock: Int, signature: Signature) {
|
|
115
|
+
require(checksig(signature, issuer));
|
|
116
|
+
require(init_weight >= 0);
|
|
117
|
+
require(init_lock >= 0);
|
|
118
|
+
|
|
119
|
+
out_count = 0;
|
|
120
|
+
for output in tx.outputs {
|
|
121
|
+
if output.token_id == this.root {
|
|
122
|
+
out_count += 1;
|
|
123
|
+
require(output.value == amount);
|
|
124
|
+
require(output.requires_branch(contract_root(StatefulTokenRules)));
|
|
125
|
+
require(!output.exclusive_branch(contract_root(StatefulTokenRules)));
|
|
126
|
+
require(TokenState.matches(output, init_weight, init_lock));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
require(out_count == 1);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
pragma edition "2026.2";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// In-place token contract upgrade keeping the exact same token_id.
|
|
5
|
+
|
|
6
|
+
export contract UpgradableRules<governance: PublicKey> {
|
|
7
|
+
function transfer() {
|
|
8
|
+
for output in tx.outputs {
|
|
9
|
+
if output.token_id == this.coin.token_id {
|
|
10
|
+
require(output.requires_branch(this.root));
|
|
11
|
+
require(!output.exclusive_branch(this.root));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function upgrade_contract(
|
|
17
|
+
new_rules: ContractRootHash,
|
|
18
|
+
upgrade_version: Int,
|
|
19
|
+
gov_sig: Signature
|
|
20
|
+
) {
|
|
21
|
+
require(upgrade_version > 0);
|
|
22
|
+
require(new_rules != this.root);
|
|
23
|
+
|
|
24
|
+
require(
|
|
25
|
+
checkdatasig(
|
|
26
|
+
gov_sig,
|
|
27
|
+
blake3(
|
|
28
|
+
cat(
|
|
29
|
+
bytes32(this.coin.token_id),
|
|
30
|
+
cat(bytes32(new_rules), num2bin(upgrade_version, 8))
|
|
31
|
+
)
|
|
32
|
+
),
|
|
33
|
+
governance,
|
|
34
|
+
0
|
|
35
|
+
)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
in_total = 0;
|
|
39
|
+
for input in tx.inputs {
|
|
40
|
+
if input.is_spend && input.coin.token_id == this.coin.token_id {
|
|
41
|
+
require(input.coin.requires_branch(this.root));
|
|
42
|
+
in_total += input.coin.value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
out_total = 0;
|
|
47
|
+
for output in tx.outputs {
|
|
48
|
+
if output.token_id == this.coin.token_id {
|
|
49
|
+
require(output.requires_branch(new_rules));
|
|
50
|
+
require(!output.exclusive_branch(new_rules));
|
|
51
|
+
out_total += output.value;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
require(out_total == in_total);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export token UpgradableToken<issuer: PublicKey, initial_rules: ContractRootHash> {
|
|
59
|
+
function issue(amount: Amount, signature: Signature) {
|
|
60
|
+
require(checksig(signature, issuer));
|
|
61
|
+
for output in tx.outputs {
|
|
62
|
+
if output.token_id == this.root {
|
|
63
|
+
require(output.requires_branch(initial_rules));
|
|
64
|
+
require(!output.exclusive_branch(initial_rules));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|