@originals/sdk 1.8.1 → 1.8.2
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/utils/hash.js +1 -0
- package/package.json +6 -5
- package/src/adapters/FeeOracleMock.ts +9 -0
- package/src/adapters/index.ts +5 -0
- package/src/adapters/providers/OrdHttpProvider.ts +126 -0
- package/src/adapters/providers/OrdMockProvider.ts +101 -0
- package/src/adapters/types.ts +66 -0
- package/src/bitcoin/BitcoinManager.ts +329 -0
- package/src/bitcoin/BroadcastClient.ts +54 -0
- package/src/bitcoin/OrdinalsClient.ts +120 -0
- package/src/bitcoin/PSBTBuilder.ts +106 -0
- package/src/bitcoin/fee-calculation.ts +38 -0
- package/src/bitcoin/providers/OrdNodeProvider.ts +92 -0
- package/src/bitcoin/providers/OrdinalsProvider.ts +56 -0
- package/src/bitcoin/providers/types.ts +59 -0
- package/src/bitcoin/transactions/commit.ts +465 -0
- package/src/bitcoin/transactions/index.ts +13 -0
- package/src/bitcoin/transfer.ts +43 -0
- package/src/bitcoin/utxo-selection.ts +322 -0
- package/src/bitcoin/utxo.ts +113 -0
- package/src/cel/ExternalReferenceManager.ts +87 -0
- package/src/cel/OriginalsCel.ts +460 -0
- package/src/cel/algorithms/createEventLog.ts +68 -0
- package/src/cel/algorithms/deactivateEventLog.ts +109 -0
- package/src/cel/algorithms/index.ts +11 -0
- package/src/cel/algorithms/updateEventLog.ts +99 -0
- package/src/cel/algorithms/verifyEventLog.ts +306 -0
- package/src/cel/algorithms/witnessEvent.ts +87 -0
- package/src/cel/cli/create.ts +330 -0
- package/src/cel/cli/index.ts +383 -0
- package/src/cel/cli/inspect.ts +549 -0
- package/src/cel/cli/migrate.ts +473 -0
- package/src/cel/cli/verify.ts +249 -0
- package/src/cel/hash.ts +71 -0
- package/src/cel/index.ts +16 -0
- package/src/cel/layers/BtcoCelManager.ts +408 -0
- package/src/cel/layers/PeerCelManager.ts +371 -0
- package/src/cel/layers/WebVHCelManager.ts +361 -0
- package/src/cel/layers/index.ts +27 -0
- package/src/cel/serialization/cbor.ts +189 -0
- package/src/cel/serialization/index.ts +10 -0
- package/src/cel/serialization/json.ts +209 -0
- package/src/cel/types.ts +160 -0
- package/src/cel/witnesses/BitcoinWitness.ts +184 -0
- package/src/cel/witnesses/HttpWitness.ts +241 -0
- package/src/cel/witnesses/WitnessService.ts +51 -0
- package/src/cel/witnesses/index.ts +11 -0
- package/src/contexts/credentials-v1.json +237 -0
- package/src/contexts/credentials-v2-examples.json +5 -0
- package/src/contexts/credentials-v2.json +340 -0
- package/src/contexts/credentials.json +237 -0
- package/src/contexts/data-integrity-v2.json +81 -0
- package/src/contexts/dids.json +58 -0
- package/src/contexts/ed255192020.json +93 -0
- package/src/contexts/ordinals-plus.json +23 -0
- package/src/contexts/originals.json +22 -0
- package/src/core/OriginalsSDK.ts +420 -0
- package/src/crypto/Multikey.ts +194 -0
- package/src/crypto/Signer.ts +262 -0
- package/src/crypto/noble-init.ts +138 -0
- package/src/did/BtcoDidResolver.ts +231 -0
- package/src/did/DIDManager.ts +705 -0
- package/src/did/Ed25519Verifier.ts +68 -0
- package/src/did/KeyManager.ts +239 -0
- package/src/did/WebVHManager.ts +499 -0
- package/src/did/createBtcoDidDocument.ts +60 -0
- package/src/did/providers/OrdinalsClientProviderAdapter.ts +68 -0
- package/src/events/EventEmitter.ts +222 -0
- package/src/events/index.ts +19 -0
- package/src/events/types.ts +331 -0
- package/src/examples/basic-usage.ts +78 -0
- package/src/examples/create-module-original.ts +435 -0
- package/src/examples/full-lifecycle-flow.ts +514 -0
- package/src/examples/run.ts +60 -0
- package/src/index.ts +204 -0
- package/src/kinds/KindRegistry.ts +320 -0
- package/src/kinds/index.ts +74 -0
- package/src/kinds/types.ts +470 -0
- package/src/kinds/validators/AgentValidator.ts +257 -0
- package/src/kinds/validators/AppValidator.ts +211 -0
- package/src/kinds/validators/DatasetValidator.ts +242 -0
- package/src/kinds/validators/DocumentValidator.ts +311 -0
- package/src/kinds/validators/MediaValidator.ts +269 -0
- package/src/kinds/validators/ModuleValidator.ts +225 -0
- package/src/kinds/validators/base.ts +276 -0
- package/src/kinds/validators/index.ts +12 -0
- package/src/lifecycle/BatchOperations.ts +381 -0
- package/src/lifecycle/LifecycleManager.ts +2156 -0
- package/src/lifecycle/OriginalsAsset.ts +524 -0
- package/src/lifecycle/ProvenanceQuery.ts +280 -0
- package/src/lifecycle/ResourceVersioning.ts +163 -0
- package/src/migration/MigrationManager.ts +587 -0
- package/src/migration/audit/AuditLogger.ts +176 -0
- package/src/migration/checkpoint/CheckpointManager.ts +112 -0
- package/src/migration/checkpoint/CheckpointStorage.ts +101 -0
- package/src/migration/index.ts +33 -0
- package/src/migration/operations/BaseMigration.ts +126 -0
- package/src/migration/operations/PeerToBtcoMigration.ts +105 -0
- package/src/migration/operations/PeerToWebvhMigration.ts +62 -0
- package/src/migration/operations/WebvhToBtcoMigration.ts +105 -0
- package/src/migration/rollback/RollbackManager.ts +170 -0
- package/src/migration/state/StateMachine.ts +92 -0
- package/src/migration/state/StateTracker.ts +156 -0
- package/src/migration/types.ts +356 -0
- package/src/migration/validation/BitcoinValidator.ts +107 -0
- package/src/migration/validation/CredentialValidator.ts +62 -0
- package/src/migration/validation/DIDCompatibilityValidator.ts +151 -0
- package/src/migration/validation/LifecycleValidator.ts +64 -0
- package/src/migration/validation/StorageValidator.ts +79 -0
- package/src/migration/validation/ValidationPipeline.ts +213 -0
- package/src/resources/ResourceManager.ts +655 -0
- package/src/resources/index.ts +21 -0
- package/src/resources/types.ts +202 -0
- package/src/storage/LocalStorageAdapter.ts +64 -0
- package/src/storage/MemoryStorageAdapter.ts +29 -0
- package/src/storage/StorageAdapter.ts +25 -0
- package/src/storage/index.ts +3 -0
- package/src/types/bitcoin.ts +98 -0
- package/src/types/common.ts +92 -0
- package/src/types/credentials.ts +89 -0
- package/src/types/did.ts +31 -0
- package/src/types/external-shims.d.ts +53 -0
- package/src/types/index.ts +7 -0
- package/src/types/network.ts +178 -0
- package/src/utils/EventLogger.ts +298 -0
- package/src/utils/Logger.ts +324 -0
- package/src/utils/MetricsCollector.ts +358 -0
- package/src/utils/bitcoin-address.ts +132 -0
- package/src/utils/cbor.ts +31 -0
- package/src/utils/encoding.ts +135 -0
- package/src/utils/hash.ts +12 -0
- package/src/utils/retry.ts +46 -0
- package/src/utils/satoshi-validation.ts +196 -0
- package/src/utils/serialization.ts +102 -0
- package/src/utils/telemetry.ts +44 -0
- package/src/utils/validation.ts +123 -0
- package/src/vc/CredentialManager.ts +955 -0
- package/src/vc/Issuer.ts +105 -0
- package/src/vc/Verifier.ts +54 -0
- package/src/vc/cryptosuites/bbs.ts +253 -0
- package/src/vc/cryptosuites/bbsSimple.ts +21 -0
- package/src/vc/cryptosuites/eddsa.ts +99 -0
- package/src/vc/documentLoader.ts +81 -0
- package/src/vc/proofs/data-integrity.ts +33 -0
- package/src/vc/utils/jsonld.ts +18 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example: Full Lifecycle Flow
|
|
3
|
+
*
|
|
4
|
+
* This comprehensive example demonstrates the complete lifecycle of an Original:
|
|
5
|
+
*
|
|
6
|
+
* 1. Create a draft (did:peer) - Private, offline creation
|
|
7
|
+
* 2. Validate and estimate costs
|
|
8
|
+
* 3. Publish to web (did:webvh) - Public discovery
|
|
9
|
+
* 4. Inscribe on Bitcoin (did:btco) - Permanent ownership
|
|
10
|
+
* 5. Transfer ownership
|
|
11
|
+
* 6. Track provenance throughout
|
|
12
|
+
*
|
|
13
|
+
* This example uses the clean lifecycle API with progress callbacks.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
OriginalsSDK,
|
|
18
|
+
OrdMockProvider,
|
|
19
|
+
ResourceManager,
|
|
20
|
+
OriginalKind,
|
|
21
|
+
type LifecycleProgress,
|
|
22
|
+
type OriginalsAsset,
|
|
23
|
+
type CostEstimate,
|
|
24
|
+
type MigrationValidation
|
|
25
|
+
} from '../index';
|
|
26
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Helper to compute content hash
|
|
30
|
+
*/
|
|
31
|
+
function computeHash(content: string | Buffer): string {
|
|
32
|
+
const buffer = typeof content === 'string' ? Buffer.from(content) : content;
|
|
33
|
+
return Buffer.from(sha256(buffer)).toString('hex');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Progress logger for lifecycle operations
|
|
38
|
+
*/
|
|
39
|
+
function createProgressLogger(_operation: string) {
|
|
40
|
+
return (progress: LifecycleProgress) => {
|
|
41
|
+
const bar = '█'.repeat(Math.floor(progress.percentage / 5)) + '░'.repeat(20 - Math.floor(progress.percentage / 5));
|
|
42
|
+
console.log(` [${bar}] ${progress.percentage}% - ${progress.phase}: ${progress.message}`);
|
|
43
|
+
if (progress.details?.transactionId) {
|
|
44
|
+
console.log(` Transaction: ${progress.details.transactionId}`);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Initialize SDK with all necessary providers
|
|
51
|
+
*/
|
|
52
|
+
function initializeSDK(): OriginalsSDK {
|
|
53
|
+
return OriginalsSDK.create({
|
|
54
|
+
network: 'regtest',
|
|
55
|
+
defaultKeyType: 'Ed25519',
|
|
56
|
+
webvhNetwork: 'magby',
|
|
57
|
+
ordinalsProvider: new OrdMockProvider(),
|
|
58
|
+
enableLogging: false,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Step 1: Create resources using ResourceManager
|
|
64
|
+
*/
|
|
65
|
+
function createResources(resourceManager: ResourceManager): void {
|
|
66
|
+
console.log('\n📁 STEP 1: Creating Resources with ResourceManager\n');
|
|
67
|
+
|
|
68
|
+
// Create main content
|
|
69
|
+
const mainContent = `
|
|
70
|
+
# My Digital Artwork
|
|
71
|
+
|
|
72
|
+
This is a unique digital creation that will be permanently recorded
|
|
73
|
+
on the Bitcoin blockchain.
|
|
74
|
+
|
|
75
|
+
## Description
|
|
76
|
+
|
|
77
|
+
A beautiful generative art piece created with mathematical precision.
|
|
78
|
+
|
|
79
|
+
## Attribution
|
|
80
|
+
|
|
81
|
+
Created by: Artist Name
|
|
82
|
+
Date: ${new Date().toISOString()}
|
|
83
|
+
`.trim();
|
|
84
|
+
|
|
85
|
+
// Create the resource
|
|
86
|
+
const mainResource = resourceManager.createResource(mainContent, {
|
|
87
|
+
id: 'content.md',
|
|
88
|
+
type: 'document',
|
|
89
|
+
contentType: 'text/markdown',
|
|
90
|
+
description: 'Main artwork description'
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
console.log('Created resource:');
|
|
94
|
+
console.log(` ID: ${mainResource.id}`);
|
|
95
|
+
console.log(` Type: ${mainResource.type}`);
|
|
96
|
+
console.log(` Content Type: ${mainResource.contentType}`);
|
|
97
|
+
console.log(` Hash: ${mainResource.hash.substring(0, 16)}...`);
|
|
98
|
+
console.log(` Size: ${mainResource.size} bytes`);
|
|
99
|
+
console.log(` Version: ${mainResource.version}`);
|
|
100
|
+
|
|
101
|
+
// Update the resource (creates version 2)
|
|
102
|
+
const updatedContent = mainContent + '\n\n## Update\n\nAdded additional metadata.';
|
|
103
|
+
const v2Resource = resourceManager.updateResource(mainResource, updatedContent, {
|
|
104
|
+
changes: 'Added update section with additional metadata'
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
console.log('\nUpdated resource (new version):');
|
|
108
|
+
console.log(` Version: ${v2Resource.version}`);
|
|
109
|
+
console.log(` Previous Hash: ${v2Resource.previousVersionHash?.substring(0, 16)}...`);
|
|
110
|
+
console.log(` New Hash: ${v2Resource.hash.substring(0, 16)}...`);
|
|
111
|
+
|
|
112
|
+
// Verify the version chain
|
|
113
|
+
const chainValidation = resourceManager.verifyVersionChain(mainResource.id);
|
|
114
|
+
console.log(`\nVersion chain validation: ${chainValidation.valid ? '✓ Valid' : '✗ Invalid'}`);
|
|
115
|
+
if (chainValidation.warnings.length > 0) {
|
|
116
|
+
console.log(' Warnings:', chainValidation.warnings.join(', '));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Step 2: Create a draft asset (did:peer layer)
|
|
122
|
+
*/
|
|
123
|
+
async function createDraft(sdk: OriginalsSDK): Promise<OriginalsAsset> {
|
|
124
|
+
console.log('\n📝 STEP 2: Creating Draft Asset (did:peer)\n');
|
|
125
|
+
|
|
126
|
+
const content = `
|
|
127
|
+
{
|
|
128
|
+
"title": "Genesis Artwork #001",
|
|
129
|
+
"artist": "Digital Creator",
|
|
130
|
+
"medium": "Generative Algorithm",
|
|
131
|
+
"created": "${new Date().toISOString()}",
|
|
132
|
+
"description": "A unique piece of digital art inscribed forever on Bitcoin"
|
|
133
|
+
}
|
|
134
|
+
`.trim();
|
|
135
|
+
|
|
136
|
+
const resources = [{
|
|
137
|
+
id: 'metadata.json',
|
|
138
|
+
type: 'data',
|
|
139
|
+
content,
|
|
140
|
+
contentType: 'application/json',
|
|
141
|
+
hash: computeHash(content),
|
|
142
|
+
size: content.length,
|
|
143
|
+
}];
|
|
144
|
+
|
|
145
|
+
// Create draft with progress tracking
|
|
146
|
+
const draft = await sdk.lifecycle.createDraft(resources, {
|
|
147
|
+
onProgress: createProgressLogger('Create Draft'),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
console.log('\nDraft created:');
|
|
151
|
+
console.log(` Asset ID: ${draft.id}`);
|
|
152
|
+
console.log(` Current Layer: ${draft.currentLayer}`);
|
|
153
|
+
console.log(` Resources: ${draft.resources.length}`);
|
|
154
|
+
|
|
155
|
+
// Check provenance
|
|
156
|
+
const provenance = draft.getProvenance();
|
|
157
|
+
console.log(` Created At: ${provenance.createdAt}`);
|
|
158
|
+
console.log(` Creator: ${provenance.creator.substring(0, 30)}...`);
|
|
159
|
+
|
|
160
|
+
return draft;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Step 3: Validate migration and estimate costs
|
|
165
|
+
*/
|
|
166
|
+
async function validateAndEstimate(sdk: OriginalsSDK, asset: OriginalsAsset): Promise<void> {
|
|
167
|
+
console.log('\n🔍 STEP 3: Validating Migration & Estimating Costs\n');
|
|
168
|
+
|
|
169
|
+
// Validate migration to did:webvh
|
|
170
|
+
console.log('Validating migration to did:webvh:');
|
|
171
|
+
const webvhValidation = sdk.lifecycle.validateMigration(asset, 'did:webvh');
|
|
172
|
+
printValidation(webvhValidation);
|
|
173
|
+
|
|
174
|
+
// Validate migration to did:btco
|
|
175
|
+
console.log('\nValidating migration to did:btco:');
|
|
176
|
+
const btcoValidation = sdk.lifecycle.validateMigration(asset, 'did:btco');
|
|
177
|
+
printValidation(btcoValidation);
|
|
178
|
+
|
|
179
|
+
// Estimate costs for Bitcoin inscription
|
|
180
|
+
console.log('\nCost estimates for Bitcoin inscription:');
|
|
181
|
+
const feeRates = [1, 5, 10, 25, 50];
|
|
182
|
+
for (const feeRate of feeRates) {
|
|
183
|
+
const estimate = await sdk.lifecycle.estimateCost(asset, 'did:btco', feeRate);
|
|
184
|
+
printCostEstimate(estimate, feeRate);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function printValidation(validation: MigrationValidation): void {
|
|
189
|
+
console.log(` Valid: ${validation.valid ? '✓' : '✗'}`);
|
|
190
|
+
console.log(` Current Layer: ${validation.currentLayer}`);
|
|
191
|
+
console.log(` Target Layer: ${validation.targetLayer}`);
|
|
192
|
+
console.log(' Checks:');
|
|
193
|
+
Object.entries(validation.checks).forEach(([check, passed]) => {
|
|
194
|
+
if (passed !== undefined) {
|
|
195
|
+
console.log(` - ${check}: ${passed ? '✓' : '✗'}`);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
if (validation.errors.length > 0) {
|
|
199
|
+
console.log(' Errors:', validation.errors.join(', '));
|
|
200
|
+
}
|
|
201
|
+
if (validation.warnings.length > 0) {
|
|
202
|
+
console.log(' Warnings:', validation.warnings.join(', '));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function printCostEstimate(estimate: CostEstimate, feeRate: number): void {
|
|
207
|
+
console.log(` ${feeRate} sat/vB:`);
|
|
208
|
+
console.log(` Total: ${estimate.totalSats} sats (~$${(estimate.totalSats * 0.0006).toFixed(4)} @ $60k/BTC)`);
|
|
209
|
+
console.log(` Breakdown: network=${estimate.breakdown.networkFee}, data=${estimate.breakdown.dataCost}, dust=${estimate.breakdown.dustValue}`);
|
|
210
|
+
console.log(` Confidence: ${estimate.confidence}`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Step 4: Publish to web (did:webvh layer)
|
|
215
|
+
*/
|
|
216
|
+
async function publishToWeb(sdk: OriginalsSDK, asset: OriginalsAsset): Promise<OriginalsAsset> {
|
|
217
|
+
console.log('\n🌐 STEP 4: Publishing to Web (did:webvh)\n');
|
|
218
|
+
|
|
219
|
+
// Subscribe to migration events
|
|
220
|
+
const unsubscribe = asset.on('asset:migrated', (event) => {
|
|
221
|
+
const migrationEvent = event as { asset: { fromLayer: string; toLayer: string } };
|
|
222
|
+
console.log(` 🔔 Event: Migrated from ${migrationEvent.asset.fromLayer} to ${migrationEvent.asset.toLayer}`);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
const published = await sdk.lifecycle.publish(
|
|
227
|
+
asset,
|
|
228
|
+
'did:webvh:magby.originals.build:example:artwork',
|
|
229
|
+
{
|
|
230
|
+
onProgress: createProgressLogger('Publish'),
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
console.log('\nPublished to web:');
|
|
235
|
+
console.log(` Asset ID: ${published.id}`);
|
|
236
|
+
console.log(` Current Layer: ${published.currentLayer}`);
|
|
237
|
+
console.log(` Bindings: ${JSON.stringify(published.bindings || {})}`);
|
|
238
|
+
|
|
239
|
+
// Check provenance after migration
|
|
240
|
+
const provenance = published.getProvenance();
|
|
241
|
+
console.log(` Migrations: ${provenance.migrations.length}`);
|
|
242
|
+
|
|
243
|
+
if (provenance.migrations.length > 0) {
|
|
244
|
+
const lastMigration = provenance.migrations[provenance.migrations.length - 1];
|
|
245
|
+
console.log(` Last migration: ${lastMigration.from} → ${lastMigration.to} at ${lastMigration.timestamp}`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Check credentials
|
|
249
|
+
console.log(` Credentials: ${published.credentials.length}`);
|
|
250
|
+
published.credentials.forEach((cred, i) => {
|
|
251
|
+
console.log(` ${i + 1}. Type: ${cred.type.join(', ')}`);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
return published;
|
|
255
|
+
} finally {
|
|
256
|
+
unsubscribe();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Step 5: Inscribe on Bitcoin (did:btco layer)
|
|
262
|
+
*/
|
|
263
|
+
async function inscribeOnBitcoin(sdk: OriginalsSDK, asset: OriginalsAsset): Promise<OriginalsAsset> {
|
|
264
|
+
console.log('\n₿ STEP 5: Inscribing on Bitcoin (did:btco)\n');
|
|
265
|
+
|
|
266
|
+
// Subscribe to migration events
|
|
267
|
+
asset.on('asset:migrated', (_event) => {
|
|
268
|
+
console.log(` 🔔 Event: Inscribed on Bitcoin`);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const inscribed = await sdk.lifecycle.inscribe(asset, {
|
|
272
|
+
feeRate: 10,
|
|
273
|
+
onProgress: createProgressLogger('Inscribe'),
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
console.log('\nInscribed on Bitcoin:');
|
|
277
|
+
console.log(` Asset ID: ${inscribed.id}`);
|
|
278
|
+
console.log(` Current Layer: ${inscribed.currentLayer}`);
|
|
279
|
+
console.log(` Bindings: ${JSON.stringify(inscribed.bindings || {})}`);
|
|
280
|
+
|
|
281
|
+
// Check provenance after inscription
|
|
282
|
+
const provenance = inscribed.getProvenance();
|
|
283
|
+
console.log(` Total migrations: ${provenance.migrations.length}`);
|
|
284
|
+
|
|
285
|
+
const btcoMigration = provenance.migrations.find(m => m.to === 'did:btco');
|
|
286
|
+
if (btcoMigration) {
|
|
287
|
+
console.log('\n Bitcoin inscription details:');
|
|
288
|
+
console.log(` Transaction ID: ${btcoMigration.transactionId || 'N/A'}`);
|
|
289
|
+
console.log(` Inscription ID: ${btcoMigration.inscriptionId || 'N/A'}`);
|
|
290
|
+
console.log(` Satoshi: ${btcoMigration.satoshi || 'N/A'}`);
|
|
291
|
+
console.log(` Fee Rate: ${btcoMigration.feeRate || 'N/A'} sat/vB`);
|
|
292
|
+
console.log(` Commit TX: ${btcoMigration.commitTxId || 'N/A'}`);
|
|
293
|
+
console.log(` Reveal TX: ${btcoMigration.revealTxId || 'N/A'}`);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return inscribed;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Step 6: Transfer ownership
|
|
301
|
+
*/
|
|
302
|
+
async function transferOwnership(sdk: OriginalsSDK, asset: OriginalsAsset): Promise<void> {
|
|
303
|
+
console.log('\n🔄 STEP 6: Transferring Ownership\n');
|
|
304
|
+
|
|
305
|
+
// New owner address (mock address for regtest)
|
|
306
|
+
const newOwner = 'bcrt1qnewowner123456789abcdef';
|
|
307
|
+
|
|
308
|
+
// Subscribe to transfer events
|
|
309
|
+
asset.on('asset:transferred', (event) => {
|
|
310
|
+
const transferEvent = event as { to: string };
|
|
311
|
+
console.log(` 🔔 Event: Ownership transferred to ${transferEvent.to}`);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const tx = await sdk.lifecycle.transfer(asset, newOwner, {
|
|
315
|
+
onProgress: createProgressLogger('Transfer'),
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
console.log('\nOwnership transferred:');
|
|
319
|
+
console.log(` Transaction ID: ${tx.txid}`);
|
|
320
|
+
console.log(` New Owner: ${newOwner}`);
|
|
321
|
+
console.log(` Fee: ${tx.fee} sats`);
|
|
322
|
+
|
|
323
|
+
// Check final provenance
|
|
324
|
+
const provenance = asset.getProvenance();
|
|
325
|
+
console.log(`\n Transfer history: ${provenance.transfers.length} transfer(s)`);
|
|
326
|
+
provenance.transfers.forEach((transfer, i) => {
|
|
327
|
+
console.log(` ${i + 1}. ${transfer.from.substring(0, 20)}... → ${transfer.to.substring(0, 20)}...`);
|
|
328
|
+
console.log(` TX: ${transfer.transactionId}`);
|
|
329
|
+
console.log(` At: ${transfer.timestamp}`);
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Step 7: View complete provenance
|
|
335
|
+
*/
|
|
336
|
+
function viewProvenance(asset: OriginalsAsset): void {
|
|
337
|
+
console.log('\n📜 STEP 7: Complete Provenance Chain\n');
|
|
338
|
+
|
|
339
|
+
const provenance = asset.getProvenance();
|
|
340
|
+
const summary = asset.getProvenanceSummary();
|
|
341
|
+
|
|
342
|
+
console.log('Provenance Summary:');
|
|
343
|
+
console.log(` Created: ${summary.created}`);
|
|
344
|
+
console.log(` Creator: ${summary.creator.substring(0, 40)}...`);
|
|
345
|
+
console.log(` Current Layer: ${summary.currentLayer}`);
|
|
346
|
+
console.log(` Migration Count: ${summary.migrationCount}`);
|
|
347
|
+
console.log(` Transfer Count: ${summary.transferCount}`);
|
|
348
|
+
console.log(` Last Activity: ${summary.lastActivity}`);
|
|
349
|
+
|
|
350
|
+
console.log('\nMigration History:');
|
|
351
|
+
provenance.migrations.forEach((migration, i) => {
|
|
352
|
+
console.log(` ${i + 1}. ${migration.from} → ${migration.to}`);
|
|
353
|
+
console.log(` Timestamp: ${migration.timestamp}`);
|
|
354
|
+
if (migration.transactionId) {
|
|
355
|
+
console.log(` TX: ${migration.transactionId}`);
|
|
356
|
+
}
|
|
357
|
+
if (migration.inscriptionId) {
|
|
358
|
+
console.log(` Inscription: ${migration.inscriptionId}`);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
console.log('\nTransfer History:');
|
|
363
|
+
if (provenance.transfers.length === 0) {
|
|
364
|
+
console.log(' No transfers recorded');
|
|
365
|
+
} else {
|
|
366
|
+
provenance.transfers.forEach((transfer, i) => {
|
|
367
|
+
console.log(` ${i + 1}. ${transfer.timestamp}`);
|
|
368
|
+
console.log(` From: ${transfer.from}`);
|
|
369
|
+
console.log(` To: ${transfer.to}`);
|
|
370
|
+
console.log(` TX: ${transfer.transactionId}`);
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
console.log('\nResource Updates:');
|
|
375
|
+
if (provenance.resourceUpdates.length === 0) {
|
|
376
|
+
console.log(' No resource updates recorded');
|
|
377
|
+
} else {
|
|
378
|
+
provenance.resourceUpdates.forEach((update, i) => {
|
|
379
|
+
console.log(` ${i + 1}. Resource: ${update.resourceId}`);
|
|
380
|
+
console.log(` Version: ${update.fromVersion} → ${update.toVersion}`);
|
|
381
|
+
console.log(` At: ${update.timestamp}`);
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Bonus: Create and publish a typed Original
|
|
388
|
+
*/
|
|
389
|
+
async function typedOriginalFlow(sdk: OriginalsSDK): Promise<void> {
|
|
390
|
+
console.log('\n🎨 BONUS: Typed Media Original Flow\n');
|
|
391
|
+
|
|
392
|
+
const imageDescription = `
|
|
393
|
+
{
|
|
394
|
+
"title": "Digital Sunrise",
|
|
395
|
+
"artist": "AI Collaborator",
|
|
396
|
+
"medium": "Generative AI + Human Curation",
|
|
397
|
+
"dimensions": "2048x2048",
|
|
398
|
+
"colorSpace": "sRGB"
|
|
399
|
+
}
|
|
400
|
+
`.trim();
|
|
401
|
+
|
|
402
|
+
const mediaAsset = await sdk.lifecycle.createTypedOriginal(
|
|
403
|
+
OriginalKind.Media,
|
|
404
|
+
{
|
|
405
|
+
kind: OriginalKind.Media,
|
|
406
|
+
name: 'Digital Sunrise',
|
|
407
|
+
version: '1.0.0',
|
|
408
|
+
description: 'A stunning generative sunrise created with AI',
|
|
409
|
+
resources: [{
|
|
410
|
+
id: 'metadata.json',
|
|
411
|
+
type: 'data',
|
|
412
|
+
content: imageDescription,
|
|
413
|
+
contentType: 'application/json',
|
|
414
|
+
hash: computeHash(imageDescription),
|
|
415
|
+
size: imageDescription.length,
|
|
416
|
+
}],
|
|
417
|
+
tags: ['generative', 'ai', 'sunrise', 'digital-art'],
|
|
418
|
+
author: {
|
|
419
|
+
name: 'AI Collaborator',
|
|
420
|
+
},
|
|
421
|
+
license: 'CC-BY-4.0',
|
|
422
|
+
metadata: {
|
|
423
|
+
mediaType: 'image',
|
|
424
|
+
mimeType: 'image/png',
|
|
425
|
+
dimensions: {
|
|
426
|
+
width: 2048,
|
|
427
|
+
height: 2048,
|
|
428
|
+
aspectRatio: '1:1',
|
|
429
|
+
},
|
|
430
|
+
altText: 'A vibrant digital sunrise over a futuristic cityscape',
|
|
431
|
+
colorSpace: 'sRGB',
|
|
432
|
+
},
|
|
433
|
+
}
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
console.log('Created Media Original:');
|
|
437
|
+
console.log(` ID: ${mediaAsset.id}`);
|
|
438
|
+
console.log(` Kind: ${OriginalKind.Media}`);
|
|
439
|
+
|
|
440
|
+
// Get the manifest
|
|
441
|
+
const manifest = sdk.lifecycle.getManifest(mediaAsset);
|
|
442
|
+
if (manifest && manifest.metadata) {
|
|
443
|
+
const meta = manifest.metadata as { mediaType?: string; dimensions?: { width: number; height: number } };
|
|
444
|
+
console.log(` Name: ${manifest.name}`);
|
|
445
|
+
console.log(` Media Type: ${meta.mediaType}`);
|
|
446
|
+
console.log(` Dimensions: ${meta.dimensions?.width}x${meta.dimensions?.height}`);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Estimate inscription cost for this typed original
|
|
450
|
+
const cost = await sdk.lifecycle.estimateTypedOriginalCost(manifest!, 'did:btco', 10);
|
|
451
|
+
console.log(`\n Estimated inscription cost: ${cost.totalSats} sats`);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Main execution - full lifecycle demonstration
|
|
456
|
+
*/
|
|
457
|
+
async function main(): Promise<void> {
|
|
458
|
+
console.log('╔════════════════════════════════════════════════════════════════╗');
|
|
459
|
+
console.log('║ ORIGINALS SDK - FULL LIFECYCLE FLOW ║');
|
|
460
|
+
console.log('║ ║');
|
|
461
|
+
console.log('║ did:peer → did:webvh → did:btco → Transfer ║');
|
|
462
|
+
console.log('╚════════════════════════════════════════════════════════════════╝');
|
|
463
|
+
|
|
464
|
+
try {
|
|
465
|
+
// Initialize
|
|
466
|
+
const sdk = initializeSDK();
|
|
467
|
+
const resourceManager = new ResourceManager();
|
|
468
|
+
|
|
469
|
+
// Execute lifecycle steps
|
|
470
|
+
createResources(resourceManager);
|
|
471
|
+
|
|
472
|
+
const draft = await createDraft(sdk);
|
|
473
|
+
|
|
474
|
+
await validateAndEstimate(sdk, draft);
|
|
475
|
+
|
|
476
|
+
const published = await publishToWeb(sdk, draft);
|
|
477
|
+
|
|
478
|
+
const inscribed = await inscribeOnBitcoin(sdk, published);
|
|
479
|
+
|
|
480
|
+
await transferOwnership(sdk, inscribed);
|
|
481
|
+
|
|
482
|
+
viewProvenance(inscribed);
|
|
483
|
+
|
|
484
|
+
await typedOriginalFlow(sdk);
|
|
485
|
+
|
|
486
|
+
console.log('\n╔════════════════════════════════════════════════════════════════╗');
|
|
487
|
+
console.log('║ LIFECYCLE COMPLETE ✓ ║');
|
|
488
|
+
console.log('╚════════════════════════════════════════════════════════════════╝\n');
|
|
489
|
+
|
|
490
|
+
} catch (error) {
|
|
491
|
+
console.error('\n❌ Error during lifecycle flow:', error);
|
|
492
|
+
process.exit(1);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Export for use as module
|
|
497
|
+
export {
|
|
498
|
+
main,
|
|
499
|
+
initializeSDK,
|
|
500
|
+
createResources,
|
|
501
|
+
createDraft,
|
|
502
|
+
validateAndEstimate,
|
|
503
|
+
publishToWeb,
|
|
504
|
+
inscribeOnBitcoin,
|
|
505
|
+
transferOwnership,
|
|
506
|
+
viewProvenance,
|
|
507
|
+
typedOriginalFlow,
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
// Run if executed directly
|
|
511
|
+
if (require.main === module) {
|
|
512
|
+
void main();
|
|
513
|
+
}
|
|
514
|
+
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example Runner
|
|
3
|
+
*
|
|
4
|
+
* Run all SDK examples to demonstrate functionality.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* bun run src/examples/run.ts
|
|
8
|
+
* bun run src/examples/run.ts basic
|
|
9
|
+
* bun run src/examples/run.ts module
|
|
10
|
+
* bun run src/examples/run.ts lifecycle
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { basicExample, digitalArtExample } from './basic-usage';
|
|
14
|
+
import { main as createModuleExample } from './create-module-original';
|
|
15
|
+
import { main as fullLifecycleExample } from './full-lifecycle-flow';
|
|
16
|
+
|
|
17
|
+
async function runAll(): Promise<void> {
|
|
18
|
+
console.log('Running all Originals SDK examples...\n');
|
|
19
|
+
|
|
20
|
+
console.log('='.repeat(60));
|
|
21
|
+
console.log('BASIC USAGE EXAMPLES');
|
|
22
|
+
console.log('='.repeat(60));
|
|
23
|
+
await basicExample();
|
|
24
|
+
await digitalArtExample();
|
|
25
|
+
|
|
26
|
+
console.log('\n' + '='.repeat(60));
|
|
27
|
+
console.log('CREATE MODULE ORIGINAL EXAMPLES');
|
|
28
|
+
console.log('='.repeat(60));
|
|
29
|
+
await createModuleExample();
|
|
30
|
+
|
|
31
|
+
console.log('\n' + '='.repeat(60));
|
|
32
|
+
console.log('FULL LIFECYCLE FLOW EXAMPLE');
|
|
33
|
+
console.log('='.repeat(60));
|
|
34
|
+
await fullLifecycleExample();
|
|
35
|
+
|
|
36
|
+
console.log('\n' + '='.repeat(60));
|
|
37
|
+
console.log('ALL EXAMPLES COMPLETED SUCCESSFULLY');
|
|
38
|
+
console.log('='.repeat(60));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function main(): Promise<void> {
|
|
42
|
+
const arg = process.argv[2];
|
|
43
|
+
|
|
44
|
+
switch (arg) {
|
|
45
|
+
case 'basic':
|
|
46
|
+
await basicExample();
|
|
47
|
+
await digitalArtExample();
|
|
48
|
+
break;
|
|
49
|
+
case 'module':
|
|
50
|
+
await createModuleExample();
|
|
51
|
+
break;
|
|
52
|
+
case 'lifecycle':
|
|
53
|
+
await fullLifecycleExample();
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
await runAll();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
main().catch(console.error);
|