@decaf-ts/for-fabric 0.0.13 → 0.0.15
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 +304 -27
- package/dist/for-fabric.cjs +1 -1
- package/dist/for-fabric.cjs.map +1 -1
- package/dist/for-fabric.js +1 -1
- package/dist/for-fabric.js.map +1 -1
- package/lib/client/FabricClientAdapter.cjs +20 -2
- package/lib/client/FabricClientAdapter.js.map +1 -1
- package/lib/client/fabric-hsm.cjs +136 -0
- package/lib/client/fabric-hsm.d.ts +6 -0
- package/lib/client/fabric-hsm.js.map +1 -0
- package/lib/contracts/crud/crud-contract.cjs +2 -1
- package/lib/contracts/crud/crud-contract.js.map +1 -1
- package/lib/esm/client/FabricClientAdapter.js +20 -2
- package/lib/esm/client/FabricClientAdapter.js.map +1 -1
- package/lib/esm/client/fabric-hsm.d.ts +6 -0
- package/lib/esm/client/fabric-hsm.js +130 -0
- package/lib/esm/client/fabric-hsm.js.map +1 -0
- package/lib/esm/contracts/crud/crud-contract.js +2 -1
- package/lib/esm/contracts/crud/crud-contract.js.map +1 -1
- package/lib/esm/shared/types.d.ts +10 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/lib/shared/types.d.ts +10 -0
- package/lib/version.cjs +1 -1
- package/lib/version.d.ts +1 -1
- package/package.json +28 -9
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
## Hyperledger Fabric
|
|
1
|
+

|
|
2
|
+
## Hyperledger Fabric Contracts for DECAF
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
Chaincode-side building blocks for Hyperledger Fabric written in TypeScript. This module provides repositories, CRUD contracts (object and serialized JSON), an ERC20 sample contract, Fabric-backed sequences, event emission, and context-aware logging. It adapts DECAF’s data-access patterns to Fabric’s world-state so you can write smart contracts with the same Repository/Model abstractions used elsewhere in DECAF.
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|

|
|
@@ -29,39 +29,48 @@ Documentation available [here](https://decaf-ts.github.io/for-fabric/)
|
|
|
29
29
|
|
|
30
30
|
### Description
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
This module focuses on the chaincode (contracts) side of @decaf-ts/for-fabric. It adapts DECAF’s Repository/Model/Adapter abstractions to Hyperledger Fabric’s world state and execution context so you can implement smart contracts with familiar patterns and minimal boilerplate.
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
Key ideas:
|
|
35
|
+
- Keep your domain models as annotated classes (using @decaf-ts/decorator-validation).
|
|
36
|
+
- Use a Repository to persist/read/query models through a Fabric-aware Adapter.
|
|
37
|
+
- Compose reusable CRUD contracts and utilities instead of hand-writing stub calls.
|
|
38
|
+
- Emit first-class Fabric events from repository operations.
|
|
39
|
+
- Leverage context-aware logging and typed flags during execution.
|
|
35
40
|
|
|
36
|
-
|
|
41
|
+
Contracts building blocks identified in src/contracts:
|
|
37
42
|
|
|
38
|
-
|
|
43
|
+
1) Core context and types
|
|
44
|
+
- FabricContractContext: Extends the generic Context to expose Fabric-specific properties (stub, clientIdentity, logger) and timestamp resolution from the ledger.
|
|
45
|
+
- FabricContractFlags: Interface extending RepositoryFlags with stub, clientIdentity and logger for contract calls.
|
|
39
46
|
|
|
40
|
-
|
|
47
|
+
2) Logging
|
|
48
|
+
- ContractLogger: MiniLogger-compatible logger bound to the Fabric contract Context. It honors log levels and forwards to the underlying Fabric logger.
|
|
41
49
|
|
|
42
|
-
|
|
50
|
+
3) Adapter and repository
|
|
51
|
+
- FabricContractAdapter: Chaincode-side Adapter that implements CRUD, bulk operations, raw Mango queries, result iteration, model preparation/reversion, composite-key prefixes, and sequence creation. Bridges DECAF abstractions to Fabric (ChaincodeStub, ClientIdentity) and CouchDB-style queries.
|
|
52
|
+
- FabricContractRepository<M>: Repository for models inside chaincode. Supports create, update, createAll, updateAll, read/readAll, raw queries (Mango), select projections, prefix-based bulk ops, and event emission through an ObserverHandler.
|
|
53
|
+
- FabricContractRepositoryObservableHandler: ObserverHandler that emits Fabric events via stub.setEvent using names generated by generateFabricEventName.
|
|
43
54
|
|
|
44
|
-
|
|
55
|
+
4) Sequences
|
|
56
|
+
- FabricContractDBSequence: Fabric-backed implementation of Sequence with current, next and range. Stores values in the world state via FabricContractRepository and supports Number or BigInt sequences with configurable startWith and incrementBy.
|
|
45
57
|
|
|
46
|
-
|
|
58
|
+
5) CRUD contracts
|
|
59
|
+
- FabricCrudContract<M>: Base smart contract exposing CRUD endpoints (create, read, update, delete, createAll, readAll, updateAll, deleteAll, raw, init, healthcheck) for a model type. Uses DeterministicSerializer and the FabricContractAdapter/Repository behind the scenes and provides logFor(ctx).
|
|
60
|
+
- SerializedCrudContract<M>: Same endpoints as FabricCrudContract but takes/returns JSON strings (de)serialized to the model class. This simplifies client interactions and is used in tests.
|
|
47
61
|
|
|
48
|
-
|
|
62
|
+
6) ERC20 sample
|
|
63
|
+
- ERC20Token, ERC20Wallet, Allowance: Sample domain models for an ERC20-like token, wallets and allowances.
|
|
64
|
+
- FabricStatement<M,R>: A CouchDBStatement bridge that runs Mango queries through FabricContractAdapter, handling primary key projection when needed.
|
|
65
|
+
- FabricERC20Contract: A full ERC20 smart contract showcasing repository-based persistence and arithmetic helpers. Implements Initialize, CheckInitialized, TokenName, Symbol, Decimals, TotalSupply, BalanceOf, Transfer, TransferFrom, Approve, Allowance, Mint, Burn, BurnFrom, ClientAccountBalance, ClientAccountID and an internal _transfer helper.
|
|
49
66
|
|
|
50
|
-
|
|
67
|
+
Design notes:
|
|
68
|
+
- Deterministic serialization is used to ensure stable bytes for world-state writes.
|
|
69
|
+
- onCreate/onCreateUpdate hooks from db-decorators are leveraged by the adapter to set primary keys and creator/owner metadata.
|
|
70
|
+
- Mango queries (CouchDB) are used for rich queries via getQueryResultWithPagination.
|
|
71
|
+
- Event emission is opt-in per operation type through FabricContractRepositoryObservableHandler’s supportedEvents list.
|
|
51
72
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
- **Event Handling**: Built-in support for emitting and handling chaincode events, facilitating communication between chaincode and client applications.
|
|
55
|
-
|
|
56
|
-
#### Integration with DECAF
|
|
57
|
-
|
|
58
|
-
This library seamlessly integrates with other DECAF components:
|
|
59
|
-
|
|
60
|
-
- Uses the decorator-validation library for model validation
|
|
61
|
-
- Extends the db-decorators framework for consistent data access
|
|
62
|
-
- Leverages the core DECAF patterns and abstractions
|
|
63
|
-
|
|
64
|
-
By providing a consistent interface across different storage technologies, @decaf-ts/for-fabric enables developers to write applications that can work with both traditional databases and blockchain ledgers with minimal code changes.
|
|
73
|
+
With these components you can build robust chaincode while keeping code concise, testable, and aligned with DECAF’s architecture.
|
|
65
74
|
|
|
66
75
|
|
|
67
76
|
### How to Use
|
|
@@ -347,6 +356,274 @@ function handleEvent(eventName: string, payload: Buffer) {
|
|
|
347
356
|
For more detailed examples and API documentation, refer to the [API Reference](./docs/api/index.html).
|
|
348
357
|
|
|
349
358
|
|
|
359
|
+
## Contracts APIs (Chaincode)
|
|
360
|
+
|
|
361
|
+
The following examples are based on the contracts in for-fabric/src/contracts and reflect the patterns used by the unit/integration tests.
|
|
362
|
+
|
|
363
|
+
### FabricCrudContract<M>
|
|
364
|
+
|
|
365
|
+
Description: Base contract exposing CRUD endpoints for a model class. It uses Repository and DeterministicSerializer under the hood.
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
import { Context, Transaction, Contract } from 'fabric-contract-api';
|
|
369
|
+
import { model, ModelArg, required } from '@decaf-ts/decorator-validation';
|
|
370
|
+
import { BaseModel, pk } from '@decaf-ts/core';
|
|
371
|
+
import { FabricCrudContract } from '@decaf-ts/for-fabric/contracts';
|
|
372
|
+
|
|
373
|
+
@model()
|
|
374
|
+
class Person extends BaseModel {
|
|
375
|
+
@pk({ type: 'Number' })
|
|
376
|
+
id!: number;
|
|
377
|
+
@required() name!: string;
|
|
378
|
+
constructor(arg?: ModelArg<Person>) { super(arg); }
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export class PersonContract extends FabricCrudContract<Person> {
|
|
382
|
+
constructor() {
|
|
383
|
+
super('PersonContract', Person);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
@Transaction(false)
|
|
387
|
+
async ping(ctx: Context): Promise<string> {
|
|
388
|
+
// Uses FabricCrudContract.logFor
|
|
389
|
+
this.logFor(ctx).info('ping');
|
|
390
|
+
return 'pong';
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Usage in tests: see tests/unit/contracts.test.ts pattern where a SerializedCrudContract subclass is exercised; FabricCrudContract is similar but takes/returns objects instead of JSON strings.
|
|
396
|
+
|
|
397
|
+
### SerializedCrudContract<M>
|
|
398
|
+
|
|
399
|
+
Description: Same endpoints as FabricCrudContract but takes and returns JSON strings. Useful for simple clients. Based on tests/unit/contracts.test.ts.
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
import { Context } from 'fabric-contract-api';
|
|
403
|
+
import { model, ModelArg, required } from '@decaf-ts/decorator-validation';
|
|
404
|
+
import { BaseModel, pk } from '@decaf-ts/core';
|
|
405
|
+
import { SerializedCrudContract } from '@decaf-ts/for-fabric/contracts';
|
|
406
|
+
|
|
407
|
+
@model()
|
|
408
|
+
class TestModel extends BaseModel {
|
|
409
|
+
@pk({ type: 'Number' }) id!: number;
|
|
410
|
+
@required() name!: string;
|
|
411
|
+
@required() nif!: string;
|
|
412
|
+
constructor(arg?: ModelArg<TestModel>) { super(arg); }
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export class TestModelContract extends SerializedCrudContract<TestModel> {
|
|
416
|
+
constructor() {
|
|
417
|
+
super('TestModelContract', TestModel);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Example invocation (mirrors unit test usage)
|
|
422
|
+
async function createExample(contract: TestModelContract, ctx: Context) {
|
|
423
|
+
const payload = new TestModel({ name: 'Alice', nif: '123456789' }).serialize();
|
|
424
|
+
const resultJson = await contract.create(ctx, payload);
|
|
425
|
+
const created = new TestModel(JSON.parse(resultJson));
|
|
426
|
+
return created;
|
|
427
|
+
}
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
### FabricContractRepository<M>
|
|
431
|
+
|
|
432
|
+
Description: Chaincode-side repository used inside contract methods to persist and query models.
|
|
433
|
+
|
|
434
|
+
```typescript
|
|
435
|
+
import { Context } from 'fabric-contract-api';
|
|
436
|
+
import { Repo } from '@decaf-ts/core';
|
|
437
|
+
import { model, required, ModelArg } from '@decaf-ts/decorator-validation';
|
|
438
|
+
import { BaseModel, pk } from '@decaf-ts/core';
|
|
439
|
+
import { FabricContractRepository } from '@decaf-ts/for-fabric/contracts';
|
|
440
|
+
|
|
441
|
+
@model()
|
|
442
|
+
class Asset extends BaseModel {
|
|
443
|
+
@pk() id!: string;
|
|
444
|
+
@required() owner!: string;
|
|
445
|
+
constructor(arg?: ModelArg<Asset>) { super(arg); }
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export class AssetContract extends Contract {
|
|
449
|
+
private repo: Repo<Asset, any, any, any, any>;
|
|
450
|
+
constructor() {
|
|
451
|
+
super('AssetContract');
|
|
452
|
+
this.repo = new FabricContractRepository<Asset>(new (require('@decaf-ts/for-fabric').contracts.FabricContractAdapter)(), Asset);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
@Transaction()
|
|
456
|
+
async Create(ctx: Context, id: string, owner: string): Promise<void> {
|
|
457
|
+
const m = new Asset({ id, owner });
|
|
458
|
+
await this.repo.create(m, ctx as any);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
@Transaction(false)
|
|
462
|
+
async Read(ctx: Context, id: string): Promise<Asset> {
|
|
463
|
+
return this.repo.read(id, ctx as any);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
@Transaction(false)
|
|
467
|
+
async QueryByOwner(ctx: Context, owner: string): Promise<Asset[]> {
|
|
468
|
+
return this.repo.raw({ selector: { owner } } as any, true, ctx as any);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### FabricContractDBSequence
|
|
474
|
+
|
|
475
|
+
Description: World-state backed sequences for generating incremental values.
|
|
476
|
+
|
|
477
|
+
```typescript
|
|
478
|
+
import { Context } from 'fabric-contract-api';
|
|
479
|
+
import { FabricContractDBSequence } from '@decaf-ts/for-fabric/contracts';
|
|
480
|
+
import { FabricContractAdapter } from '@decaf-ts/for-fabric/contracts';
|
|
481
|
+
|
|
482
|
+
const adapter = new FabricContractAdapter();
|
|
483
|
+
|
|
484
|
+
export class OrderContract extends Contract {
|
|
485
|
+
private orderSeq = new FabricContractDBSequence({
|
|
486
|
+
name: 'orderSeq',
|
|
487
|
+
type: 'Number',
|
|
488
|
+
startWith: 1,
|
|
489
|
+
incrementBy: 1,
|
|
490
|
+
}, adapter);
|
|
491
|
+
|
|
492
|
+
@Transaction()
|
|
493
|
+
async CreateOrder(ctx: Context): Promise<number> {
|
|
494
|
+
const next = await this.orderSeq.next(ctx as any);
|
|
495
|
+
// use next as order id
|
|
496
|
+
return next as number;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
@Transaction(false)
|
|
500
|
+
async NextRange(ctx: Context, count: number): Promise<number[]> {
|
|
501
|
+
return (await this.orderSeq.range(count, ctx as any)) as number[];
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
### FabricStatement<M,R>
|
|
507
|
+
|
|
508
|
+
Description: Bridge to run Mango queries through the Fabric adapter and get typed models back; used internally by repositories and also directly in advanced cases. See tests/unit/erc20conttract.test.ts mocking CouchDBStatement processing.
|
|
509
|
+
|
|
510
|
+
```typescript
|
|
511
|
+
import { FabricStatement } from '@decaf-ts/for-fabric/contracts';
|
|
512
|
+
import { FabricContractAdapter } from '@decaf-ts/for-fabric/contracts';
|
|
513
|
+
import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
|
|
514
|
+
import { MangoQuery } from '@decaf-ts/for-couchdb';
|
|
515
|
+
import { Model } from '@decaf-ts/decorator-validation';
|
|
516
|
+
|
|
517
|
+
class MyModel extends Model {}
|
|
518
|
+
|
|
519
|
+
const adapter = new FabricContractAdapter();
|
|
520
|
+
|
|
521
|
+
async function query(ctx: FabricContractContext) {
|
|
522
|
+
const stmt = new FabricStatement<MyModel, MyModel[]>(adapter, ctx);
|
|
523
|
+
const models = await stmt.raw<MyModel[]>({ selector: { type: 'MyModel' } } as MangoQuery);
|
|
524
|
+
return models;
|
|
525
|
+
}
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### ContractLogger
|
|
529
|
+
|
|
530
|
+
Description: Context-aware logger bound to Fabric’s Context, honoring log levels.
|
|
531
|
+
|
|
532
|
+
```typescript
|
|
533
|
+
import { Context, Transaction } from 'fabric-contract-api';
|
|
534
|
+
import { Contract } from 'fabric-contract-api';
|
|
535
|
+
import { ContractLogger } from '@decaf-ts/for-fabric/contracts';
|
|
536
|
+
|
|
537
|
+
export class LoggableContract extends Contract {
|
|
538
|
+
@Transaction()
|
|
539
|
+
async DoWork(ctx: Context): Promise<void> {
|
|
540
|
+
const log = new ContractLogger('LoggableContract', { level: 'info' }, ctx as any);
|
|
541
|
+
log.info('Starting work');
|
|
542
|
+
// ... work ...
|
|
543
|
+
log.debug('Finished');
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
### FabricContractRepositoryObservableHandler
|
|
549
|
+
|
|
550
|
+
Description: Emits Fabric events for repository operations. You can also use it directly to emit a custom event.
|
|
551
|
+
|
|
552
|
+
```typescript
|
|
553
|
+
import { FabricContractRepositoryObservableHandler } from '@decaf-ts/for-fabric/contracts';
|
|
554
|
+
import { OperationKeys } from '@decaf-ts/db-decorators';
|
|
555
|
+
import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
|
|
556
|
+
import { MiniLogger } from '@decaf-ts/logging';
|
|
557
|
+
|
|
558
|
+
async function emitExample(ctx: FabricContractContext) {
|
|
559
|
+
const handler = new FabricContractRepositoryObservableHandler();
|
|
560
|
+
const log = new MiniLogger('obs');
|
|
561
|
+
await handler.updateObservers(log as any, 'assets', OperationKeys.CREATE, 'asset1', ctx);
|
|
562
|
+
}
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
### FabricContractContext
|
|
566
|
+
|
|
567
|
+
Description: Access Fabric-specific context inside contracts.
|
|
568
|
+
|
|
569
|
+
```typescript
|
|
570
|
+
import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
|
|
571
|
+
|
|
572
|
+
function readContext(ctx: FabricContractContext) {
|
|
573
|
+
const ts = ctx.timestamp; // Date from stub.getDateTimestamp()
|
|
574
|
+
const id = ctx.identity.getID();
|
|
575
|
+
ctx.logger.info(`Tx by ${id} at ${ts.toISOString()}`);
|
|
576
|
+
}
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
### FabricERC20Contract (sample)
|
|
580
|
+
|
|
581
|
+
Description: Full ERC20 implementation used in tests (see tests/unit/erc20conttract.test.ts).
|
|
582
|
+
|
|
583
|
+
```typescript
|
|
584
|
+
import { FabricERC20Contract } from '@decaf-ts/for-fabric/contracts';
|
|
585
|
+
import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
|
|
586
|
+
|
|
587
|
+
const contract = new FabricERC20Contract('TestToken');
|
|
588
|
+
|
|
589
|
+
async function initAndRead(ctx: FabricContractContext) {
|
|
590
|
+
const created = await contract.Initialize(ctx, 'TestToken', 'TT', 18);
|
|
591
|
+
if (created) {
|
|
592
|
+
const name = await contract.TokenName(ctx);
|
|
593
|
+
const decimals = await contract.Decimals(ctx);
|
|
594
|
+
return { name, decimals };
|
|
595
|
+
}
|
|
596
|
+
throw new Error('Init failed');
|
|
597
|
+
}
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
### Notes on tests as examples
|
|
601
|
+
|
|
602
|
+
- tests/unit/contracts.test.ts shows creating a SerializedCrudContract and calling create(ctx, jsonPayload) with a mocked Fabric Context.
|
|
603
|
+
- tests/unit/erc20conttract.test.ts demonstrates initializing the ERC20 contract and reading TokenName.
|
|
604
|
+
- tests/integration/Serialized-Contract.test.ts shows end-to-end JSON-based CRUD flows via the serialized contract, including create, read, update and rich queries.
|
|
605
|
+
|
|
606
|
+
These patterns are mirrored in the examples above to ensure correctness and consistency with the repository’s test suite.
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
## Coding Principles
|
|
610
|
+
|
|
611
|
+
- group similar functionality in folders (analog to namespaces but without any namespace declaration)
|
|
612
|
+
- one class per file;
|
|
613
|
+
- one interface per file (unless interface is just used as a type);
|
|
614
|
+
- group types as other interfaces in a types.ts file per folder;
|
|
615
|
+
- group constants or enums in a constants.ts file per folder;
|
|
616
|
+
- group decorators in a decorators.ts file per folder;
|
|
617
|
+
- always import from the specific file, never from a folder or index file (exceptions for dependencies on other packages);
|
|
618
|
+
- prefer the usage of established design patters where applicable:
|
|
619
|
+
- Singleton (can be an anti-pattern. use with care);
|
|
620
|
+
- factory;
|
|
621
|
+
- observer;
|
|
622
|
+
- strategy;
|
|
623
|
+
- builder;
|
|
624
|
+
- etc;
|
|
625
|
+
|
|
626
|
+
|
|
350
627
|
### Related
|
|
351
628
|
|
|
352
629
|
[](https://github.com/decaf-ts/ts-workspace)
|
|
@@ -385,4 +662,4 @@ So if you can, if this project in any way. either by learning something or simpl
|
|
|
385
662
|
|
|
386
663
|
This project is released under the [MIT License](./LICENSE.md).
|
|
387
664
|
|
|
388
|
-
By developers, for developers...
|
|
665
|
+
By developers, for developers...
|
package/dist/for-fabric.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e,t;e=this,t=function(e,t,r,a,i,n,o,s,c,l){"use strict";class d extends a.Context{constructor(){super()}get stub(){return this.get("stub")}get timestamp(){return this.stub.getDateTimestamp()}get identity(){return this.get("clientIdentity")}get logger(){return this.get("logger")}}class p extends o.ObserverHandler{constructor(e=[a.OperationKeys.CREATE,a.OperationKeys.UPDATE,a.OperationKeys.DELETE,a.BulkCrudOperationKeys.CREATE_ALL,a.BulkCrudOperationKeys.UPDATE_ALL,a.BulkCrudOperationKeys.DELETE_ALL]){super(),this.supportedEvents=e}async updateObservers(e,t,r,a,i,n,o){const{stub:s}=i;if(-1!==this.supportedEvents.indexOf(r)){e.debug(`Emitting ${r} event`);const i=((e,t,r)=>{const a=[e,t];return r&&a.push(r),a.join("_")})(t,r,n);s.setEvent(i,Buffer.from(JSON.stringify({id:a})))}else s.setEvent(r,Buffer.from(JSON.stringify(o)))}}var u,h;(e=>{e.PRIVATE="private",e.FABRIC="fabric.",e.OWNEDBY="owned-by"})(u||(u={})),(e=>{e.X509="X.509"})(h||(h={}));const g="hlf-fabric";function y(){return function(e,t,r){const i=r.value;return r.value=async function(...e){const r=e[0],n=r.clientIdentity.getID(),s=await this.tokenRepository.selectWithContext(void 0,r),c=await s.execute();if(0==c.length)throw new a.NotFoundError("No tokens avaialble");if(c.length>1)throw new a.NotFoundError("To many token available : "+c.length);if(c[0].owner!=n)throw new o.AuthorizationError(`User not authorized to run ${t} on the token`);return await i.apply(this,e)},r}}function f(e){return r.Model.key(u.FABRIC+e)}function m(e){if(!e)throw Error("Collection name is required");const t=f(u.PRIVATE);return(i,n)=>{const o=n||void 0,s=Reflect.getMetadata(t,i[r.ModelKeys.ANCHOR]||i,o),c=s?.collections||[];r.propMetadata(f(u.PRIVATE),{...!n&&{collections:c?[...new Set([...c,e])]:[e]},isPrivate:!n})(n?i.constructor:i[r.ModelKeys.ANCHOR]||i),n&&(r.propMetadata(f(u.PRIVATE),{collections:c?[...new Set([...c,e])]:[e]})(i,n),a.transient()(i,n))}}const w=/private\s+data\s+matching\s+public\s+hash\s+version\s+is\s+not\s+available/i;function _(e){let t=Reflect.getMetadata(f(u.PRIVATE),e);return t=t||Reflect.getMetadata(f(u.PRIVATE),e.constructor),t}function b(e){const t=_(e);return!(!t||void 0===t.isPrivate)&&t.isPrivate}function v(e){if(!(e=>!!_(e))(e))return{model:e};const t=a.getAllPropertyDecoratorsRecursive(e,void 0,f(u.PRIVATE)),i=b(e),n=_(e);let o={model:e,private:void 0};if(o=i?Object.keys(e).reduce((t,r)=>{const i=n.collections;t.private=t.private||{};for(const n of i)try{t.private[n]=t.private[n]||{},t.private[n][r]=e[r]}catch(e){throw new a.SerializationError(`Failed to serialize private property ${r}: ${e}`)}return t},{model:{}}):Object.entries(t).reduce((t,[r,i])=>{const n=i.find(e=>""===e.key);if(n){const i=n.props.collections;t.private=t.private||{};for(const n of i)try{t.private[n]=t.private[n]||{},t.private[n][r]=e[r]}catch(e){throw new a.SerializationError(`Failed to serialize private property ${r}: ${e}`)}}else t.model=t.model||{},t.model[r]=e[r];return t},{}),o.model=o.model||{},o.model=r.Model.build(o.model,e.constructor.name),o.private){const t=Object.keys(o.private);for(const a of t)o.private[a]=r.Model.build(o.private[a],e.constructor.name)}return o}class S extends a.InternalError{constructor(e){super(e,S.name)}}class C extends a.InternalError{constructor(e){super(e,C.name)}}class A extends a.InternalError{constructor(e){super(e,A.name)}}class E extends a.InternalError{constructor(e){super(e,E.name,500)}}class O extends a.BaseError{constructor(e="private data matching public hash version is not available ..."){super(O.name,e,403)}}class k extends a.BaseError{constructor(e){super(k.name,e,409)}}let x=class extends o.BaseModel{constructor(e){super(e)}};c.__decorate([o.pk(),c.__metadata("design:type",String)],x.prototype,"id",void 0),c.__decorate([r.required(),o.index(),c.__metadata("design:type",Object)],x.prototype,"current",void 0),x=c.__decorate([o.table(t.CouchDBKeys.SEQUENCE),r.model(),c.__metadata("design:paramtypes",[Object])],x);class T extends o.Sequence{constructor(e,t,r){super(e);for(const e of r||[])m(e)(x);this.repo=o.Repository.forModel(x,t.alias)}async current(e){if(!e)throw new E("Context is required");const{name:t,startWith:r}=this.options;try{const r=await this.repo.read(t,e);return this.parse(r.current)}catch(e){if(e instanceof a.NotFoundError){if(void 0===r)throw new a.InternalError("Starting value is not defined for a non existing sequence");try{return this.parse(r)}catch(e){throw new a.InternalError(`Failed to parse initial value for sequence ${r}: ${e}`)}}throw new a.InternalError(`Failed to retrieve current value for sequence ${t}: ${e}`)}}parse(e){return o.Sequence.parseValue(this.options.type,e)}async increment(e,t,r){if(!r)throw new E("Context is required");const{type:i,incrementBy:n,name:o}=this.options;let s;const c=t||n;if(c%n!==0)throw new a.InternalError("Value to increment does not consider the incrementBy setting: "+n);switch(i){case"Number":s=this.parse(e)+c;break;case"BigInt":s=this.parse(e)+BigInt(c);break;default:throw new a.InternalError("Should never happen")}let l;try{l=await this.repo.update(new x({id:o,current:s}),r)}catch(e){if(!(e instanceof a.NotFoundError))throw e;l=await this.repo.create(new x({id:o,current:s}),r)}return l.current}async next(e){if(!e)throw new E("Context is required");const t=await this.current(e);return this.increment(t,void 0,e)}async range(e,t){if(!t)throw new E("Context is required");const r=await this.current(t),i=this.parse(this.options.incrementBy),n=await this.increment(r,this.parse(e)*i,t),o=[];for(let t=1;e>=t;t++)o.push(r+i*this.parse(t));if(o[o.length-1]!==n)throw new a.InternalError("Miscalculation of range");return o}}class R extends o.Repository{constructor(e,t,r){super(e,t),this.trackedEvents=r}logFor(e){return n.Logging.for(R,{},e)}async create(e,...t){const r=t[t.length-1],a=this.logFor(r).for(this.create);a.info("Preparing model: "+JSON.stringify(e));let i,{record:n,id:o,transient:s}=this.adapter.prepare(e,this.pk,this.tableName,...t);return a.info("Creating model: "+JSON.stringify(e)),n=await this.adapter.create(this.tableName,o,n,...t),t.length&&(i=t[t.length-1]),a.info("Reverting model: "+JSON.stringify(e)),this.adapter.revert(n,this.class,this.pk,o,i&&i.get("rebuildWithTransient")?s:void 0)}async read(e,...t){const r=await this.adapter.read(this.tableName,e,new this.class,...t);return this.adapter.revert(r,this.class,this.pk,e)}async delete(e,...t){const r=await this.adapter.delete(this.tableName,e,new this.class,...t);return this.adapter.revert(r,this.class,this.pk,e)}async update(e,...t){const r=t[t.length-1],a=this.logFor(r).for(this.update);a.info("Preparing model: "+JSON.stringify(e));let i,{record:n,id:o,transient:s}=this.adapter.prepare(e,this.pk,this.tableName,...t);return a.info("Updating model: "+JSON.stringify(e)),n=await this.adapter.update(this.tableName,o,n,...t),t.length&&(i=t[t.length-1]),a.info("Reverting model: "+JSON.stringify(e)),this.adapter.revert(n,this.class,this.pk,o,i&&i.get("rebuildWithTransient")?s:void 0)}ObserverHandler(){return new p}async createAll(e,...t){if(!e.length)return e;const r=e.map(e=>this.adapter.prepare(e,this.pk,this.tableName,...t)),a=r.map(e=>e.id);let i=r.map(e=>e.record);const n=r.map(e=>e.transient).filter(e=>!!e);let o;return t.length&&(o=t[t.length-1]),i=await this.adapter.createAll(this.tableName,a,i,...t),i.map((e,t)=>this.adapter.revert(e,this.class,this.pk,a[t],o&&o.get("rebuildWithTransient")?n:void 0))}async updateAll(e,...t){if(!e.length)return e;const r=e.map(e=>this.adapter.prepare(e,this.pk,this.tableName,...t)),a=r.map(e=>e.transient).filter(e=>!!e);let i;return t.length&&(i=t[t.length-1]),(await this.adapter.updateAll(this.tableName,r.map(e=>e.id),r.map(e=>e.record),...t)).map((e,t)=>this.adapter.revert(e,this.class,this.pk,r[t].id,i&&i.get("rebuildWithTransient")?a:void 0))}async raw(e,t,...r){const i=r.pop(),n=await a.Context.args("QUERY",this.class,[i],this.adapter,{});return this.adapter.raw(e,t,new this.class,...n.args)}async updateObservers(e,t,r,a,...i){if(!this.trackedEvents||-1!==this.trackedEvents.indexOf(t))return await super.updateObservers(e,t,r,a,...i)}async selectWithContext(e,t){let r;return t instanceof d?r.context=t:r=await a.Context.args(a.OperationKeys.CREATE,this.class,[t],this.adapter,this._overrides||{}),e?this.adapter.Statement(r.context).select(e).from(this.class):this.adapter.Statement(r.context).select().from(this.class)}async createAllPrefix(e,...t){const r=t[t.length-1],i=await a.Context.args(a.OperationKeys.CREATE,this.class,t,this.adapter,this._overrides||{});if(!e.length)return[e,...i.args];const n=o.Repository.getSequenceOptions(e[0]);let s=[];n.type?(n.name||(n.name=T.pk(e[0])),s=await(await this.adapter.Sequence(n)).range(e.length,r)):s=e.map((e,t)=>{if(void 0===e[this.pk])throw new a.InternalError("Primary key is not defined for model in position "+t);return e[this.pk]}),e=await Promise.all(e.map(async(e,t)=>(e=new this.class(e),n.type&&(e[this.pk]=s[t]),await a.enforceDBDecorators(this,i.context,e,a.OperationKeys.CREATE,a.OperationKeys.ON),e)));const c=i.context.get("ignoredValidationProperties")||[],l=(await Promise.all(e.map(e=>Promise.resolve(e.hasErrors(...c))))).reduce((e,t,r)=>(t&&(e="string"==typeof e?e+`\n - ${r}: ${t.toString()}`:` - ${r}: ${t.toString()}`),e),void 0);if(l)throw new a.ValidationError(l);return[e,...i.args]}async createPrefix(e,...t){const r=await super.createPrefix(e,...t),i=r[0][this.pk];try{if(await this.read(i+"",...t))throw new a.ConflictError(`Conflict detected while creating model with id: ${i} already exists`)}catch(e){if(404!==e.code)throw e;this.logFor(t[t.length-1]).info(`Record entry with pk ${i} does not exist, creating it now...`)}return r}}class F extends t.CouchDBStatement{constructor(e,t){super(e),this.ctx=t}async raw(e){const t=await this.adapter.raw(e,!0,this.ctx),r=a.findPrimaryKey(new this.fromSelector),i=r.id,n=r.props.type;return this.selectSelector?t:t.map(e=>this.processRecord(e,i,n))}build(){const e={};e[t.CouchDBKeys.TABLE]={},e[t.CouchDBKeys.TABLE]=o.Repository.table(this.fromSelector);const r={selector:e};if(this.selectSelector&&(r.fields=this.selectSelector),this.whereCondition){const e=this.parseCondition(o.Condition.and(this.whereCondition,o.Condition.attribute(t.CouchDBKeys.TABLE).eq(r.selector[t.CouchDBKeys.TABLE]))).selector,a=Object.keys(e);if(1===a.length&&-1!==Object.values(t.CouchDBGroupOperator).indexOf(a[0]))switch(a[0]){case t.CouchDBGroupOperator.AND:e[t.CouchDBGroupOperator.AND]=[...Object.values(e[t.CouchDBGroupOperator.AND]).reduce((e,r)=>{const a=Object.keys(r);if(1!==a.length)throw Error("Too many keys in query selector. should be one");const i=a[0];return i===t.CouchDBGroupOperator.AND?e.push(...r[i]):e.push(r),e},[])],r.selector=e;break;case t.CouchDBGroupOperator.OR:{const a={};a[t.CouchDBGroupOperator.AND]=[e,...Object.entries(r.selector).map(([e,t])=>{const r={};return r[e]=t,r})],r.selector=a;break}default:throw Error("This should be impossible")}else Object.entries(e).forEach(([e,t])=>{r.selector[e],r.selector[e]=t})}if(this.orderBySelector){r.sort=r.sort||[],r.selector=r.selector||{};const[e,a]=this.orderBySelector,i={};i[e]=a,r.sort.push(i),r.selector[e]||(r.selector[e]={},r.selector[e][t.CouchDBOperator.BIGGER]=null)}return this.limitSelector&&(r.limit=this.limitSelector),this.offsetSelector&&(r.skip=this.offsetSelector),r}}class P extends r.JSONSerializer{constructor(){super()}deserialize(e,t){return JSON.parse(e)}serialize(e){return require("json-stringify-deterministic")(require("sort-keys-recursive")(this.preSerialize(e)))}preSerialize(e){return Object.assign({},e)}}async function N(e,t,i,n){const s=n[i];if(!s)return;if("object"!=typeof s){const t=o.repositoryFromTypeMetadata(n,i,this.adapter.alias),r=await t.read(s,e);return await o.cacheModelForPopulate(e,n,i,s,r),void(n[i]=s)}t.class="string"==typeof t.class?t.class:t.class().name;const c=r.Model.get(t.class);if(!c)throw new a.InternalError("Could not find model "+t.class);const l=o.Repository.forModel(c,this.adapter.alias),d=await l.create(s,e),p=a.findPrimaryKey(d).id;await o.cacheModelForPopulate(e,n,i,d[p],d),n[i]=d[p]}async function I(e,t,r,i){const n=i[r];if(!n)return;if(t.cascade.update!==o.Cascade.CASCADE)return;if("object"!=typeof n){const t=o.repositoryFromTypeMetadata(i,r,this.adapter.alias),a=await t.read(n,e);return await o.cacheModelForPopulate(e,i,r,n,a),void(i[r]=n)}const s=await o.createOrUpdate(i[r],e,this.adapter.alias),c=a.findPrimaryKey(s).id;await o.cacheModelForPopulate(e,i,r,s[c],s),i[r]=s[c]}async function z(e,t,a,i){const n=i[a];if(!n)return;if(t.cascade.update!==o.Cascade.CASCADE)return;const s=o.repositoryFromTypeMetadata(i,a,this.adapter.alias);let c;c=n instanceof r.Model?await s.delete(i[a][s.pk],e):await s.delete(i[a],e),await o.cacheModelForPopulate(e,i,a,c[s.pk],c)}async function D(e,t,r,i){const n=i[r];if(!n||!n.length)return;const s=typeof n[0];if(!n.every(e=>typeof e===s))throw new a.InternalError(`Invalid operation. All elements of property ${r} must match the same type.`);const c=new Set([...n]);if("object"!==s){const t=o.repositoryFromTypeMetadata(i,r,this.adapter.alias);for(const a of c){const n=await t.read(a,e);await o.cacheModelForPopulate(e,i,r,a,n)}return void(i[r]=[...c])}const l=a.findPrimaryKey(n[0]).id,d=new Set;for(const t of n){const a=await o.createOrUpdate(t,e,this.adapter.alias);await o.cacheModelForPopulate(e,i,r,a[l],a),d.add(a[l])}i[r]=[...d]}async function $(e,t,r,i){if(t.cascade.delete!==o.Cascade.CASCADE)return;const n=i[r];if(!n||!n.length)return;const s=typeof n[0];if(!n.every(e=>typeof e===s))throw new a.InternalError(`Invalid operation. All elements of property ${r} must match the same type.`);const c="object"===s,l=c?o.Repository.forModel(n[0],this.adapter.alias):o.repositoryFromTypeMetadata(i,r,this.adapter.alias),d=new Set([...c?n.map(e=>e[l.pk]):n]);for(const t of d.values()){const a=await l.delete(t,e);await o.cacheModelForPopulate(e,i,r,t,a)}i[r]=[...d]}async function B(e,t,r,i){if(!t.populate)return;const n=i[r],s=Array.isArray(n);if(void 0===n||s&&0===n.length)return;const c=await(async(t,r,i,n,s)=>{let c,l;const d=[];for(const p of n){c=o.getPopulateKey(r.constructor.name,i,p);try{l=await t.get(c)}catch(t){const n=o.repositoryFromTypeMetadata(r,i,s);if(!n)throw new a.InternalError("Could not find repo");l=await n.read(p,e)}d.push(l)}return d})(e,i,r,s?n:[n],this.adapter.alias);i[r]=s?c:c[0]}async function M(e,t,r,a){try{const t=e.get("clientIdentity");a[r]=t.getID()}catch(e){throw new o.UnsupportedError("No User found in context. Please provide a user in the context")}}async function q(e,t,r,i){if(!t.type||i[r])return;let n;t.name||(t.name=o.sequenceNameForModel(i,"pk"));try{n=await this.adapter.Sequence(t)}catch(e){throw new a.InternalError(`Failed to instantiate Sequence ${t.name}: ${e}`)}const s=await n.next(e);Object.defineProperty(i,r,{enumerable:!0,writable:!1,configurable:!0,value:s})}class j extends t.CouchDBAdapter{getClient(){throw new o.UnsupportedError("Client is not supported in Fabric contracts")}static{this.textDecoder=new TextDecoder("utf8")}static{this.serializer=new P}logFor(e){return n.Logging.for(j,{},e)}repository(){return R}constructor(e,t){super(e,g,t),this.Context=d}for(e,...t){return super.for(e,...t)}async create(e,t,r,...a){const{stub:i,logger:n}=a.pop(),o=n.for(this.create);try{o.info(`adding entry to ${e} table with pk ${t}`),r=await this.putState(i,t.toString(),r)}catch(e){throw this.parseError(e)}return r}async read(e,t,...r){const{stub:i,logger:n}=r.pop(),o=n.for(this.read);let s;try{const r=await this.readState(i,e,t.toString());if(1>r.length)throw o.debug(`No record found for id ${t} in ${e} table`),new a.NotFoundError(`No record found for id ${t} in ${e} table`);2>r.length?(o.debug(`No record found for id ${t} in ${e} table`),s=r.pop()):s=this.mergeModels(r)}catch(e){throw this.parseError(e)}return s}async update(e,t,r,...a){const{stub:i,logger:n}=a.pop(),o=n.for(this.update);try{o.info(`updating entry to ${e} table with pk ${t}`),r=await this.putState(i,t.toString(),r)}catch(e){throw this.parseError(e)}return r}async delete(e,t,...r){const a=r.pop(),{stub:i,logger:n}=a,o=n.for(this.delete);let s;r.push(a);try{s=await this.read(e,t,...r),o.verbose(`deleting entry with pk ${t} from ${e} table`),this.deleteState(i,e,t.toString())}catch(e){throw this.parseError(e)}return s}async deleteState(e,t,r,...a){const i=e.createCompositeKey(t,[r+""]);await e.deleteState(i)}async putState(e,t,r,...i){let n;try{n=Buffer.from(j.serializer.serialize(r))}catch(e){throw new a.SerializationError(`Failed to serialize record with id ${t}: ${e}`)}return await e.putState(t.toString(),n),r}async readState(e,t,r,...i){const n=e.createCompositeKey(t,[r+""]),o=[];let s=await e.getState(n);if(""===s.toString())throw new a.NotFoundError(`Record with id ${r} not found`);try{s=j.serializer.deserialize(s.toString())}catch(e){throw new a.SerializationError("Failed to parse record: "+e)}return o.push(s),o}async queryResult(e,t,...r){return await e.getQueryResult(JSON.stringify(t))}async queryResultPaginated(e,t,r=250,a,...i){return await e.getQueryResultWithPagination(JSON.stringify(t),r,a?.toString())}mergeModels(e){const t=e=>Object.entries(e).reduce((e,[t,r])=>(void 0!==r&&(e[t]=r),e),{});let r=e.pop();for(const a of e)r=Object.assign({},t(r),t(a));return r}decode(e){return j.textDecoder.decode(e)}async flags(e,t,r,a,...i){return Object.assign(await super.flags(e,t,r,...i),{stub:a.stub,identity:a.clientIdentity??a.identity,logger:a.logger??this.logFor(a)})}index(e){return Promise.resolve(void 0)}async resultIterator(e,t,r=!1){const a=[];let i=await t.next();for(;!i.done;){if(i.value&&i.value.value.toString()){let t={};if(e.debug(i.value.value.toString("utf8")),r){t.TxId=i.value.txId,t.Timestamp=i.value.timestamp;try{t.Value=JSON.parse(i.value.value.toString("utf8"))}catch(r){e.error(r),t.Value=i.value.value.toString("utf8")}}else try{t=JSON.parse(i.value.value.toString("utf8"))}catch(r){e.error(r),t=i.value.value.toString("utf8")}a.push(t)}i=await t.next()}return e.debug(`Closing iterator after ${a.length} results`),t.close(),a}async raw(e,t,...r){const{stub:a,logger:i}=r.pop(),n=i.for(this.raw),{skip:o,limit:s}=e;let c;s||o?(delete e.limit,delete e.skip,n.debug(`Retrieving paginated iterator: limit: ${s}/ skip: ${o}`),c=(await this.queryResultPaginated(a,e,s||250,o?.toString())).iterator):(n.debug("Retrieving iterator"),c=await this.queryResult(a,e)),n.debug("Iterator acquired");const l=await this.resultIterator(n,c);return n.debug("returning {0} results",""+(Array.isArray(l)?l.length:1)),l}Statement(e){if(!e)throw new E("Context is required");return new F(this,e)}async Sequence(e){return new T(e,this)}async createAll(e,t,r,...i){if(t.length!==r.length)throw new a.InternalError("Ids and models must have the same length");const{logger:n}=i[i.length-1],o=n.for(this.createAll);return o.info(`Creating ${t.length} entries ${e} table`),o.debug("pks: "+t),Promise.all(t.map(async(t,a)=>this.create(e,t,r[a],...i)))}async updateAll(e,t,r,...i){if(t.length!==r.length)throw new a.InternalError("Ids and models must have the same length");const{logger:n}=i[i.length-1],o=n.for(this.createAll);return o.info(`Updating ${t.length} entries ${e} table`),o.debug("pks: "+t),Promise.all(t.map(async(t,a)=>this.update(e,t,r[a],...i)))}prepare(e,t,...r){const{stub:i,logger:n}=r.pop(),s=r.shift(),c=n.for(this.prepare),l=a.modelToTransient(e),d=Object.entries(l.model).reduce((t,[r,i])=>{if(void 0===i)return t;const n=o.Repository.column(e,r);if(this.isReserved(n))throw new a.InternalError(`Property name ${n} is reserved`);return t[n]=i,t},{});return e[o.PersistenceKeys.METADATA]&&(c.silly("Passing along persistence metadata for "+e[o.PersistenceKeys.METADATA]),Object.defineProperty(d,o.PersistenceKeys.METADATA,{enumerable:!1,writable:!1,configurable:!0,value:e[o.PersistenceKeys.METADATA]})),c.info(`Preparing record for ${s} table with pk ${e[t]}`),{record:d,id:i.createCompositeKey(s,[e[t]+""]),transient:l.transient}}revert(e,t,i,n,s){const c=this.log.for(this.revert),l={};l[i]=n;const d="string"==typeof t?r.Model.build(l,t):new t(l);c.silly(`Rebuilding model ${d.constructor.name} id ${n}`);const p=e[o.PersistenceKeys.METADATA],u=Object.keys(d).reduce((t,r)=>(t[r]=e[o.Repository.column(t,r)],t),d);return s&&(c.verbose("re-adding transient properties: "+Object.keys(s).join(", ")),Object.entries(s).forEach(([e,t])=>{if(e in u&&void 0!==u[e])throw new a.InternalError(`Transient property ${e} already exists on model ${d.constructor.name}. should be impossible`);u[e]=t})),p&&(c.silly(`Passing along ${this.flavour} persistence metadata for ${d.constructor.name} id ${n}: ${p}`),Object.defineProperty(u,o.PersistenceKeys.METADATA,{enumerable:!1,configurable:!1,writable:!1,value:p})),u}createPrefix(e,r,a,...i){const n=i.pop(),o={};return o[t.CouchDBKeys.TABLE]=e,Object.assign(o,a),[e,r,o,n]}updatePrefix(e,r,a,...i){const n=i.pop(),o={};return o[t.CouchDBKeys.TABLE]=e,Object.assign(o,a),[e,r,o,n]}createAllPrefix(e,r,i,...n){if(r.length!==i.length)throw new a.InternalError("Ids and models must have the same length");const o=n.pop(),s=r.map((r,a)=>{const n={};return n[t.CouchDBKeys.TABLE]=e,Object.assign(n,i[a]),n});return[e,r,s,o]}updateAllPrefix(e,r,i,...n){if(r.length!==i.length)throw new a.InternalError("Ids and models must have the same length");const o=n.pop(),s=r.map((r,a)=>{const n={};return n[t.CouchDBKeys.TABLE]=e,Object.assign(n,i[a]),n});return[e,r,s,o]}static decoration(){super.decoration();const e=o.Repository.key(o.PersistenceKeys.CREATED_BY),t=o.Repository.key(o.PersistenceKeys.UPDATED_BY);r.Decoration.flavouredAs(g).for(e).define(a.onCreate(M),r.propMetadata(e,{})).apply(),r.Decoration.flavouredAs(g).for(t).define(a.onCreateUpdate(M),r.propMetadata(t,{})).apply();const n=o.Repository.key(a.DBKeys.ID);r.Decoration.flavouredAs(g).for(n).define(o.index([o.OrderDirection.ASC,o.OrderDirection.DSC]),r.required(),a.readonly(),r.propMetadata(n,o.NumericSequence),a.onCreate(q,o.NumericSequence)).apply();const c=o.Adapter.key(o.PersistenceKeys.COLUMN);r.Decoration.flavouredAs(g).for(c).extend(i.Property()).apply();const l=o.Adapter.key(o.PersistenceKeys.TABLE);r.Decoration.flavouredAs(g).for(l).extend(e=>i.Object()(e)).apply();const d=o.Repository.key(o.PersistenceKeys.ONE_TO_ONE);r.Decoration.flavouredAs(g).for(d).define({decorator:(e,t,i,n,c)=>{const l={class:e.name?e.name:e,cascade:t,populate:i};return n&&(l.joinTable=n),c&&(l.name=c),s.apply(r.prop(o.PersistenceKeys.RELATIONS),r.type([e.name?e.name:e,String.name,Number.name,BigInt.name]),a.onCreate(N,l),a.onUpdate(I,l),a.onDelete(z,l),a.afterAny(B,l),r.propMetadata(d,l))}}).apply();const p=o.Repository.key(o.PersistenceKeys.ONE_TO_MANY);r.Decoration.for(p).define({decorator:(e,t,i,n,c)=>{const l={class:e.name?e.name:e,cascade:t,populate:i};return n&&(l.joinTable=n),c&&(l.name=c),s.apply(r.prop(o.PersistenceKeys.RELATIONS),r.list([e,String,Number]),a.onCreate(D,l),a.onUpdate(o.oneToManyOnUpdate,l),a.onDelete($,l),a.afterAny(B,l),r.propMetadata(p,l))}}).apply()}}j.decoration(),o.Adapter.setCurrent(g);class K extends r.JSONSerializer{constructor(){super()}deserialize(e){return super.deserialize(e)}serialize(e){return require("json-stringify-deterministic")(require("sort-keys-recursive")(this.preSerialize(e)))}}class L extends j{constructor(e,t,r){super(e,t),this.collections=r,this.collections=r||[]}async Sequence(e){return new T(e,this,this.collections)}async read(e,t,r,...i){const{stub:n,logger:o}=i.pop(),s=o.for(this.read);let c;try{const i=await this.readState(n,e,t.toString(),r);if(1>i.length)throw s.debug(`No record found for id ${t} in ${e} table`),new a.NotFoundError(`No record found for id ${t} in ${e} table`);2>i.length?(s.debug(`No record found for id ${t} in ${e} table`),c=i.pop()):c=this.mergeModels(i)}catch(e){throw this.parseError(e)}return c}async delete(e,t,r,...a){const i=a.pop(),{stub:n,logger:o}=i,s=o.for(this.delete);let c;a.push(i);try{c=await this.read(e,t,r,...a),s.verbose(`deleting entry with pk ${t} from ${e} table`),this.deleteState(n,e,t.toString(),r)}catch(e){throw this.parseError(e)}return c}prepare(e,t,...r){const{stub:i,logger:n}=r.pop(),s=r.shift(),c=n.for(this.prepare),l=((e,t)=>{const r=a.modelToTransient(t),i=v(t),n=t=>Object.entries(t).reduce((r,[i,n])=>{if(void 0===n)return r;const s=o.Repository.column(t,i);if(e.isReserved(s))throw new a.InternalError(`Property name ${s} is reserved`);return r[s]=n,r},{});if(i.private){const e=Object.keys(i.private);for(const t of e)i.private[t]=n(i.private[t])}return{model:r.model,transient:r.transient,privateData:i.private,result:n(r.model)}})(this,e);return c.info(`Preparing record for ${s} table with pk ${e[t]}`),{record:l.privateData,id:i.createCompositeKey(s,[e[t]+""]),transient:l.transient}}createPrefix(e,r,a,...i){const n=i.pop(),o=Object.keys(a);for(const r of o)a[r][t.CouchDBKeys.TABLE]=e;return[e,r,a,n]}updatePrefix(e,r,a,...i){const n=i.pop(),o=Object.keys(a);for(const r of o)a[r][t.CouchDBKeys.TABLE]=e;return[e,r,a,n]}async putState(e,t,r,...i){const n=Object.keys(r);let o,s={};for(const i of n){s=r[i];try{o=Buffer.from(j.serializer.serialize(r[i]))}catch(e){throw new a.SerializationError(`Failed to serialize record with id ${t}: ${e}`)}await e.putPrivateData(i,t.toString(),o)}return s}async readState(e,t,r,i,...n){const o=e.createCompositeKey(t,[r+""]),s=v(i),c=Object.keys(s.private),l=[];for(const t of c)try{let i=await e.getPrivateData(t,o);if(""===i.toString())throw new a.NotFoundError(`Entry with id ${r} doesn't exist...`);try{i=j.serializer.deserialize(i.toString())}catch(e){throw new a.SerializationError("Failed to parse private data: "+e)}l.push(i)}catch(e){if(w.test(e.message))throw new O(e);throw e}return l}async deleteState(e,t,r,a,...i){const n=e.createCompositeKey(t,[r+""]),o=v(a),s=Object.keys(o.private);for(const t of s)await e.deletePrivateData(t,n)}async queryResult(e,t,r){const a=v(r).private,i=Object.keys(a)[0]||"";return(await e.getPrivateDataQueryResult(i,JSON.stringify(t))).iterator}async queryResultPaginated(e,t,r=250,a=void 0,i){const n=v(i).private,o=Object.keys(n)[0]||"",s=await e.getPrivateDataQueryResult(o,JSON.stringify(t)),c=[];let l=0,d=!a,p=null;for(;;){const e=await s.next();if(e.value&&e.value.value.toString()){const t=e.value.key,i=e.value.value.toString("utf8");if(!d){t===a?.toString()&&(d=!0);continue}if(c.push({Key:t,Record:JSON.parse(i)}),p=t,l++,l>=r)return await s.close(),{iterator:c,metadata:{fetchedRecordsCount:c.length,bookmark:p}}}if(e.done)return await s.close(),{iterator:c,metadata:{fetchedRecordsCount:c.length,bookmark:""}}}}async raw(e,t,...r){const{stub:a,logger:i}=r.pop(),n=i.for(this.raw),{skip:o,limit:s}=e,c=r.shift();let l;s||o?(delete e.limit,delete e.skip,n.debug(`Retrieving paginated iterator: limit: ${s}/ skip: ${o}`),l=(await this.queryResultPaginated(a,e,s||250,o?.toString(),c)).iterator):(n.debug("Retrieving iterator"),l=await this.queryResult(a,e,c)),n.debug("Iterator acquired");const d=await this.resultIterator(n,l);return n.debug("returning {0} results",""+(Array.isArray(d)?d.length:1)),d}}class J extends i.Contract{static{this.serializer=new K}constructor(e,t){super(e),this.clazz=t,this.initialized=!1,J.adapter=this.getAdapter(t),this.repo=o.Repository.forModel(t,J.adapter.alias)}getAdapter(e){const t=new e;if(b(t)){const e=v(t),r=Object.keys(e.private);return new L(void 0,"fabric-private-data-adapter",r)}return new j(void 0,"fabric-public-data-adapter")}logFor(e){return n.Logging.for(J.name,{},e)}async create(e,t,...r){const a=this.logFor(e).for(this.create);"string"==typeof t&&(t=this.deserialize(t)),a.info("Creating model: "+JSON.stringify(t));const i=this.getTransientData(e);return a.info("Merging transient data..."),t=this.repo.merge(t,i),this.repo.create(t,e,...r)}async read(e,t,...r){return this.logFor(e).for(this.read).info(`reading entry with pk ${t} `),this.repo.read(t,e,...r)}getTransientData(e){const t=e.stub.getTransient();let r={};return t.has(this.repo.tableName)&&(r=JSON.parse(t.get(this.repo.tableName)?.toString("utf8"))),r}async update(e,t,...r){const a=this.logFor(e).for(this.update);"string"==typeof t&&(t=this.deserialize(t)),a.info("Updating model: "+JSON.stringify(t));const i=this.getTransientData(e);return a.info("Merging transient data..."),t=this.repo.merge(t,i),this.repo.update(t,e,...r)}async delete(e,t,...r){return this.logFor(e).for(this.delete).info(`deleting entry with pk ${t} `),this.repo.delete(t+"",e,...r)}async deleteAll(e,t,...r){return"string"==typeof t&&(t=JSON.parse(t)),this.repo.deleteAll(t,e,...r)}async readAll(e,t,...r){return"string"==typeof t&&(t=JSON.parse(t)),this.repo.readAll(t,e,...r)}async updateAll(e,t,...r){const a=this.logFor(e).for(this.updateAll);return"string"==typeof t&&(t=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e))),a.info(`updating ${t.length} entries to the table`),this.repo.updateAll(t,e,...r)}async raw(e,t,r,...a){return"string"==typeof t&&(t=JSON.parse(t)),this.repo.raw(t,r,e,...a)}serialize(e){return J.serializer.serialize(e)}deserialize(e){return J.serializer.deserialize(e)}async init(e){const t=this.logFor(e).for(this.init);t.info("Running contract initialization..."),this.initialized=!0,t.info("Contract initialization completed.")}async healthcheck(e){return this.logFor(e).for(this.healthcheck).info(`Running Healthcheck: ${this.initialized}...`),{healthcheck:this.initialized}}async createAll(e,t,...r){const a=this.logFor(e).for(this.createAll);return"string"==typeof t&&(t=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e))),a.info(`adding ${t.length} entries to the table`),this.repo.createAll(t,e,...r)}}class U extends J{constructor(e,t){super(e,t)}logFor(e){return n.Logging.for(U,{},e)}async create(e,t){const r=this.logFor(e).for(this.create);r.info("Creating model: "+t);const a=this.deserialize(t);return r.info("Model deserialized: "+JSON.stringify(a)),this.serialize(await super.create(e,a))}async read(e,t){return this.logFor(e).for(this.read).info("Reading id: "+t),this.serialize(await super.read(e,t))}async update(e,t){return this.logFor(e).for(this.update).info("Updating model: "+t),this.serialize(await super.update(e,t))}async delete(e,t){return this.logFor(e).for(this.delete).info("Deleting id: "+t),this.serialize(await super.delete(e,t))}async deleteAll(e,t){const r=JSON.parse(t);return this.logFor(e).for(this.deleteAll).info(`deleting ${r.length} entries from the table`),JSON.stringify((await super.deleteAll(e,r)).map(e=>this.serialize(e)))}async readAll(e,t){const r=JSON.parse(t);return this.logFor(e).for(this.readAll).info(`reading ${r.length} entries from the table`),JSON.stringify((await super.readAll(e,r)).map(e=>this.serialize(e)))}async updateAll(e,t){const r=this.logFor(e).for(this.updateAll),a=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e));return r.info(`Updating ${a.length} entries to the table`),JSON.stringify((await super.updateAll(e,a)).map(e=>this.serialize(e)))}async raw(e,t,r){const a=JSON.parse(t);return super.raw(e,a,r)}async init(e){await super.init(e)}async healthcheck(e){return JSON.stringify(await super.healthcheck(e))}async createAll(e,t){const r=this.logFor(e).for(this.createAll),a=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e));return r.info(`Adding ${a.length} entries to the table`),JSON.stringify((await super.createAll(e,a)).map(e=>this.serialize(e)))}}function V(e,t){const r=e+t;if(e!==r-t||t!==r-e)throw new S(`Addition overflow: ${e} + ${t}`);return r}function W(e,t){const r=e-t;if(e!==r+t||t!==e-r)throw new S(`Subtraction overflow: ${e} - ${t}`);return r}c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"create",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"read",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"update",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"delete",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"deleteAll",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"readAll",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"updateAll",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Boolean]),c.__metadata("design:returntype",Promise)],U.prototype,"raw",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],U.prototype,"init",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],U.prototype,"healthcheck",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"createAll",null);let G=class extends o.BaseModel{constructor(e){super(e)}};c.__decorate([o.pk({type:"String"}),c.__metadata("design:type",String)],G.prototype,"name",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],G.prototype,"owner",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],G.prototype,"symbol",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",Number)],G.prototype,"decimals",void 0),G=c.__decorate([o.table("erc20_tokens"),r.model(),c.__metadata("design:paramtypes",[Object])],G);let H=class extends o.BaseModel{constructor(e){super(e)}};c.__decorate([o.pk({type:"String"}),c.__metadata("design:type",String)],H.prototype,"id",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],H.prototype,"token",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",Number)],H.prototype,"balance",void 0),c.__decorate([o.column(),c.__metadata("design:type",String)],H.prototype,"captive",void 0),H=c.__decorate([o.table("erc20_wallets"),r.model(),c.__metadata("design:paramtypes",[Object])],H);let Q=class extends o.BaseModel{constructor(e){super(e)}};var Y;c.__decorate([o.pk({type:"String"}),o.column(),r.required(),c.__metadata("design:type",String)],Q.prototype,"owner",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],Q.prototype,"spender",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",Number)],Q.prototype,"value",void 0),Q=c.__decorate([o.table("erc20_allowances"),r.model(),c.__metadata("design:paramtypes",[Object])],Q),(e=>{e.TRANSFER="Transfer",e.APPROVAL="Approval"})(Y||(Y={}));class X extends J{constructor(e){super(e,H),X.adapter=X.adapter||new j,this.walletRepository=R.forModel(H,X.adapter.alias),this.tokenRepository=R.forModel(G,X.adapter.alias),this.allowanceRepository=R.forModel(Q,X.adapter.alias)}async TokenName(e){await this.CheckInitialized(e);const t=await this.tokenRepository.selectWithContext(void 0,e);return(await t.execute())[0].name}async Symbol(e){await this.CheckInitialized(e);const t=await this.tokenRepository.selectWithContext(void 0,e);return(await t.execute())[0].symbol}async Decimals(e){await this.CheckInitialized(e);const t=await this.tokenRepository.selectWithContext(void 0,e);return(await t.execute())[0].decimals}async TotalSupply(e){await this.CheckInitialized(e);const t=await this.walletRepository.selectWithContext(void 0,e),r=await t.execute();if(0==r.length)throw new a.NotFoundError(`The token ${this.getName()} does not exist`);let i=0;return r.forEach(e=>{i+=e.balance}),i}async BalanceOf(e,t){return await this.CheckInitialized(e),(await this.walletRepository.read(t,e)).balance}async Transfer(e,t,r){await this.CheckInitialized(e);const i=e.clientIdentity.getID();if(!await this._transfer(e,i,t,r))throw new a.InternalError("Failed to transfer");return!0}async TransferFrom(e,t,r,i){await this.CheckInitialized(e);const n=e.clientIdentity.getID(),o=await this._getAllowance(e,t,n);if(!o||0>o.value)throw new A(`spender ${n} has no allowance from ${t}`);const s=o.value;if(i>s)throw new C("The spender does not have enough allowance to spend.");const c=W(s,i),l=Object.assign({},o,{value:c});if(await this.allowanceRepository.update(l,e),!await this._transfer(e,t,r,i))throw new a.InternalError("Failed to transfer");return!0}async _transfer(e,t,r,i){const n=this.logFor(e).for(this._transfer);if(t===r)throw new o.AuthorizationError("cannot transfer to and from same client account");if(0>i)throw new C("transfer amount cannot be negative");const s=await this.walletRepository.read(t,e),c=s.balance;if(i>c)throw new C(`client account ${t} has insufficient funds.`);let l,d=!1;try{l=await this.walletRepository.read(r,e)}catch(t){if(!(t instanceof a.BaseError))throw new a.InternalError(t);if(404!==t.code)throw new a.InternalError(t.message);l=new H({id:r,balance:0,token:await this.TokenName(e)}),d=!0}const p=l.balance,u=W(c,i),h=V(p,i),g=Object.assign({},s,{balance:u});await this.walletRepository.update(g,e);const y=Object.assign({},l,{balance:h});d?await this.walletRepository.create(y,e):await this.walletRepository.update(y,e);const f={from:t,to:r,value:i};return this.repo.ObserverHandler().updateObservers(n,"",Y.TRANSFER,"",e,"",f),!0}async Approve(e,t,r){await this.CheckInitialized(e);const a=this.logFor(e).for(this.Approve),i=e.clientIdentity.getID();let n=await this._getAllowance(e,i,t);if((await this.walletRepository.read(i,e)).balance<r)throw new C(`client account ${i} has insufficient funds.`);n?(n.value=r,await this.allowanceRepository.update(n,e)):(n=new Q({owner:i,spender:t,value:r}),await this.allowanceRepository.create(n,e));const o={owner:i,spender:t,value:r};return this.repo.ObserverHandler().updateObservers(a,"",Y.APPROVAL,"",e,"",o),!0}async Allowance(e,t,r){await this.CheckInitialized(e);const a=await this._getAllowance(e,t,r);if(!a)throw new A(`spender ${r} has no allowance from ${t}`);return a.value}async _getAllowance(e,t,r){const a=o.Condition.and(o.Condition.attribute("owner").eq(t),o.Condition.attribute("spender").eq(r)),i=await this.allowanceRepository.selectWithContext(void 0,e),n=await i.where(a).execute();return n?.[0]}async Initialize(e,t){const r=await this.tokenRepository.selectWithContext(void 0,e);if((await r.execute()).length>0)throw new o.AuthorizationError("contract options are already set, client is not authorized to change them");return t.owner=e.clientIdentity.getID(),await this.tokenRepository.create(t,e),!0}async CheckInitialized(e){const t=await this.tokenRepository.selectWithContext(void 0,e);if(0==(await t.execute()).length)throw new k("contract options need to be set before calling any function, call Initialize() to initialize contract")}async Mint(e,t){await this.CheckInitialized(e);const r=this.logFor(e).for(this.Mint),i=e.clientIdentity.getID();if(0>=t)throw new a.ValidationError("mint amount must be a positive integer");let n;try{n=await this.walletRepository.read(i,e);const r=V(n.balance,t),a=Object.assign({},n,{balance:r});await this.walletRepository.update(a,e)}catch(r){if(!(r instanceof a.BaseError))throw new a.InternalError(r);if(404!==r.code)throw new a.InternalError(r.message);{const r=new H({id:i,balance:t,token:await this.TokenName(e)});await this.walletRepository.create(r,e)}}const o={from:"0x0",to:i,value:t};this.repo.ObserverHandler().updateObservers(r,"",Y.TRANSFER,"",e,"",o)}async Burn(e,t){await this.CheckInitialized(e);const r=this.logFor(e).for(this.Burn),a=e.clientIdentity.getID(),i=await this.walletRepository.read(a,e),n=i.balance;if(t>n)throw new C("Minter has insufficient funds.");const o=W(n,t),s=Object.assign({},i,{balance:o});await this.walletRepository.update(s,e),r.info(t+" tokens were burned");const c={from:a,to:"0x0",value:t};this.repo.ObserverHandler().updateObservers(r,"",Y.TRANSFER,"",e,"",c)}async BurnFrom(e,t,r){await this.CheckInitialized(e);const a=this.logFor(e).for(this.BurnFrom),i=await this.walletRepository.read(t,e),n=i.balance;if(r>n)throw new C(t+" has insufficient funds.");const o=W(n,r),s=Object.assign({},i,{balance:o});await this.walletRepository.update(s,e),a.info(`${r} tokens were berned from ${t}`);const c={from:t,to:"0x0",value:r};this.repo.ObserverHandler().updateObservers(a,"",Y.TRANSFER,"",e,"",c)}async ClientAccountBalance(e){await this.CheckInitialized(e);const t=e.clientIdentity.getID(),r=await this.walletRepository.read(t,e);if(!r)throw new C(`The account ${t} does not exist`);return r.balance}async ClientAccountID(e){return await this.CheckInitialized(e),e.clientIdentity.getID()}}c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"TokenName",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"Symbol",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"Decimals",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"TotalSupply",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],X.prototype,"BalanceOf",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Transfer",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"TransferFrom",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Approve",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,String]),c.__metadata("design:returntype",Promise)],X.prototype,"Allowance",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,G]),c.__metadata("design:returntype",Promise)],X.prototype,"Initialize",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"CheckInitialized",null),c.__decorate([y(),i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Mint",null),c.__decorate([y(),i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Burn",null),c.__decorate([y(),i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"BurnFrom",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"ClientAccountBalance",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"ClientAccountID",null);const Z=[X];class ee extends n.MiniLogger{constructor(e,t,r){if(super(e,t),r){if(r instanceof d)throw new E("Receiving incorrect context... It should be the contract context!!!");this.logger=r.logging.getLogger(e)}else this.logger=new n.MiniLogger(e,t)}log(e,t,r){if(n.NumericLogLevels[this.config("level")]<n.NumericLogLevels[e])return;let a;switch(e){case n.LogLevel.info:a=this.logger.info;break;case n.LogLevel.verbose:a=this.logger.verbose;break;case n.LogLevel.debug:a=this.logger.debug;break;case n.LogLevel.error:a=this.logger.error;break;case n.LogLevel.silly:a=this.logger.silly;break;default:throw Error("Invalid log level")}a.call(this.logger,this.createLog(e,t,r))}}n.Logging.setFactory((e,t,r)=>new ee(e,t||{},r));const te="##VERSION##",re="##PACKAGE##";l.Metadata.registerLibrary(re,te),e.ContractLogger=ee,e.FabricContractAdapter=j,e.FabricContractContext=d,e.FabricContractRepository=R,e.FabricContractRepositoryObservableHandler=p,e.FabricContractSequence=T,e.FabricCrudContract=J,e.FabricStatement=F,e.PACKAGE_NAME=re,e.SerializedCrudContract=U,e.VERSION=te,e.contracts=Z,e.createdByOnFabricCreateUpdate=M,e.pkFabricOnCreate=q},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@decaf-ts/for-couchdb"),require("@decaf-ts/decorator-validation"),require("@decaf-ts/db-decorators"),require("fabric-contract-api"),require("@decaf-ts/logging"),require("@decaf-ts/core"),require("@decaf-ts/reflection"),require("tslib"),require("@decaf-ts/decoration")):"function"==typeof define&&define.amd?define(["exports","@decaf-ts/for-couchdb","@decaf-ts/decorator-validation","@decaf-ts/db-decorators","fabric-contract-api","@decaf-ts/logging","@decaf-ts/core","@decaf-ts/reflection","tslib","@decaf-ts/decoration"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["for-fabric"]={},e.decafTsForCouchdb,e.decafTsDecoratorValidation,e.decafTsDbDecorators,e.fabricContractApi,e.decafTsLogging,e.decafTsCore,e.decafTsReflection,e.tslib,e.decafTsDecoration);
|
|
1
|
+
var e,t;e=this,t=function(e,t,r,a,i,n,o,s,c,l){"use strict";class d extends a.Context{constructor(){super()}get stub(){return this.get("stub")}get timestamp(){return this.stub.getDateTimestamp()}get identity(){return this.get("clientIdentity")}get logger(){return this.get("logger")}}class p extends o.ObserverHandler{constructor(e=[a.OperationKeys.CREATE,a.OperationKeys.UPDATE,a.OperationKeys.DELETE,a.BulkCrudOperationKeys.CREATE_ALL,a.BulkCrudOperationKeys.UPDATE_ALL,a.BulkCrudOperationKeys.DELETE_ALL]){super(),this.supportedEvents=e}async updateObservers(e,t,r,a,i,n,o){const{stub:s}=i;if(-1!==this.supportedEvents.indexOf(r)){e.debug(`Emitting ${r} event`);const i=((e,t,r)=>{const a=[e,t];return r&&a.push(r),a.join("_")})(t,r,n);s.setEvent(i,Buffer.from(JSON.stringify({id:a})))}else s.setEvent(r,Buffer.from(JSON.stringify(o)))}}var u,h;(e=>{e.PRIVATE="private",e.FABRIC="fabric.",e.OWNEDBY="owned-by"})(u||(u={})),(e=>{e.X509="X.509"})(h||(h={}));const g="hlf-fabric";function y(){return function(e,t,r){const i=r.value;return r.value=async function(...e){const r=e[0],n=r.clientIdentity.getID(),s=await this.tokenRepository.selectWithContext(void 0,r),c=await s.execute();if(0==c.length)throw new a.NotFoundError("No tokens avaialble");if(c.length>1)throw new a.NotFoundError("To many token available : "+c.length);if(c[0].owner!=n)throw new o.AuthorizationError(`User not authorized to run ${t} on the token`);return await i.apply(this,e)},r}}function f(e){return r.Model.key(u.FABRIC+e)}function m(e){if(!e)throw Error("Collection name is required");const t=f(u.PRIVATE);return(i,n)=>{const o=n||void 0,s=Reflect.getMetadata(t,i[r.ModelKeys.ANCHOR]||i,o),c=s?.collections||[];r.propMetadata(f(u.PRIVATE),{...!n&&{collections:c?[...new Set([...c,e])]:[e]},isPrivate:!n})(n?i.constructor:i[r.ModelKeys.ANCHOR]||i),n&&(r.propMetadata(f(u.PRIVATE),{collections:c?[...new Set([...c,e])]:[e]})(i,n),a.transient()(i,n))}}const w=/private\s+data\s+matching\s+public\s+hash\s+version\s+is\s+not\s+available/i;function _(e){let t=Reflect.getMetadata(f(u.PRIVATE),e);return t=t||Reflect.getMetadata(f(u.PRIVATE),e.constructor),t}function b(e){const t=_(e);return!(!t||void 0===t.isPrivate)&&t.isPrivate}function v(e){if(!(e=>!!_(e))(e))return{model:e};const t=a.getAllPropertyDecoratorsRecursive(e,void 0,f(u.PRIVATE)),i=b(e),n=_(e);let o={model:e,private:void 0};if(o=i?Object.keys(e).reduce((t,r)=>{const i=n.collections;t.private=t.private||{};for(const n of i)try{t.private[n]=t.private[n]||{},t.private[n][r]=e[r]}catch(e){throw new a.SerializationError(`Failed to serialize private property ${r}: ${e}`)}return t},{model:{}}):Object.entries(t).reduce((t,[r,i])=>{const n=i.find(e=>""===e.key);if(n){const i=n.props.collections;t.private=t.private||{};for(const n of i)try{t.private[n]=t.private[n]||{},t.private[n][r]=e[r]}catch(e){throw new a.SerializationError(`Failed to serialize private property ${r}: ${e}`)}}else t.model=t.model||{},t.model[r]=e[r];return t},{}),o.model=o.model||{},o.model=r.Model.build(o.model,e.constructor.name),o.private){const t=Object.keys(o.private);for(const a of t)o.private[a]=r.Model.build(o.private[a],e.constructor.name)}return o}class S extends a.InternalError{constructor(e){super(e,S.name)}}class C extends a.InternalError{constructor(e){super(e,C.name)}}class A extends a.InternalError{constructor(e){super(e,A.name)}}class E extends a.InternalError{constructor(e){super(e,E.name,500)}}class O extends a.BaseError{constructor(e="private data matching public hash version is not available ..."){super(O.name,e,403)}}class k extends a.BaseError{constructor(e){super(k.name,e,409)}}let x=class extends o.BaseModel{constructor(e){super(e)}};c.__decorate([o.pk(),c.__metadata("design:type",String)],x.prototype,"id",void 0),c.__decorate([r.required(),o.index(),c.__metadata("design:type",Object)],x.prototype,"current",void 0),x=c.__decorate([o.table(t.CouchDBKeys.SEQUENCE),r.model(),c.__metadata("design:paramtypes",[Object])],x);class T extends o.Sequence{constructor(e,t,r){super(e);for(const e of r||[])m(e)(x);this.repo=o.Repository.forModel(x,t.alias)}async current(e){if(!e)throw new E("Context is required");const{name:t,startWith:r}=this.options;try{const r=await this.repo.read(t,e);return this.parse(r.current)}catch(e){if(e instanceof a.NotFoundError){if(void 0===r)throw new a.InternalError("Starting value is not defined for a non existing sequence");try{return this.parse(r)}catch(e){throw new a.InternalError(`Failed to parse initial value for sequence ${r}: ${e}`)}}throw new a.InternalError(`Failed to retrieve current value for sequence ${t}: ${e}`)}}parse(e){return o.Sequence.parseValue(this.options.type,e)}async increment(e,t,r){if(!r)throw new E("Context is required");const{type:i,incrementBy:n,name:o}=this.options;let s;const c=t||n;if(c%n!==0)throw new a.InternalError("Value to increment does not consider the incrementBy setting: "+n);switch(i){case"Number":s=this.parse(e)+c;break;case"BigInt":s=this.parse(e)+BigInt(c);break;default:throw new a.InternalError("Should never happen")}let l;try{l=await this.repo.update(new x({id:o,current:s}),r)}catch(e){if(!(e instanceof a.NotFoundError))throw e;l=await this.repo.create(new x({id:o,current:s}),r)}return l.current}async next(e){if(!e)throw new E("Context is required");const t=await this.current(e);return this.increment(t,void 0,e)}async range(e,t){if(!t)throw new E("Context is required");const r=await this.current(t),i=this.parse(this.options.incrementBy),n=await this.increment(r,this.parse(e)*i,t),o=[];for(let t=1;e>=t;t++)o.push(r+i*this.parse(t));if(o[o.length-1]!==n)throw new a.InternalError("Miscalculation of range");return o}}class R extends o.Repository{constructor(e,t,r){super(e,t),this.trackedEvents=r}logFor(e){return n.Logging.for(R,{},e)}async create(e,...t){const r=t[t.length-1],a=this.logFor(r).for(this.create);a.info("Preparing model: "+JSON.stringify(e));let i,{record:n,id:o,transient:s}=this.adapter.prepare(e,this.pk,this.tableName,...t);return a.info("Creating model: "+JSON.stringify(e)),n=await this.adapter.create(this.tableName,o,n,...t),t.length&&(i=t[t.length-1]),a.info("Reverting model: "+JSON.stringify(e)),this.adapter.revert(n,this.class,this.pk,o,i&&i.get("rebuildWithTransient")?s:void 0)}async read(e,...t){const r=await this.adapter.read(this.tableName,e,new this.class,...t);return this.adapter.revert(r,this.class,this.pk,e)}async delete(e,...t){const r=await this.adapter.delete(this.tableName,e,new this.class,...t);return this.adapter.revert(r,this.class,this.pk,e)}async update(e,...t){const r=t[t.length-1],a=this.logFor(r).for(this.update);a.info("Preparing model: "+JSON.stringify(e));let i,{record:n,id:o,transient:s}=this.adapter.prepare(e,this.pk,this.tableName,...t);return a.info("Updating model: "+JSON.stringify(e)),n=await this.adapter.update(this.tableName,o,n,...t),t.length&&(i=t[t.length-1]),a.info("Reverting model: "+JSON.stringify(e)),this.adapter.revert(n,this.class,this.pk,o,i&&i.get("rebuildWithTransient")?s:void 0)}ObserverHandler(){return new p}async createAll(e,...t){if(!e.length)return e;const r=e.map(e=>this.adapter.prepare(e,this.pk,this.tableName,...t)),a=r.map(e=>e.id);let i=r.map(e=>e.record);const n=r.map(e=>e.transient).filter(e=>!!e);let o;return t.length&&(o=t[t.length-1]),i=await this.adapter.createAll(this.tableName,a,i,...t),i.map((e,t)=>this.adapter.revert(e,this.class,this.pk,a[t],o&&o.get("rebuildWithTransient")?n:void 0))}async updateAll(e,...t){if(!e.length)return e;const r=e.map(e=>this.adapter.prepare(e,this.pk,this.tableName,...t)),a=r.map(e=>e.transient).filter(e=>!!e);let i;return t.length&&(i=t[t.length-1]),(await this.adapter.updateAll(this.tableName,r.map(e=>e.id),r.map(e=>e.record),...t)).map((e,t)=>this.adapter.revert(e,this.class,this.pk,r[t].id,i&&i.get("rebuildWithTransient")?a:void 0))}async raw(e,t,...r){const i=r.pop(),n=await a.Context.args("QUERY",this.class,[i],this.adapter,{});return this.adapter.raw(e,t,new this.class,...n.args)}async updateObservers(e,t,r,a,...i){if(!this.trackedEvents||-1!==this.trackedEvents.indexOf(t))return await super.updateObservers(e,t,r,a,...i)}async selectWithContext(e,t){let r;return t instanceof d?r.context=t:r=await a.Context.args(a.OperationKeys.CREATE,this.class,[t],this.adapter,this._overrides||{}),e?this.adapter.Statement(r.context).select(e).from(this.class):this.adapter.Statement(r.context).select().from(this.class)}async createAllPrefix(e,...t){const r=t[t.length-1],i=await a.Context.args(a.OperationKeys.CREATE,this.class,t,this.adapter,this._overrides||{});if(!e.length)return[e,...i.args];const n=o.Repository.getSequenceOptions(e[0]);let s=[];n.type?(n.name||(n.name=T.pk(e[0])),s=await(await this.adapter.Sequence(n)).range(e.length,r)):s=e.map((e,t)=>{if(void 0===e[this.pk])throw new a.InternalError("Primary key is not defined for model in position "+t);return e[this.pk]}),e=await Promise.all(e.map(async(e,t)=>(e=new this.class(e),n.type&&(e[this.pk]=s[t]),await a.enforceDBDecorators(this,i.context,e,a.OperationKeys.CREATE,a.OperationKeys.ON),e)));const c=i.context.get("ignoredValidationProperties")||[],l=(await Promise.all(e.map(e=>Promise.resolve(e.hasErrors(...c))))).reduce((e,t,r)=>(t&&(e="string"==typeof e?e+`\n - ${r}: ${t.toString()}`:` - ${r}: ${t.toString()}`),e),void 0);if(l)throw new a.ValidationError(l);return[e,...i.args]}async createPrefix(e,...t){const r=await super.createPrefix(e,...t),i=r[0][this.pk];try{if(await this.read(i+"",...t))throw new a.ConflictError(`Conflict detected while creating model with id: ${i} already exists`)}catch(e){if(404!==e.code)throw e;this.logFor(t[t.length-1]).info(`Record entry with pk ${i} does not exist, creating it now...`)}return r}}class F extends t.CouchDBStatement{constructor(e,t){super(e),this.ctx=t}async raw(e){const t=await this.adapter.raw(e,!0,this.ctx),r=a.findPrimaryKey(new this.fromSelector),i=r.id,n=r.props.type;return this.selectSelector?t:t.map(e=>this.processRecord(e,i,n))}build(){const e={};e[t.CouchDBKeys.TABLE]={},e[t.CouchDBKeys.TABLE]=o.Repository.table(this.fromSelector);const r={selector:e};if(this.selectSelector&&(r.fields=this.selectSelector),this.whereCondition){const e=this.parseCondition(o.Condition.and(this.whereCondition,o.Condition.attribute(t.CouchDBKeys.TABLE).eq(r.selector[t.CouchDBKeys.TABLE]))).selector,a=Object.keys(e);if(1===a.length&&-1!==Object.values(t.CouchDBGroupOperator).indexOf(a[0]))switch(a[0]){case t.CouchDBGroupOperator.AND:e[t.CouchDBGroupOperator.AND]=[...Object.values(e[t.CouchDBGroupOperator.AND]).reduce((e,r)=>{const a=Object.keys(r);if(1!==a.length)throw Error("Too many keys in query selector. should be one");const i=a[0];return i===t.CouchDBGroupOperator.AND?e.push(...r[i]):e.push(r),e},[])],r.selector=e;break;case t.CouchDBGroupOperator.OR:{const a={};a[t.CouchDBGroupOperator.AND]=[e,...Object.entries(r.selector).map(([e,t])=>{const r={};return r[e]=t,r})],r.selector=a;break}default:throw Error("This should be impossible")}else Object.entries(e).forEach(([e,t])=>{r.selector[e],r.selector[e]=t})}if(this.orderBySelector){r.sort=r.sort||[],r.selector=r.selector||{};const[e,a]=this.orderBySelector,i={};i[e]=a,r.sort.push(i),r.selector[e]||(r.selector[e]={},r.selector[e][t.CouchDBOperator.BIGGER]=null)}return this.limitSelector&&(r.limit=this.limitSelector),this.offsetSelector&&(r.skip=this.offsetSelector),r}}class P extends r.JSONSerializer{constructor(){super()}deserialize(e,t){return JSON.parse(e)}serialize(e){return require("json-stringify-deterministic")(require("sort-keys-recursive")(this.preSerialize(e)))}preSerialize(e){return Object.assign({},e)}}async function N(e,t,i,n){const s=n[i];if(!s)return;if("object"!=typeof s){const t=o.repositoryFromTypeMetadata(n,i,this.adapter.alias),r=await t.read(s,e);return await o.cacheModelForPopulate(e,n,i,s,r),void(n[i]=s)}t.class="string"==typeof t.class?t.class:t.class().name;const c=r.Model.get(t.class);if(!c)throw new a.InternalError("Could not find model "+t.class);const l=o.Repository.forModel(c,this.adapter.alias),d=await l.create(s,e),p=a.findPrimaryKey(d).id;await o.cacheModelForPopulate(e,n,i,d[p],d),n[i]=d[p]}async function I(e,t,r,i){const n=i[r];if(!n)return;if(t.cascade.update!==o.Cascade.CASCADE)return;if("object"!=typeof n){const t=o.repositoryFromTypeMetadata(i,r,this.adapter.alias),a=await t.read(n,e);return await o.cacheModelForPopulate(e,i,r,n,a),void(i[r]=n)}const s=await o.createOrUpdate(i[r],e,this.adapter.alias),c=a.findPrimaryKey(s).id;await o.cacheModelForPopulate(e,i,r,s[c],s),i[r]=s[c]}async function z(e,t,a,i){const n=i[a];if(!n)return;if(t.cascade.update!==o.Cascade.CASCADE)return;const s=o.repositoryFromTypeMetadata(i,a,this.adapter.alias);let c;c=n instanceof r.Model?await s.delete(i[a][s.pk],e):await s.delete(i[a],e),await o.cacheModelForPopulate(e,i,a,c[s.pk],c)}async function D(e,t,r,i){const n=i[r];if(!n||!n.length)return;const s=typeof n[0];if(!n.every(e=>typeof e===s))throw new a.InternalError(`Invalid operation. All elements of property ${r} must match the same type.`);const c=new Set([...n]);if("object"!==s){const t=o.repositoryFromTypeMetadata(i,r,this.adapter.alias);for(const a of c){const n=await t.read(a,e);await o.cacheModelForPopulate(e,i,r,a,n)}return void(i[r]=[...c])}const l=a.findPrimaryKey(n[0]).id,d=new Set;for(const t of n){const a=await o.createOrUpdate(t,e,this.adapter.alias);await o.cacheModelForPopulate(e,i,r,a[l],a),d.add(a[l])}i[r]=[...d]}async function $(e,t,r,i){if(t.cascade.delete!==o.Cascade.CASCADE)return;const n=i[r];if(!n||!n.length)return;const s=typeof n[0];if(!n.every(e=>typeof e===s))throw new a.InternalError(`Invalid operation. All elements of property ${r} must match the same type.`);const c="object"===s,l=c?o.Repository.forModel(n[0],this.adapter.alias):o.repositoryFromTypeMetadata(i,r,this.adapter.alias),d=new Set([...c?n.map(e=>e[l.pk]):n]);for(const t of d.values()){const a=await l.delete(t,e);await o.cacheModelForPopulate(e,i,r,t,a)}i[r]=[...d]}async function B(e,t,r,i){if(!t.populate)return;const n=i[r],s=Array.isArray(n);if(void 0===n||s&&0===n.length)return;const c=await(async(t,r,i,n,s)=>{let c,l;const d=[];for(const p of n){c=o.getPopulateKey(r.constructor.name,i,p);try{l=await t.get(c)}catch(t){const n=o.repositoryFromTypeMetadata(r,i,s);if(!n)throw new a.InternalError("Could not find repo");l=await n.read(p,e)}d.push(l)}return d})(e,i,r,s?n:[n],this.adapter.alias);i[r]=s?c:c[0]}async function M(e,t,r,a){try{const t=e.get("clientIdentity");a[r]=t.getID()}catch(e){throw new o.UnsupportedError("No User found in context. Please provide a user in the context")}}async function q(e,t,r,i){if(!t.type||i[r])return;let n;t.name||(t.name=o.sequenceNameForModel(i,"pk"));try{n=await this.adapter.Sequence(t)}catch(e){throw new a.InternalError(`Failed to instantiate Sequence ${t.name}: ${e}`)}const s=await n.next(e);Object.defineProperty(i,r,{enumerable:!0,writable:!1,configurable:!0,value:s})}class j extends t.CouchDBAdapter{getClient(){throw new o.UnsupportedError("Client is not supported in Fabric contracts")}static{this.textDecoder=new TextDecoder("utf8")}static{this.serializer=new P}logFor(e){return n.Logging.for(j,{},e)}repository(){return R}constructor(e,t){super(e,g,t),this.Context=d}for(e,...t){return super.for(e,...t)}async create(e,t,r,...a){const{stub:i,logger:n}=a.pop(),o=n.for(this.create);try{o.info(`adding entry to ${e} table with pk ${t}`),r=await this.putState(i,t.toString(),r)}catch(e){throw this.parseError(e)}return r}async read(e,t,...r){const{stub:i,logger:n}=r.pop(),o=n.for(this.read);let s;try{const r=await this.readState(i,e,t.toString());if(1>r.length)throw o.debug(`No record found for id ${t} in ${e} table`),new a.NotFoundError(`No record found for id ${t} in ${e} table`);2>r.length?(o.debug(`No record found for id ${t} in ${e} table`),s=r.pop()):s=this.mergeModels(r)}catch(e){throw this.parseError(e)}return s}async update(e,t,r,...a){const{stub:i,logger:n}=a.pop(),o=n.for(this.update);try{o.info(`updating entry to ${e} table with pk ${t}`),r=await this.putState(i,t.toString(),r)}catch(e){throw this.parseError(e)}return r}async delete(e,t,...r){const a=r.pop(),{stub:i,logger:n}=a,o=n.for(this.delete);let s;r.push(a);try{s=await this.read(e,t,...r),o.verbose(`deleting entry with pk ${t} from ${e} table`),this.deleteState(i,e,t.toString())}catch(e){throw this.parseError(e)}return s}async deleteState(e,t,r,...a){const i=e.createCompositeKey(t,[r+""]);await e.deleteState(i)}async putState(e,t,r,...i){let n;try{n=Buffer.from(j.serializer.serialize(r))}catch(e){throw new a.SerializationError(`Failed to serialize record with id ${t}: ${e}`)}return await e.putState(t.toString(),n),r}async readState(e,t,r,...i){const n=e.createCompositeKey(t,[r+""]),o=[];let s=await e.getState(n);if(""===s.toString())throw new a.NotFoundError(`Record with id ${r} not found`);try{s=j.serializer.deserialize(s.toString())}catch(e){throw new a.SerializationError("Failed to parse record: "+e)}return o.push(s),o}async queryResult(e,t,...r){return await e.getQueryResult(JSON.stringify(t))}async queryResultPaginated(e,t,r=250,a,...i){return await e.getQueryResultWithPagination(JSON.stringify(t),r,a?.toString())}mergeModels(e){const t=e=>Object.entries(e).reduce((e,[t,r])=>(void 0!==r&&(e[t]=r),e),{});let r=e.pop();for(const a of e)r=Object.assign({},t(r),t(a));return r}decode(e){return j.textDecoder.decode(e)}async flags(e,t,r,a,...i){return Object.assign(await super.flags(e,t,r,...i),{stub:a.stub,identity:a.clientIdentity??a.identity,logger:a.logger??this.logFor(a)})}index(e){return Promise.resolve(void 0)}async resultIterator(e,t,r=!1){const a=[];let i=await t.next();for(;!i.done;){if(i.value&&i.value.value.toString()){let t={};if(e.debug(i.value.value.toString("utf8")),r){t.TxId=i.value.txId,t.Timestamp=i.value.timestamp;try{t.Value=JSON.parse(i.value.value.toString("utf8"))}catch(r){e.error(r),t.Value=i.value.value.toString("utf8")}}else try{t=JSON.parse(i.value.value.toString("utf8"))}catch(r){e.error(r),t=i.value.value.toString("utf8")}a.push(t)}i=await t.next()}return e.debug(`Closing iterator after ${a.length} results`),t.close(),a}async raw(e,t,...r){const{stub:a,logger:i}=r.pop(),n=i.for(this.raw),{skip:o,limit:s}=e;let c;s||o?(delete e.limit,delete e.skip,n.debug(`Retrieving paginated iterator: limit: ${s}/ skip: ${o}`),c=(await this.queryResultPaginated(a,e,s||250,o?.toString())).iterator):(n.debug("Retrieving iterator"),c=await this.queryResult(a,e)),n.debug("Iterator acquired");const l=await this.resultIterator(n,c);return n.debug("returning {0} results",""+(Array.isArray(l)?l.length:1)),l}Statement(e){if(!e)throw new E("Context is required");return new F(this,e)}async Sequence(e){return new T(e,this)}async createAll(e,t,r,...i){if(t.length!==r.length)throw new a.InternalError("Ids and models must have the same length");const{logger:n}=i[i.length-1],o=n.for(this.createAll);return o.info(`Creating ${t.length} entries ${e} table`),o.debug("pks: "+t),Promise.all(t.map(async(t,a)=>this.create(e,t,r[a],...i)))}async updateAll(e,t,r,...i){if(t.length!==r.length)throw new a.InternalError("Ids and models must have the same length");const{logger:n}=i[i.length-1],o=n.for(this.createAll);return o.info(`Updating ${t.length} entries ${e} table`),o.debug("pks: "+t),Promise.all(t.map(async(t,a)=>this.update(e,t,r[a],...i)))}prepare(e,t,...r){const{stub:i,logger:n}=r.pop(),s=r.shift(),c=n.for(this.prepare),l=a.modelToTransient(e),d=Object.entries(l.model).reduce((t,[r,i])=>{if(void 0===i)return t;const n=o.Repository.column(e,r);if(this.isReserved(n))throw new a.InternalError(`Property name ${n} is reserved`);return t[n]=i,t},{});return e[o.PersistenceKeys.METADATA]&&(c.silly("Passing along persistence metadata for "+e[o.PersistenceKeys.METADATA]),Object.defineProperty(d,o.PersistenceKeys.METADATA,{enumerable:!1,writable:!1,configurable:!0,value:e[o.PersistenceKeys.METADATA]})),c.info(`Preparing record for ${s} table with pk ${e[t]}`),{record:d,id:i.createCompositeKey(s,[e[t]+""]),transient:l.transient}}revert(e,t,i,n,s){const c=this.log.for(this.revert),l={};l[i]=n;const d="string"==typeof t?r.Model.build(l,t):new t(l);c.silly(`Rebuilding model ${d.constructor.name} id ${n}`);const p=e[o.PersistenceKeys.METADATA],u=Object.keys(d).reduce((t,r)=>(t[r]=e[o.Repository.column(t,r)],t),d);return s&&(c.verbose("re-adding transient properties: "+Object.keys(s).join(", ")),Object.entries(s).forEach(([e,t])=>{if(e in u&&void 0!==u[e])throw new a.InternalError(`Transient property ${e} already exists on model ${d.constructor.name}. should be impossible`);u[e]=t})),p&&(c.silly(`Passing along ${this.flavour} persistence metadata for ${d.constructor.name} id ${n}: ${p}`),Object.defineProperty(u,o.PersistenceKeys.METADATA,{enumerable:!1,configurable:!1,writable:!1,value:p})),u}createPrefix(e,r,a,...i){const n=i.pop(),o={};return o[t.CouchDBKeys.TABLE]=e,Object.assign(o,a),[e,r,o,n]}updatePrefix(e,r,a,...i){const n=i.pop(),o={};return o[t.CouchDBKeys.TABLE]=e,Object.assign(o,a),[e,r,o,n]}createAllPrefix(e,r,i,...n){if(r.length!==i.length)throw new a.InternalError("Ids and models must have the same length");const o=n.pop(),s=r.map((r,a)=>{const n={};return n[t.CouchDBKeys.TABLE]=e,Object.assign(n,i[a]),n});return[e,r,s,o]}updateAllPrefix(e,r,i,...n){if(r.length!==i.length)throw new a.InternalError("Ids and models must have the same length");const o=n.pop(),s=r.map((r,a)=>{const n={};return n[t.CouchDBKeys.TABLE]=e,Object.assign(n,i[a]),n});return[e,r,s,o]}static decoration(){super.decoration();const e=o.Repository.key(o.PersistenceKeys.CREATED_BY),t=o.Repository.key(o.PersistenceKeys.UPDATED_BY);r.Decoration.flavouredAs(g).for(e).define(a.onCreate(M),r.propMetadata(e,{})).apply(),r.Decoration.flavouredAs(g).for(t).define(a.onCreateUpdate(M),r.propMetadata(t,{})).apply();const n=o.Repository.key(a.DBKeys.ID);r.Decoration.flavouredAs(g).for(n).define(o.index([o.OrderDirection.ASC,o.OrderDirection.DSC]),r.required(),a.readonly(),r.propMetadata(n,o.NumericSequence),a.onCreate(q,o.NumericSequence)).apply();const c=o.Adapter.key(o.PersistenceKeys.COLUMN);r.Decoration.flavouredAs(g).for(c).extend(i.Property()).apply();const l=o.Adapter.key(o.PersistenceKeys.TABLE);r.Decoration.flavouredAs(g).for(l).extend(e=>i.Object()(e)).apply();const d=o.Repository.key(o.PersistenceKeys.ONE_TO_ONE);r.Decoration.flavouredAs(g).for(d).define({decorator:(e,t,i,n,c)=>{const l={class:e.name?e.name:e,cascade:t,populate:i};return n&&(l.joinTable=n),c&&(l.name=c),s.apply(r.prop(o.PersistenceKeys.RELATIONS),r.type([e.name?e.name:e,String.name,Number.name,BigInt.name]),a.onCreate(N,l),a.onUpdate(I,l),a.onDelete(z,l),a.afterAny(B,l),r.propMetadata(d,l))}}).apply();const p=o.Repository.key(o.PersistenceKeys.ONE_TO_MANY);r.Decoration.for(p).define({decorator:(e,t,i,n,c)=>{const l={class:e.name?e.name:e,cascade:t,populate:i};return n&&(l.joinTable=n),c&&(l.name=c),s.apply(r.prop(o.PersistenceKeys.RELATIONS),r.list([e,String,Number]),a.onCreate(D,l),a.onUpdate(o.oneToManyOnUpdate,l),a.onDelete($,l),a.afterAny(B,l),r.propMetadata(p,l))}}).apply()}}j.decoration(),o.Adapter.setCurrent(g);class K extends r.JSONSerializer{constructor(){super()}deserialize(e){return super.deserialize(e)}serialize(e){return require("json-stringify-deterministic")(require("sort-keys-recursive")(this.preSerialize(e)))}}class L extends j{constructor(e,t,r){super(e,t),this.collections=r,this.collections=r||[]}async Sequence(e){return new T(e,this,this.collections)}async read(e,t,r,...i){const{stub:n,logger:o}=i.pop(),s=o.for(this.read);let c;try{const i=await this.readState(n,e,t.toString(),r);if(1>i.length)throw s.debug(`No record found for id ${t} in ${e} table`),new a.NotFoundError(`No record found for id ${t} in ${e} table`);2>i.length?(s.debug(`No record found for id ${t} in ${e} table`),c=i.pop()):c=this.mergeModels(i)}catch(e){throw this.parseError(e)}return c}async delete(e,t,r,...a){const i=a.pop(),{stub:n,logger:o}=i,s=o.for(this.delete);let c;a.push(i);try{c=await this.read(e,t,r,...a),s.verbose(`deleting entry with pk ${t} from ${e} table`),this.deleteState(n,e,t.toString(),r)}catch(e){throw this.parseError(e)}return c}prepare(e,t,...r){const{stub:i,logger:n}=r.pop(),s=r.shift(),c=n.for(this.prepare),l=((e,t)=>{const r=a.modelToTransient(t),i=v(t),n=t=>Object.entries(t).reduce((r,[i,n])=>{if(void 0===n)return r;const s=o.Repository.column(t,i);if(e.isReserved(s))throw new a.InternalError(`Property name ${s} is reserved`);return r[s]=n,r},{});if(i.private){const e=Object.keys(i.private);for(const t of e)i.private[t]=n(i.private[t])}return{model:r.model,transient:r.transient,privateData:i.private,result:n(r.model)}})(this,e);return c.info(`Preparing record for ${s} table with pk ${e[t]}`),{record:l.privateData,id:i.createCompositeKey(s,[e[t]+""]),transient:l.transient}}createPrefix(e,r,a,...i){const n=i.pop(),o=Object.keys(a);for(const r of o)a[r][t.CouchDBKeys.TABLE]=e;return[e,r,a,n]}updatePrefix(e,r,a,...i){const n=i.pop(),o=Object.keys(a);for(const r of o)a[r][t.CouchDBKeys.TABLE]=e;return[e,r,a,n]}async putState(e,t,r,...i){const n=Object.keys(r);let o,s={};for(const i of n){s=r[i];try{o=Buffer.from(j.serializer.serialize(r[i]))}catch(e){throw new a.SerializationError(`Failed to serialize record with id ${t}: ${e}`)}await e.putPrivateData(i,t.toString(),o)}return s}async readState(e,t,r,i,...n){const o=e.createCompositeKey(t,[r+""]),s=v(i),c=Object.keys(s.private),l=[];for(const t of c)try{let i=await e.getPrivateData(t,o);if(""===i.toString())throw new a.NotFoundError(`Entry with id ${r} doesn't exist...`);try{i=j.serializer.deserialize(i.toString())}catch(e){throw new a.SerializationError("Failed to parse private data: "+e)}l.push(i)}catch(e){if(w.test(e.message))throw new O(e);throw e}return l}async deleteState(e,t,r,a,...i){const n=e.createCompositeKey(t,[r+""]),o=v(a),s=Object.keys(o.private);for(const t of s)await e.deletePrivateData(t,n)}async queryResult(e,t,r){const a=v(r).private,i=Object.keys(a)[0]||"";return(await e.getPrivateDataQueryResult(i,JSON.stringify(t))).iterator}async queryResultPaginated(e,t,r=250,a=void 0,i){const n=v(i).private,o=Object.keys(n)[0]||"",s=await e.getPrivateDataQueryResult(o,JSON.stringify(t)),c=[];let l=0,d=!a,p=null;for(;;){const e=await s.next();if(e.value&&e.value.value.toString()){const t=e.value.key,i=e.value.value.toString("utf8");if(!d){t===a?.toString()&&(d=!0);continue}if(c.push({Key:t,Record:JSON.parse(i)}),p=t,l++,l>=r)return await s.close(),{iterator:c,metadata:{fetchedRecordsCount:c.length,bookmark:p}}}if(e.done)return await s.close(),{iterator:c,metadata:{fetchedRecordsCount:c.length,bookmark:""}}}}async raw(e,t,...r){const{stub:a,logger:i}=r.pop(),n=i.for(this.raw),{skip:o,limit:s}=e,c=r.shift();let l;s||o?(delete e.limit,delete e.skip,n.debug(`Retrieving paginated iterator: limit: ${s}/ skip: ${o}`),l=(await this.queryResultPaginated(a,e,s||250,o?.toString(),c)).iterator):(n.debug("Retrieving iterator"),l=await this.queryResult(a,e,c)),n.debug("Iterator acquired");const d=await this.resultIterator(n,l);return n.debug("returning {0} results",""+(Array.isArray(d)?d.length:1)),d}}class J extends i.Contract{static{this.serializer=new K}constructor(e,t){super(e),this.clazz=t,this.initialized=!1,J.adapter=this.getAdapter(t),this.repo=o.Repository.forModel(t,J.adapter.alias)}getAdapter(e){const t=new e;if(b(t)){const e=v(t),r=Object.keys(e.private);return new L(void 0,"fabric-private-data-adapter",r)}return new j(void 0,g)}logFor(e){return n.Logging.for(J.name,{},e)}async create(e,t,...r){const a=this.logFor(e).for(this.create);"string"==typeof t&&(t=this.deserialize(t)),a.info("Creating model: "+JSON.stringify(t));const i=this.getTransientData(e);return a.info("Merging transient data..."),t=this.repo.merge(t,i),this.repo.create(t,e,...r)}async read(e,t,...r){return this.logFor(e).for(this.read).info(`reading entry with pk ${t} `),this.repo.read(t,e,...r)}getTransientData(e){const t=e.stub.getTransient();let r={};return t.has(this.repo.tableName)&&(r=JSON.parse(t.get(this.repo.tableName)?.toString("utf8"))),r}async update(e,t,...r){const a=this.logFor(e).for(this.update);"string"==typeof t&&(t=this.deserialize(t)),a.info("Updating model: "+JSON.stringify(t));const i=this.getTransientData(e);return a.info("Merging transient data..."),t=this.repo.merge(t,i),this.repo.update(t,e,...r)}async delete(e,t,...r){return this.logFor(e).for(this.delete).info(`deleting entry with pk ${t} `),this.repo.delete(t+"",e,...r)}async deleteAll(e,t,...r){return"string"==typeof t&&(t=JSON.parse(t)),this.repo.deleteAll(t,e,...r)}async readAll(e,t,...r){return"string"==typeof t&&(t=JSON.parse(t)),this.repo.readAll(t,e,...r)}async updateAll(e,t,...r){const a=this.logFor(e).for(this.updateAll);return"string"==typeof t&&(t=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e))),a.info(`updating ${t.length} entries to the table`),this.repo.updateAll(t,e,...r)}async raw(e,t,r,...a){return"string"==typeof t&&(t=JSON.parse(t)),this.repo.raw(t,r,e,...a)}serialize(e){return J.serializer.serialize(e)}deserialize(e){return J.serializer.deserialize(e)}async init(e){const t=this.logFor(e).for(this.init);t.info("Running contract initialization..."),this.initialized=!0,t.info("Contract initialization completed.")}async healthcheck(e){return this.logFor(e).for(this.healthcheck).info(`Running Healthcheck: ${this.initialized}...`),{healthcheck:this.initialized}}async createAll(e,t,...r){const a=this.logFor(e).for(this.createAll);return"string"==typeof t&&(t=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e))),a.info(`adding ${t.length} entries to the table`),this.repo.createAll(t,e,...r)}}class U extends J{constructor(e,t){super(e,t)}logFor(e){return n.Logging.for(U,{},e)}async create(e,t){const r=this.logFor(e).for(this.create);r.info("Creating model: "+t);const a=this.deserialize(t);return r.info("Model deserialized: "+JSON.stringify(a)),this.serialize(await super.create(e,a))}async read(e,t){return this.logFor(e).for(this.read).info("Reading id: "+t),this.serialize(await super.read(e,t))}async update(e,t){return this.logFor(e).for(this.update).info("Updating model: "+t),this.serialize(await super.update(e,t))}async delete(e,t){return this.logFor(e).for(this.delete).info("Deleting id: "+t),this.serialize(await super.delete(e,t))}async deleteAll(e,t){const r=JSON.parse(t);return this.logFor(e).for(this.deleteAll).info(`deleting ${r.length} entries from the table`),JSON.stringify((await super.deleteAll(e,r)).map(e=>this.serialize(e)))}async readAll(e,t){const r=JSON.parse(t);return this.logFor(e).for(this.readAll).info(`reading ${r.length} entries from the table`),JSON.stringify((await super.readAll(e,r)).map(e=>this.serialize(e)))}async updateAll(e,t){const r=this.logFor(e).for(this.updateAll),a=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e));return r.info(`Updating ${a.length} entries to the table`),JSON.stringify((await super.updateAll(e,a)).map(e=>this.serialize(e)))}async raw(e,t,r){const a=JSON.parse(t);return super.raw(e,a,r)}async init(e){await super.init(e)}async healthcheck(e){return JSON.stringify(await super.healthcheck(e))}async createAll(e,t){const r=this.logFor(e).for(this.createAll),a=JSON.parse(t).map(e=>this.deserialize(e)).map(e=>new this.clazz(e));return r.info(`Adding ${a.length} entries to the table`),JSON.stringify((await super.createAll(e,a)).map(e=>this.serialize(e)))}}function V(e,t){const r=e+t;if(e!==r-t||t!==r-e)throw new S(`Addition overflow: ${e} + ${t}`);return r}function W(e,t){const r=e-t;if(e!==r+t||t!==e-r)throw new S(`Subtraction overflow: ${e} - ${t}`);return r}c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"create",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"read",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"update",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"delete",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"deleteAll",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"readAll",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"updateAll",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Boolean]),c.__metadata("design:returntype",Promise)],U.prototype,"raw",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],U.prototype,"init",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],U.prototype,"healthcheck",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],U.prototype,"createAll",null);let G=class extends o.BaseModel{constructor(e){super(e)}};c.__decorate([o.pk({type:"String"}),c.__metadata("design:type",String)],G.prototype,"name",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],G.prototype,"owner",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],G.prototype,"symbol",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",Number)],G.prototype,"decimals",void 0),G=c.__decorate([o.table("erc20_tokens"),r.model(),c.__metadata("design:paramtypes",[Object])],G);let H=class extends o.BaseModel{constructor(e){super(e)}};c.__decorate([o.pk({type:"String"}),c.__metadata("design:type",String)],H.prototype,"id",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],H.prototype,"token",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",Number)],H.prototype,"balance",void 0),c.__decorate([o.column(),c.__metadata("design:type",String)],H.prototype,"captive",void 0),H=c.__decorate([o.table("erc20_wallets"),r.model(),c.__metadata("design:paramtypes",[Object])],H);let Q=class extends o.BaseModel{constructor(e){super(e)}};var Y;c.__decorate([o.pk({type:"String"}),o.column(),r.required(),c.__metadata("design:type",String)],Q.prototype,"owner",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",String)],Q.prototype,"spender",void 0),c.__decorate([o.column(),r.required(),c.__metadata("design:type",Number)],Q.prototype,"value",void 0),Q=c.__decorate([o.table("erc20_allowances"),r.model(),c.__metadata("design:paramtypes",[Object])],Q),(e=>{e.TRANSFER="Transfer",e.APPROVAL="Approval"})(Y||(Y={}));class X extends J{constructor(e){super(e,H),X.adapter=X.adapter||new j,this.walletRepository=R.forModel(H,X.adapter.alias),this.tokenRepository=R.forModel(G,X.adapter.alias),this.allowanceRepository=R.forModel(Q,X.adapter.alias)}async TokenName(e){await this.CheckInitialized(e);const t=await this.tokenRepository.selectWithContext(void 0,e);return(await t.execute())[0].name}async Symbol(e){await this.CheckInitialized(e);const t=await this.tokenRepository.selectWithContext(void 0,e);return(await t.execute())[0].symbol}async Decimals(e){await this.CheckInitialized(e);const t=await this.tokenRepository.selectWithContext(void 0,e);return(await t.execute())[0].decimals}async TotalSupply(e){await this.CheckInitialized(e);const t=await this.walletRepository.selectWithContext(void 0,e),r=await t.execute();if(0==r.length)throw new a.NotFoundError(`The token ${this.getName()} does not exist`);let i=0;return r.forEach(e=>{i+=e.balance}),i}async BalanceOf(e,t){return await this.CheckInitialized(e),(await this.walletRepository.read(t,e)).balance}async Transfer(e,t,r){await this.CheckInitialized(e);const i=e.clientIdentity.getID();if(!await this._transfer(e,i,t,r))throw new a.InternalError("Failed to transfer");return!0}async TransferFrom(e,t,r,i){await this.CheckInitialized(e);const n=e.clientIdentity.getID(),o=await this._getAllowance(e,t,n);if(!o||0>o.value)throw new A(`spender ${n} has no allowance from ${t}`);const s=o.value;if(i>s)throw new C("The spender does not have enough allowance to spend.");const c=W(s,i),l=Object.assign({},o,{value:c});if(await this.allowanceRepository.update(l,e),!await this._transfer(e,t,r,i))throw new a.InternalError("Failed to transfer");return!0}async _transfer(e,t,r,i){const n=this.logFor(e).for(this._transfer);if(t===r)throw new o.AuthorizationError("cannot transfer to and from same client account");if(0>i)throw new C("transfer amount cannot be negative");const s=await this.walletRepository.read(t,e),c=s.balance;if(i>c)throw new C(`client account ${t} has insufficient funds.`);let l,d=!1;try{l=await this.walletRepository.read(r,e)}catch(t){if(!(t instanceof a.BaseError))throw new a.InternalError(t);if(404!==t.code)throw new a.InternalError(t.message);l=new H({id:r,balance:0,token:await this.TokenName(e)}),d=!0}const p=l.balance,u=W(c,i),h=V(p,i),g=Object.assign({},s,{balance:u});await this.walletRepository.update(g,e);const y=Object.assign({},l,{balance:h});d?await this.walletRepository.create(y,e):await this.walletRepository.update(y,e);const f={from:t,to:r,value:i};return this.repo.ObserverHandler().updateObservers(n,"",Y.TRANSFER,"",e,"",f),!0}async Approve(e,t,r){await this.CheckInitialized(e);const a=this.logFor(e).for(this.Approve),i=e.clientIdentity.getID();let n=await this._getAllowance(e,i,t);if((await this.walletRepository.read(i,e)).balance<r)throw new C(`client account ${i} has insufficient funds.`);n?(n.value=r,await this.allowanceRepository.update(n,e)):(n=new Q({owner:i,spender:t,value:r}),await this.allowanceRepository.create(n,e));const o={owner:i,spender:t,value:r};return this.repo.ObserverHandler().updateObservers(a,"",Y.APPROVAL,"",e,"",o),!0}async Allowance(e,t,r){await this.CheckInitialized(e);const a=await this._getAllowance(e,t,r);if(!a)throw new A(`spender ${r} has no allowance from ${t}`);return a.value}async _getAllowance(e,t,r){const a=o.Condition.and(o.Condition.attribute("owner").eq(t),o.Condition.attribute("spender").eq(r)),i=await this.allowanceRepository.selectWithContext(void 0,e),n=await i.where(a).execute();return n?.[0]}async Initialize(e,t){const r=await this.tokenRepository.selectWithContext(void 0,e);if((await r.execute()).length>0)throw new o.AuthorizationError("contract options are already set, client is not authorized to change them");return t.owner=e.clientIdentity.getID(),await this.tokenRepository.create(t,e),!0}async CheckInitialized(e){const t=await this.tokenRepository.selectWithContext(void 0,e);if(0==(await t.execute()).length)throw new k("contract options need to be set before calling any function, call Initialize() to initialize contract")}async Mint(e,t){await this.CheckInitialized(e);const r=this.logFor(e).for(this.Mint),i=e.clientIdentity.getID();if(0>=t)throw new a.ValidationError("mint amount must be a positive integer");let n;try{n=await this.walletRepository.read(i,e);const r=V(n.balance,t),a=Object.assign({},n,{balance:r});await this.walletRepository.update(a,e)}catch(r){if(!(r instanceof a.BaseError))throw new a.InternalError(r);if(404!==r.code)throw new a.InternalError(r.message);{const r=new H({id:i,balance:t,token:await this.TokenName(e)});await this.walletRepository.create(r,e)}}const o={from:"0x0",to:i,value:t};this.repo.ObserverHandler().updateObservers(r,"",Y.TRANSFER,"",e,"",o)}async Burn(e,t){await this.CheckInitialized(e);const r=this.logFor(e).for(this.Burn),a=e.clientIdentity.getID(),i=await this.walletRepository.read(a,e),n=i.balance;if(t>n)throw new C("Minter has insufficient funds.");const o=W(n,t),s=Object.assign({},i,{balance:o});await this.walletRepository.update(s,e),r.info(t+" tokens were burned");const c={from:a,to:"0x0",value:t};this.repo.ObserverHandler().updateObservers(r,"",Y.TRANSFER,"",e,"",c)}async BurnFrom(e,t,r){await this.CheckInitialized(e);const a=this.logFor(e).for(this.BurnFrom),i=await this.walletRepository.read(t,e),n=i.balance;if(r>n)throw new C(t+" has insufficient funds.");const o=W(n,r),s=Object.assign({},i,{balance:o});await this.walletRepository.update(s,e),a.info(`${r} tokens were berned from ${t}`);const c={from:t,to:"0x0",value:r};this.repo.ObserverHandler().updateObservers(a,"",Y.TRANSFER,"",e,"",c)}async ClientAccountBalance(e){await this.CheckInitialized(e);const t=e.clientIdentity.getID(),r=await this.walletRepository.read(t,e);if(!r)throw new C(`The account ${t} does not exist`);return r.balance}async ClientAccountID(e){return await this.CheckInitialized(e),e.clientIdentity.getID()}}c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"TokenName",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"Symbol",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"Decimals",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"TotalSupply",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String]),c.__metadata("design:returntype",Promise)],X.prototype,"BalanceOf",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Transfer",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"TransferFrom",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Approve",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,String]),c.__metadata("design:returntype",Promise)],X.prototype,"Allowance",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,G]),c.__metadata("design:returntype",Promise)],X.prototype,"Initialize",null),c.__decorate([i.Transaction(!1),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"CheckInitialized",null),c.__decorate([y(),i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Mint",null),c.__decorate([y(),i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"Burn",null),c.__decorate([y(),i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context,String,Number]),c.__metadata("design:returntype",Promise)],X.prototype,"BurnFrom",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"ClientAccountBalance",null),c.__decorate([i.Transaction(),c.__metadata("design:type",Function),c.__metadata("design:paramtypes",[i.Context]),c.__metadata("design:returntype",Promise)],X.prototype,"ClientAccountID",null);const Z=[X];class ee extends n.MiniLogger{constructor(e,t,r){if(super(e,t),r){if(r instanceof d)throw new E("Receiving incorrect context... It should be the contract context!!!");this.logger=r.logging.getLogger(e)}else this.logger=new n.MiniLogger(e,t)}log(e,t,r){if(n.NumericLogLevels[this.config("level")]<n.NumericLogLevels[e])return;let a;switch(e){case n.LogLevel.info:a=this.logger.info;break;case n.LogLevel.verbose:a=this.logger.verbose;break;case n.LogLevel.debug:a=this.logger.debug;break;case n.LogLevel.error:a=this.logger.error;break;case n.LogLevel.silly:a=this.logger.silly;break;default:throw Error("Invalid log level")}a.call(this.logger,this.createLog(e,t,r))}}n.Logging.setFactory((e,t,r)=>new ee(e,t||{},r));const te="##VERSION##",re="##PACKAGE##";l.Metadata.registerLibrary(re,te),e.ContractLogger=ee,e.FabricContractAdapter=j,e.FabricContractContext=d,e.FabricContractRepository=R,e.FabricContractRepositoryObservableHandler=p,e.FabricContractSequence=T,e.FabricCrudContract=J,e.FabricStatement=F,e.PACKAGE_NAME=re,e.SerializedCrudContract=U,e.VERSION=te,e.contracts=Z,e.createdByOnFabricCreateUpdate=M,e.pkFabricOnCreate=q},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@decaf-ts/for-couchdb"),require("@decaf-ts/decorator-validation"),require("@decaf-ts/db-decorators"),require("fabric-contract-api"),require("@decaf-ts/logging"),require("@decaf-ts/core"),require("@decaf-ts/reflection"),require("tslib"),require("@decaf-ts/decoration")):"function"==typeof define&&define.amd?define(["exports","@decaf-ts/for-couchdb","@decaf-ts/decorator-validation","@decaf-ts/db-decorators","fabric-contract-api","@decaf-ts/logging","@decaf-ts/core","@decaf-ts/reflection","tslib","@decaf-ts/decoration"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["for-fabric"]={},e.decafTsForCouchdb,e.decafTsDecoratorValidation,e.decafTsDbDecorators,e.fabricContractApi,e.decafTsLogging,e.decafTsCore,e.decafTsReflection,e.tslib,e.decafTsDecoration);
|
|
2
2
|
//# sourceMappingURL=for-fabric.cjs.map
|