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());
    });
};
import chaiAsPromised from 'chai-as-promised';
import chai, { expect } from 'chai';
import { DidResolver } from '../../../src/did/did-resolver.js';
import { GeneralJwsSigner } from '../../../src/jose/jws/general/signer.js';
import { GeneralJwsVerifier } from '../../../src/jose/jws/general/verifier.js';
import { Jws } from '../../../src/utils/jws.js';
import { signers } from '../../../src/jose/algorithms/signing/signers.js';
import sinon from 'sinon';
const { Ed25519, secp256k1 } = signers;
chai.use(chaiAsPromised);
describe('General JWS Sign/Verify', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    afterEach(() => {
        // restores all fakes, stubs, spies etc. not restoring causes a memory leak.
        // more info here: https://sinonjs.org/releases/v13/general-setup/
        sinon.restore();
    });
    it('should sign and verify secp256k1 signature using a key vector correctly', () => __awaiter(void 0, void 0, void 0, function* () {
        const user = {
            alias: 'bob@mailinator.com',
            id: 'did:dwn:bob#key1',
            token: '1234567890',
            deviceId: '0987654321',
            appId: 3,
            authMode: 5,
        };
        const { privateJwk, publicJwk } = yield secp256k1.generateKeyPair();
        const payloadBytes = new TextEncoder().encode(user);
        const protectedHeader = { alg: 'ES256K', kid: 'did:dwn:alice#key1' };
        const signer = yield GeneralJwsSigner.create(payloadBytes, [{ privateJwk, protectedHeader }]);
        const jws = signer.getJws();
        // console.log('jws', jws);
        const mockResolutionResult = {
            didResolutionMetadata: {},
            didDocument: {
                verificationMethod: [{
                        id: 'did:dwn:alice#key1',
                        type: 'JsonWebKey2020',
                        controller: 'did:dwn:alice',
                        publicKeyJwk: publicJwk
                    }]
            },
            didDocumentMetadata: {}
        };
        const resolverStub = sinon.createStubInstance(DidResolver, {
            // @ts-ignore
            resolve: sinon.stub().withArgs('did:dwn:alice').resolves(mockResolutionResult)
        });
        const verifier = new GeneralJwsVerifier(jws);
        const verificationResult = yield verifier.verify(resolverStub);
        expect(verificationResult.signers.length).to.equal(1);
        expect(verificationResult.signers).to.include('did:dwn:alice');
    }));
    it('should sign and verify ed25519 signature using a key vector correctly', () => __awaiter(void 0, void 0, void 0, function* () {
        const { privateJwk, publicJwk } = yield Ed25519.generateKeyPair();
        const payloadBytes = new TextEncoder().encode('anyPayloadValue');
        const protectedHeader = { alg: 'EdDSA', kid: 'did:dwn:alice#key1' };
        const signer = yield GeneralJwsSigner.create(payloadBytes, [{ privateJwk, protectedHeader }]);
        const jws = signer.getJws();
        const mockResolutionResult = {
            didResolutionMetadata: {},
            didDocument: {
                verificationMethod: [{
                        id: 'did:dwn:alice#key1',
                        type: 'JsonWebKey2020',
                        controller: 'did:dwn:alice',
                        publicKeyJwk: publicJwk
                    }]
            },
            didDocumentMetadata: {}
        };
        const resolverStub = sinon.createStubInstance(DidResolver, {
            // @ts-ignore
            resolve: sinon.stub().withArgs('did:dwn:alice').resolves(mockResolutionResult)
        });
        const verifier = new GeneralJwsVerifier(jws);
        const verificatonResult = yield verifier.verify(resolverStub);
        expect(verificatonResult.signers.length).to.equal(1);
        expect(verificatonResult.signers).to.include('did:dwn:alice');
    }));
    it('should support multiple signatures using different key types', () => __awaiter(void 0, void 0, void 0, function* () {
        const secp256k1Keys = yield secp256k1.generateKeyPair();
        const ed25519Keys = yield Ed25519.generateKeyPair();
        const alice = {
            did: 'did:dwn:alice',
            privateJwk: secp256k1Keys.privateJwk,
            jwkPublic: secp256k1Keys.publicJwk,
            protectedHeader: { alg: 'ES256K', kid: 'did:dwn:alice#key1' },
            mockResolutionResult: {
                didResolutionMetadata: {},
                didDocument: {
                    verificationMethod: [{
                            id: 'did:dwn:alice#key1',
                            type: 'JsonWebKey2020',
                            controller: 'did:dwn:alice',
                            publicKeyJwk: secp256k1Keys.publicJwk
                        }]
                },
                didDocumentMetadata: {}
            }
        };
        const bob = {
            did: 'did:dwn:bob',
            privateJwk: ed25519Keys.privateJwk,
            jwkPublic: ed25519Keys.publicJwk,
            protectedHeader: { alg: 'EdDSA', kid: 'did:dwn:bob#key1' },
            mockResolutionResult: {
                didResolutionMetadata: {},
                didDocument: {
                    verificationMethod: [{
                            id: 'did:dwn:bob#key1',
                            type: 'JsonWebKey2020',
                            controller: 'did:dwn:bob',
                            publicKeyJwk: ed25519Keys.publicJwk,
                        }]
                },
                didDocumentMetadata: {}
            }
        };
        const signatureInputs = [
            { privateJwk: alice.privateJwk, protectedHeader: alice.protectedHeader },
            { privateJwk: bob.privateJwk, protectedHeader: bob.protectedHeader },
        ];
        const payloadBytes = new TextEncoder().encode('anyPayloadValue');
        const signer = yield GeneralJwsSigner.create(payloadBytes, signatureInputs);
        const jws = signer.getJws();
        const resolveStub = sinon.stub();
        resolveStub.withArgs('did:dwn:alice').resolves(alice.mockResolutionResult);
        resolveStub.withArgs('did:dwn:bob').resolves(bob.mockResolutionResult);
        const resolverStub = sinon.createStubInstance(DidResolver, {
            // @ts-ignore
            resolve: resolveStub
        });
        const verifier = new GeneralJwsVerifier(jws);
        const verificatonResult = yield verifier.verify(resolverStub);
        expect(verificatonResult.signers.length).to.equal(2);
        expect(verificatonResult.signers).to.include(alice.did);
        expect(verificatonResult.signers).to.include(bob.did);
    }));
    it('should not verify the same signature more than once', () => __awaiter(void 0, void 0, void 0, function* () {
        const { privateJwk: privateJwkEd25519, publicJwk: publicJwkEd25519 } = yield Ed25519.generateKeyPair();
        const { privateJwk: privateJwkSecp256k1, publicJwk: publicJwkSecp256k1 } = yield secp256k1.generateKeyPair();
        const payloadBytes = new TextEncoder().encode('anyPayloadValue');
        const protectedHeaderEd25519 = { alg: 'EdDSA', kid: 'did:dwn:alice#key1' };
        const protectedHeaderSecp256k1 = { alg: 'ES256K', kid: 'did:dwn:alice#key2' };
        const signer = yield GeneralJwsSigner.create(payloadBytes, [
            { privateJwk: privateJwkEd25519, protectedHeader: protectedHeaderEd25519 },
            { privateJwk: privateJwkSecp256k1, protectedHeader: protectedHeaderSecp256k1 }
        ]);
        const jws = signer.getJws();
        const mockResolutionResult = {
            didResolutionMetadata: {},
            didDocument: {
                verificationMethod: [{
                        id: 'did:dwn:alice#key1',
                        type: 'JsonWebKey2020',
                        controller: 'did:dwn:alice',
                        publicKeyJwk: publicJwkEd25519
                    }, {
                        id: 'did:dwn:alice#key2',
                        type: 'JsonWebKey2020',
                        controller: 'did:dwn:alice',
                        publicKeyJwk: publicJwkSecp256k1
                    }]
            },
            didDocumentMetadata: {}
        };
        const resolverStub = sinon.createStubInstance(DidResolver, {
            // @ts-ignore
            resolve: sinon.stub().withArgs('did:dwn:alice').resolves(mockResolutionResult)
        });
        const verifier = new GeneralJwsVerifier(jws);
        const verifySignatureSpy = sinon.spy(Jws, 'verifySignature');
        const cacheSetSpy = sinon.spy(verifier.cache, 'set');
        yield verifier.verify(resolverStub);
        yield verifier.verify(resolverStub);
        sinon.assert.calledTwice(cacheSetSpy);
        sinon.assert.calledTwice(verifySignatureSpy);
    }));
});
//# sourceMappingURL=general.spec.js.map