"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const addInvestorUniquenessClaim_1 = require("../addInvestorUniquenessClaim");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
describe('addInvestorUniquenessClaim procedure', () => {
    let mockContext;
    let did;
    let ticker;
    let cddId;
    let scope;
    let scopeId;
    let proof;
    let proofScopeIdWellFormed;
    let firstChallengeResponse;
    let secondChallengeResponse;
    let subtractExpressionsRes;
    let blindedScopeDidHash;
    let scopeClaimProof;
    let expiry;
    let stringToIdentityIdStub;
    let claimToMeshClaimStub;
    let stringToInvestorZkProofDataStub;
    let scopeClaimProofToMeshScopeClaimProofStub;
    let scopeToMeshScopeStub;
    let stringToScopeIdStub;
    let dateToMomentStub;
    let rawDid;
    let rawTicker;
    let rawScope;
    let rawScopeId;
    let rawClaim;
    let rawClaimV2;
    let rawProof;
    let rawScopeClaimProof;
    let rawExpiry;
    let addTransactionStub;
    beforeAll(() => {
        did = 'someDid';
        mocks_1.dsMockUtils.initMocks({ contextOptions: { did } });
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        ticker = 'SOME_ASSET';
        cddId = 'someCddId';
        scope = { type: types_2.ScopeType.Ticker, value: ticker };
        scopeId = 'someScopeId';
        proof = 'someProof';
        proofScopeIdWellFormed = 'someProofScopeIdWellFormed';
        firstChallengeResponse = 'someFirstChallengeResponse';
        secondChallengeResponse = 'someSecondChallengeResponse';
        subtractExpressionsRes = 'someSubtractExpressionsRes';
        blindedScopeDidHash = 'someBlindedScopeDidHash';
        scopeClaimProof = {
            proofScopeIdWellFormed,
            proofScopeIdCddIdMatch: {
                challengeResponses: [firstChallengeResponse, secondChallengeResponse],
                subtractExpressionsRes,
                blindedScopeDidHash,
            },
        };
        expiry = new Date(new Date().getTime() + 10000);
        stringToIdentityIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToIdentityId');
        claimToMeshClaimStub = sinon_1.default.stub(utilsConversionModule, 'claimToMeshClaim');
        stringToInvestorZkProofDataStub = sinon_1.default.stub(utilsConversionModule, 'stringToInvestorZKProofData');
        scopeClaimProofToMeshScopeClaimProofStub = sinon_1.default.stub(utilsConversionModule, 'scopeClaimProofToMeshScopeClaimProof');
        dateToMomentStub = sinon_1.default.stub(utilsConversionModule, 'dateToMoment');
        stringToScopeIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToScopeId');
        scopeToMeshScopeStub = sinon_1.default.stub(utilsConversionModule, 'scopeToMeshScope');
        rawDid = mocks_1.dsMockUtils.createMockIdentityId(did);
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        /* eslint-disable @typescript-eslint/naming-convention */
        rawClaim = mocks_1.dsMockUtils.createMockClaim({
            InvestorUniqueness: [
                mocks_1.dsMockUtils.createMockScope({ Ticker: rawTicker }),
                mocks_1.dsMockUtils.createMockScopeId(ticker),
                mocks_1.dsMockUtils.createMockCddId(cddId),
            ],
        });
        rawClaimV2 = mocks_1.dsMockUtils.createMockClaim({
            InvestorUniquenessV2: mocks_1.dsMockUtils.createMockCddId(cddId),
        });
        rawScope = mocks_1.dsMockUtils.createMockScope({ Ticker: rawTicker });
        rawScopeId = mocks_1.dsMockUtils.createMockScopeId(scopeId);
        rawProof = mocks_1.dsMockUtils.createMockInvestorZKProofData(proof);
        rawScopeClaimProof = mocks_1.dsMockUtils.createMockScopeClaimProof({
            proof_scope_id_wellformed: proofScopeIdWellFormed,
            proof_scope_id_cdd_id_match: {
                subtract_expressions_res: subtractExpressionsRes,
                challenge_responses: [firstChallengeResponse, secondChallengeResponse],
                blinded_scope_did_hash: blindedScopeDidHash,
            },
            scope_id: scopeId,
        });
        /* eslint-enable @typescript-eslint/naming-convention */
        rawExpiry = mocks_1.dsMockUtils.createMockMoment(new bignumber_js_1.default(expiry.getTime()));
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        stringToIdentityIdStub.withArgs(did, mockContext).returns(rawDid);
        claimToMeshClaimStub
            .withArgs({
            type: types_2.ClaimType.InvestorUniqueness,
            scope,
            cddId,
            scopeId,
        }, mockContext)
            .returns(rawClaim);
        claimToMeshClaimStub
            .withArgs({
            type: types_2.ClaimType.InvestorUniquenessV2,
            cddId,
        }, mockContext)
            .returns(rawClaimV2);
        stringToInvestorZkProofDataStub.withArgs(proof, mockContext).returns(rawProof);
        scopeClaimProofToMeshScopeClaimProofStub
            .withArgs(scopeClaimProof, scopeId, mockContext)
            .returns(rawScopeClaimProof);
        dateToMomentStub.withArgs(expiry, mockContext).returns(rawExpiry);
        stringToScopeIdStub.withArgs(scopeId, mockContext).returns(rawScopeId);
        scopeToMeshScopeStub.withArgs(scope, mockContext).returns(rawScope);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should add an add investor uniqueness claim transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const addInvestorUniquenessClaimTransaction = mocks_1.dsMockUtils.createTxStub('identity', 'addInvestorUniquenessClaim');
        yield addInvestorUniquenessClaim_1.prepareAddInvestorUniquenessClaim.call(proc, {
            scope,
            proof,
            cddId,
            scopeId,
            expiry,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction: addInvestorUniquenessClaimTransaction,
            args: [rawDid, rawClaim, rawProof, rawExpiry],
        });
        yield addInvestorUniquenessClaim_1.prepareAddInvestorUniquenessClaim.call(proc, {
            scope,
            proof,
            cddId,
            scopeId,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction: addInvestorUniquenessClaimTransaction,
            args: [rawDid, rawClaim, rawProof, null],
        });
    }));
    test('should add an add investor uniqueness claim v2 transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const addInvestorUniquenessClaimV2Transaction = mocks_1.dsMockUtils.createTxStub('identity', 'addInvestorUniquenessClaimV2');
        yield addInvestorUniquenessClaim_1.prepareAddInvestorUniquenessClaim.call(proc, {
            scope,
            proof: scopeClaimProof,
            cddId,
            scopeId,
            expiry,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction: addInvestorUniquenessClaimV2Transaction,
            args: [rawDid, rawScope, rawClaimV2, rawScopeClaimProof, rawExpiry],
        });
        yield addInvestorUniquenessClaim_1.prepareAddInvestorUniquenessClaim.call(proc, {
            scope,
            proof: scopeClaimProof,
            cddId,
            scopeId,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction: addInvestorUniquenessClaimV2Transaction,
            args: [rawDid, rawScope, rawClaimV2, rawScopeClaimProof, null],
        });
    }));
    test('should throw an error if the expiry date is in the past', () => __awaiter(void 0, void 0, void 0, function* () {
        const commonArgs = {
            scope,
            cddId,
            scopeId,
            expiry: new Date('10/14/1987'),
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield expect(addInvestorUniquenessClaim_1.prepareAddInvestorUniquenessClaim.call(proc, Object.assign(Object.assign({}, commonArgs), { proof }))).rejects.toThrow('Expiry date must be in the future');
        return expect(addInvestorUniquenessClaim_1.prepareAddInvestorUniquenessClaim.call(proc, Object.assign(Object.assign({}, commonArgs), { proof: scopeClaimProof }))).rejects.toThrow('Expiry date must be in the future');
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const commonArgs = {
                scope,
                cddId,
                scopeId,
            };
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = addInvestorUniquenessClaim_1.getAuthorization.bind(proc);
            expect(boundFunc(Object.assign(Object.assign({}, commonArgs), { proof }))).toEqual({
                permissions: {
                    assets: [],
                    transactions: [types_1.TxTags.identity.AddInvestorUniquenessClaim],
                    portfolios: [],
                },
            });
            expect(boundFunc(Object.assign(Object.assign({}, commonArgs), { proof: scopeClaimProof }))).toEqual({
                permissions: {
                    assets: [],
                    transactions: [types_1.TxTags.identity.AddInvestorUniquenessClaimV2],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=addInvestorUniquenessClaim.js.map