Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

feat!: chain Asset Lock proof #296

Merged
merged 34 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/StateRepositoryInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,31 @@
*/

/**
* Check if AssetLock Transaction outPoint exists in spent list
* Store AssetLock Transaction outPoint in spent list
*
* @async
* @method
* @name StateRepository#checkAssetLockTransactionOutPointExists
* @name StateRepository#storeAssetLockTransactionOutPoint
* @param {Buffer} outPointBuffer
* @returns {Promise<boolean>}
* @returns {Promise<void>}
*/

/**
* Store AssetLock Transaction outPoint in spent list
* Verify AssetLock outPoint
*
* @async
* @method
* @name StateRepository#storeAssetLockTransactionOutPoint
* @name StateRepository#checkAssetLockTransactionOutPointAlreadyUsed
* @param {Buffer} outPointBuffer
* @returns {Promise<void>}
* @returns {Promise<boolean>}
*/

/**
* Verify AssetLock height
*
* @async
* @method
* @name StateRepository#verifyChainLockHeight
* @param {number} height
* @returns {Promise<boolean>}
*/
23 changes: 23 additions & 0 deletions lib/errors/InvalidIdentityAssetLockProofHeightError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const ConsensusError = require('./ConsensusError');

class InvalidIdentityAssetLockProofHeightError extends ConsensusError {
/**
*
* @param {number} height
*/
constructor(height) {
super('Asset lock proof height is greater than consensus height');

this.height = height;
}

/**
*
* @returns {number}
*/
getHeight() {
return this.height;
}
}

module.exports = InvalidIdentityAssetLockProofHeightError;
23 changes: 23 additions & 0 deletions lib/errors/InvalidIdentityAssetLockProofOutPointError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const ConsensusError = require('./ConsensusError');

class InvalidIdentityAssetLockProofOutPointError extends ConsensusError {
/**
*
* @param {Buffer} outPoint
*/
constructor(outPoint) {
super('Asset lock proof outPoint was already used');

this.outPoint = outPoint;
}

/**
*
* @returns {Buffer}
*/
getOutPoint() {
return this.outPoint;
}
}

module.exports = InvalidIdentityAssetLockProofOutPointError;
17 changes: 5 additions & 12 deletions lib/identity/IdentityFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ class IdentityFacade {
/**
* Create Identity
*
* @param {Transaction} assetLockTransaction
* @param {number} outputIndex
* @param {InstantAssetLockProof} assetLockProof
* @param {PublicKey[]} publicKeys
* @return {Identity}
*/
create(assetLockTransaction, outputIndex, assetLockProof, publicKeys) {
create(assetLockProof, publicKeys) {
return this.factory.create(
assetLockTransaction,
outputIndex,
assetLockProof,
publicKeys,
);
Expand Down Expand Up @@ -86,10 +82,11 @@ class IdentityFacade {
* Create Asset Lock with proofs
*
* @param {InstantLock} instantLock
* @param {Transaction} assetLockTransaction
* @returns {InstantAssetLockProof}
*/
createInstantAssetLockProof(instantLock) {
return this.factory.createInstantAssetLockProof(instantLock);
createInstantAssetLockProof(instantLock, assetLockTransaction) {
return this.factory.createInstantAssetLockProof(instantLock, assetLockTransaction);
}

/**
Expand All @@ -106,16 +103,12 @@ class IdentityFacade {
* Create identity top up transition
*
* @param {Identifier|Buffer|string} identityId - identity to top up
* @param {Transaction} assetLockTransaction
* @param {number} outputIndex
* @param {InstantAssetLockProof} assetLockProof
* @return {IdentityTopUpTransition}
*/
createIdentityTopUpTransition(identityId, assetLockTransaction, outputIndex, assetLockProof) {
createIdentityTopUpTransition(identityId, assetLockProof) {
return this.factory.createIdentityTopUpTransition(
identityId,
assetLockTransaction,
outputIndex,
assetLockProof,
);
}
Expand Down
16 changes: 5 additions & 11 deletions lib/identity/IdentityFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ class IdentityFactory {
/**
* Create Identity
*
* @param {Transaction} assetLockTransaction
* @param {number} outputIndex
* @param {InstantAssetLockProof} assetLockProof
* @param {PublicKey[]} publicKeys
* @return {Identity}
*/
create(assetLockTransaction, outputIndex, assetLockProof, publicKeys) {
create(assetLockProof, publicKeys) {
const assetLock = new AssetLock({
transaction: assetLockTransaction.toBuffer(),
outputIndex,
proof: assetLockProof.toObject(),
});

Expand Down Expand Up @@ -102,11 +98,13 @@ class IdentityFactory {
* Create Asset Lock with proofs
*
* @param {InstantLock} instantLock
* @param {Transaction} assetLockTransaction
* @returns {InstantAssetLockProof}
*/
createInstantAssetLockProof(instantLock) {
createInstantAssetLockProof(instantLock, assetLockTransaction) {
return new InstantAssetLockProof({
instantLock: instantLock.toBuffer(),
transaction: assetLockTransaction.toBuffer(),
});
}

Expand All @@ -131,15 +129,11 @@ class IdentityFactory {
* Create identity top up transition
*
* @param {Identifier|Buffer|string} identityId - identity to top up
* @param {Transaction} assetLockTransaction
* @param {number} outputIndex
* @param {InstantAssetLockProof} assetLockProof
* @return {IdentityTopUpTransition}
*/
createIdentityTopUpTransition(identityId, assetLockTransaction, outputIndex, assetLockProof) {
createIdentityTopUpTransition(identityId, assetLockProof) {
const assetLock = new AssetLock({
transaction: assetLockTransaction.toBuffer(),
outputIndex,
proof: assetLockProof.toObject(),
});

Expand Down
55 changes: 4 additions & 51 deletions lib/identity/stateTransitions/assetLock/AssetLock.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,14 @@
const { Transaction } = require('@dashevo/dashcore-lib');

const hash = require('../../../util/hash');

const InstantAssetLockProof = require('./proof/instant/InstantAssetLockProof');
const Identifier = require('../../../identifier/Identifier');
const ChainAssetLockProof = require('./proof/chain/ChainAssetLockProof');

class AssetLock {
/**
* @param {RawAssetLock} rawAssetLock
*/
constructor(rawAssetLock) {
this.outputIndex = rawAssetLock.outputIndex;
this.transaction = new Transaction(rawAssetLock.transaction);
this.proof = new AssetLock.PROOF_CLASSES_BY_TYPES[rawAssetLock.proof.type](rawAssetLock.proof);
}

/**
* Get asset lock transaction
*
* @returns {Transaction}
*/
getTransaction() {
return this.transaction;
}

/**
* Get asset lock transaction output index
*
* @returns {number}
*/
getOutputIndex() {
return this.outputIndex;
}

/**
* Get transaction output
*
* @returns {Output}
*/
getOutput() {
return this.transaction.outputs[this.getOutputIndex()];
}

/**
* Get transaction outPoint
* @return {Buffer}
*/
getOutPoint() {
return this.transaction.getOutPointBuffer(this.getOutputIndex());
}

/**
* Get proof
*
Expand All @@ -65,9 +24,7 @@ class AssetLock {
* @returns {Identifier}
*/
createIdentifier() {
return new Identifier(
hash(this.getTransaction().getOutPointBuffer(this.getOutputIndex())),
);
return this.proof.createIdentifier();
}

/**
Expand All @@ -76,8 +33,6 @@ class AssetLock {
*/
toObject() {
return {
transaction: this.getTransaction().toBuffer(),
outputIndex: this.getOutputIndex(),
proof: this.getProof().toObject(),
};
}
Expand All @@ -88,30 +43,28 @@ class AssetLock {
toJSON() {
return {
...this.toObject(),
transaction: this.getTransaction().toString('base64'),
proof: this.getProof().toJSON(),
};
}
}

/**
* @typedef {Object} RawAssetLock
* @property {Buffer} transaction
* @property {number} outputIndex
* @property {RawInstantAssetLockProof} proof
*/

/**
* @typedef {Object} JsonAssetLock
* @property {string} transaction
* @property {number} outputIndex
* @property {JsonInstantAssetLockProof} proof
*/

AssetLock.PROOF_TYPE_INSTANT = 0;
AssetLock.PROOF_TYPE_CHAIN = 1;

AssetLock.PROOF_CLASSES_BY_TYPES = {
[AssetLock.PROOF_TYPE_INSTANT]: InstantAssetLockProof,
[AssetLock.PROOF_TYPE_CHAIN]: ChainAssetLockProof,
};

module.exports = AssetLock;
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const Identifier = require('../../../../../identifier/Identifier');
const hash = require('../../../../../util/hash');

class ChainAssetLockProof {
/**
* @param {RawChainAssetLockProof} rawAssetLockProof
*/
constructor(rawAssetLockProof) {
this.height = rawAssetLockProof.height;
this.outPoint = rawAssetLockProof.outPoint;
}

/**
* Get proof type
*
* @returns {number}
*/
getType() {
return 1;
}

/**
* Get height
*
* @returns {number}
*/
getHeight() {
return this.height;
}

/**
* Get outPoint
*
* @return {Buffer}
*/
getOutPoint() {
return this.outPoint;
}

/**
* Create identifier
*
* @returns {Identifier}
*/
createIdentifier() {
return new Identifier(
hash(this.getOutPoint()),
);
}

/**
* Get plain object representation
*
* @returns {RawChainAssetLockProof}
*/
toObject() {
return {
type: this.getType(),
height: this.getHeight(),
outPoint: this.getOutPoint(),
};
}

/**
* Get JSON representation
*
* @returns {JsonChainAssetLockProof}
*/
toJSON() {
return {
type: this.getType(),
height: this.getHeight(),
outPoint: this.getOutPoint().toString('hex'),
};
}
}

/**
* @typedef {Object} RawChainAssetLockProof
* @property {number} type
* @property {number} height
* @property {Buffer} outPoint
*/

/**
* @typedef {Object} JsonChainAssetLockProof
* @property {number} type
* @property {number} height
* @property {string} outPoint
*/

module.exports = ChainAssetLockProof;
Loading