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

Commit

Permalink
feat: integrate dpp with Chain Lock Proof support
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shuplenkov committed Apr 16, 2021
1 parent 3a6945c commit cf8b73a
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 26 deletions.
25 changes: 22 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@dashevo/dapi-client": "~0.18.0",
"@dashevo/dashcore-lib": "~0.19.21",
"@dashevo/dpp": "~0.19.0-dev.1",
"@dashevo/dpp": "github:dashevo/js-dpp#chain-asset-lock",
"@dashevo/wallet-lib": "~7.19.0-dev.1",
"bs58": "^4.0.1"
},
Expand Down
12 changes: 6 additions & 6 deletions src/SDK/Client/Client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ describe('Dash - Client', function suite() {

const serializedSt = dapiClientMock.platform.broadcastStateTransition.getCall(0).args[0];
const interceptedIdentityStateTransition = await client.platform.dpp.stateTransition.createFromBuffer(serializedSt);
const interceptedAssetLock = interceptedIdentityStateTransition.getAssetLock();
const interceptedAssetLockProof = interceptedIdentityStateTransition.getAssetLockProof();

const transaction = new Transaction(transportMock.sendTransaction.getCall(0).args[0]);
const isLock = createFakeInstantLock(transaction.hash);

// Check intercepted st
expect(interceptedAssetLock.getProof().getInstantLock()).to.be.deep.equal(isLock);
expect(interceptedAssetLock.getTransaction().hash).to.be.equal(transaction.hash);
expect(interceptedAssetLockProof.getInstantLock()).to.be.deep.equal(isLock);
expect(interceptedAssetLockProof.getTransaction().hash).to.be.equal(transaction.hash);

const importedIdentityIds = account.identities.getIdentityIds();
// Check that we've imported identities properly
Expand Down Expand Up @@ -173,15 +173,15 @@ describe('Dash - Client', function suite() {

const serializedSt = dapiClientMock.platform.broadcastStateTransition.getCall(1).args[0];
const interceptedIdentityStateTransition = await client.platform.dpp.stateTransition.createFromBuffer(serializedSt);
const interceptedAssetLock = interceptedIdentityStateTransition.getAssetLock();
const interceptedAssetLockProof = interceptedIdentityStateTransition.getAssetLockProof();

expect(interceptedIdentityStateTransition.getType()).to.be.equal(stateTransitionTypes.IDENTITY_TOP_UP);

const transaction = new Transaction(transportMock.sendTransaction.getCall(1).args[0]);
const isLock = createFakeInstantLock(transaction.hash);
// Check intercepted st
expect(interceptedAssetLock.getProof().getInstantLock()).to.be.deep.equal(isLock);
expect(interceptedAssetLock.getTransaction().hash).to.be.equal(transaction.hash);
expect(interceptedAssetLockProof.getInstantLock()).to.be.deep.equal(isLock);
expect(interceptedAssetLockProof.getTransaction().hash).to.be.equal(transaction.hash);
});

it('should throw TransitionBroadcastError when transport resolves error', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/SDK/Client/Platform/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class Platform {
// This check still exists on the client side, however there's no need to
// perform the check as in this client we always use a new transaction
// register/top up identity
checkAssetLockTransactionOutPointExists() { return false; },
isAssetLockTransactionOutPointAlreadyUsed() { return false; },
verifyInstantLock() { return true; },
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { createFakeInstantLock } from "../../../../../../utils/createFakeIntantL
* Creates a funding transaction for the platform identity and returns one-time key to sign the state transition
* @param {Platform} platform
* @param {Transaction} assetLockTransaction
* @param {number} outputIndex - index of the funding output in the asset lock transaction
* @return {AssetLockProof} - asset lock proof to be used in the state transition
* that can be used to sign registration/top-up state transition
*/
export default async function createAssetLockProof(platform : Platform, assetLockTransaction: Transaction): Promise<any> {
export default async function createAssetLockProof(platform : Platform, assetLockTransaction: Transaction, outputIndex: number): Promise<any> {
const account = await platform.client.getWalletAccount();
const { dpp } = platform;

let instantLock = await account.waitForInstantLock(assetLockTransaction.hash);
// Create poof that the transaction won't be double spend

// @ts-ignore
return await dpp.identity.createInstantAssetLockProof(instantLock);
return await dpp.identity.createInstantAssetLockProof(instantLock, assetLockTransaction, outputIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { Platform } from "../../../Platform";
/**
* Creates a funding transaction for the platform identity and returns one-time key to sign the state transition
* @param {Platform} platform
* @param {Transaction} assetLockTransaction
* @param {number} assetLockOutputIndex - index of the funding output in the asset lock transaction
* @param {AssetLockProof} assetLockProof - asset lock transaction proof for the identity create transition
* @param {PrivateKey} assetLockPrivateKey - private key used in asset lock
* @return {{identity: Identity, identityCreateTransition: IdentityCreateTransition}} - identity, state transition and index of the key used to create it
* that can be used to sign registration/top-up state transition
*/
export default async function createIdentityCreateTransition(platform : Platform, assetLockTransaction: Transaction, assetLockOutputIndex: number, assetLockProof: any, assetLockPrivateKey: PrivateKey): Promise<{ identity: any, identityCreateTransition: any, identityIndex: number }> {
export default async function createIdentityCreateTransition(platform : Platform, assetLockProof: any, assetLockPrivateKey: PrivateKey): Promise<{ identity: any, identityCreateTransition: any, identityIndex: number }> {
const account = await platform.client.getWalletAccount();
const { dpp } = platform;

Expand All @@ -24,7 +22,7 @@ export default async function createIdentityCreateTransition(platform : Platform
// Create Identity
// @ts-ignore
const identity = dpp.identity.create(
assetLockTransaction, assetLockOutputIndex, assetLockProof, [identityPublicKey]
assetLockProof, [identityPublicKey]
);

// Create ST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import { Platform } from "../../../Platform";
/**
* Creates a funding transaction for the platform identity and returns one-time key to sign the state transition
* @param {Platform} platform
* @param {Transaction} assetLockTransaction
* @param {number} assetLockOutputIndex - index of the funding output in the asset lock transaction
* @param {AssetLockProof} assetLockProof - asset lock transaction proof for the identity create transition
* @param {PrivateKey} assetLockPrivateKey - private key used in asset lock
* @param {string|Buffer|Identifier} identityId
* @return {{identity: Identity, identityCreateTransition: IdentityCreateTransition}} - identity, state transition and index of the key used to create it
* that can be used to sign registration/top-up state transition
*/
export default async function createIdentityTopUpTransition(platform : Platform, assetLockTransaction: Transaction, assetLockOutputIndex: number, assetLockProof: any, assetLockPrivateKey: PrivateKey, identityId: any): Promise<any> {
export default async function createIdentityTopUpTransition(platform : Platform, assetLockProof: any, assetLockPrivateKey: PrivateKey, identityId: any): Promise<any> {
const { dpp } = platform;

// @ts-ignore
const identityTopUpTransition = dpp.identity.createIdentityTopUpTransition(
identityId, assetLockTransaction, assetLockOutputIndex, assetLockProof
identityId, assetLockProof
);

identityTopUpTransition.signByPrivateKey(assetLockPrivateKey);
Expand Down
4 changes: 2 additions & 2 deletions src/SDK/Client/Platform/methods/identities/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export default async function register(

// Broadcast Asset Lock transaction
await account.broadcastTransaction(assetLockTransaction);
const assetLockProof = await createAssetLockProof(this, assetLockTransaction);
const assetLockProof = await createAssetLockProof(this, assetLockTransaction, assetLockOutputIndex);

const { identity, identityCreateTransition, identityIndex } = await createIdentityCreateTransition(
this, assetLockTransaction, assetLockOutputIndex, assetLockProof, assetLockPrivateKey
this, assetLockProof, assetLockPrivateKey
);

await broadcastStateTransition(this, identityCreateTransition);
Expand Down
4 changes: 2 additions & 2 deletions src/SDK/Client/Platform/methods/identities/topUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export async function topUp(this: Platform, identityId: Identifier | string, amo
// Broadcast Asset Lock transaction
await account.broadcastTransaction(assetLockTransaction);
// Create a proof for the asset lock transaction
const assetLockProof = await createAssetLockProof(this, assetLockTransaction);
const assetLockProof = await createAssetLockProof(this, assetLockTransaction, assetLockOutputIndex);

// @ts-ignore
const identityTopUpTransition = await createIdentityTopUpTransition(this, assetLockTransaction, assetLockOutputIndex, assetLockProof, assetLockPrivateKey, identityId);
const identityTopUpTransition = await createIdentityTopUpTransition(this, assetLockProof, assetLockPrivateKey, identityId);

// Broadcast ST
await broadcastStateTransition(this, identityTopUpTransition);
Expand Down
4 changes: 3 additions & 1 deletion src/test/mocks/createAndAttachTransportMocksToClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ function makeGetIdentityRespondWithIdentity(client, dapiClientMock) {
let interceptedIdentityStateTransition = await client.platform.dpp.stateTransition.createFromBuffer(stBuffer);

if (interceptedIdentityStateTransition.getType() === stateTransitionTypes.IDENTITY_CREATE) {


let identityToResolve = new Identity({
protocolVersion: interceptedIdentityStateTransition.getProtocolVersion(),
id: interceptedIdentityStateTransition.getIdentityId().toBuffer(),
publicKeys: interceptedIdentityStateTransition.getPublicKeys().map((key) => key.toObject()),
balance: interceptedIdentityStateTransition.getAssetLock().getOutput().satoshis,
balance: interceptedIdentityStateTransition.getAssetLockProof().getOutput().satoshis,
revision: 0,
});
dapiClientMock.platform.getIdentity.withArgs(identityToResolve.getId()).resolves(identityToResolve.toBuffer());
Expand Down

0 comments on commit cf8b73a

Please sign in to comment.