Exemple #1
0
    // ////////////////////////////////////////////////////////////////////////////////////////////
    // Contract support

    public async Task <bool> Create(BlkHeader blkHeader, CancellationToken token)
    {
        var documentId = new DocumentId(blkHeader.DocumentId);

        BlockChainModel?model = await Get(documentId, token);

        if (model != null)
        {
            return(false);
        }

        BlockChain blockChain = new BlockChainBuilder()
                                .SetPrincipleId(blkHeader.PrincipalId)
                                .Build();

        blockChain.Add(blkHeader, blkHeader.PrincipalId);

        blockChain = await Sign(blockChain, token);

        if (!blockChain.IsValid())
        {
            throw new InvalidOperationException("Blockchain is invalid");
        }

        await Set(documentId, blockChain.ToBlockChainModel(), token);

        return(true);
    }
    public async Task GivenNoContract_WhenCreated_ShouldVerify()
    {
        ContractClient client = TestApplication.GetContractClient();

        var documentId = new DocumentId("test/unit-tests-smart/contract1");

        var query = new QueryParameter()
        {
            Filter    = "test/unit-tests-smart",
            Recursive = false,
        };

        IReadOnlyList <string> search = (await client.Search(query).ReadNext()).Records;

        if (search.Any(x => x == (string)documentId))
        {
            await client.Delete(documentId);
        }

        var blkHeader = new BlkHeader
        {
            PrincipalId = "dev/user/[email protected]",
            DocumentId  = (string)documentId,
            Creator     = "test",
            Description = "test description",
        };

        await client.Create(blkHeader);

        BlockChainModel model = await client.Get(documentId);

        model.Should().NotBeNull();
        model.Blocks.Should().NotBeNull();
        model.Blocks.Count.Should().Be(2);

        model.Blocks[0].Should().NotBeNull();
        model.Blocks[0].IsValid().Should().BeTrue();

        model.Blocks[1].Should().NotBeNull();
        model.Blocks[1].IsValid().Should().BeTrue();
        model.Blocks[1].DataBlock.Should().NotBeNull();
        model.Blocks[1].DataBlock.BlockType.Should().Be(typeof(BlkHeader).Name);

        bool isValid = await client.Validate(model);

        isValid.Should().BeTrue();


        BatchSet <string> searchList = await client.Search(query).ReadNext();

        searchList.Should().NotBeNull();
        searchList.Records.Any(x => x.EndsWith(documentId.Path)).Should().BeTrue();

        (await client.Delete(documentId)).Should().BeTrue();

        searchList = await client.Search(query).ReadNext();

        searchList.Should().NotBeNull();
        searchList.Records.Any(x => x.EndsWith(documentId.Path)).Should().BeFalse();
    }
Exemple #3
0
    public static void Verify(this BlkHeader subject)
    {
        subject.VerifyNotNull(nameof(subject));
        subject.VerifyBase();

        DocumentId.VerifyId(subject.DocumentId);
        subject.Creator.VerifyNotEmpty(nameof(subject.Creator));
        subject.Description.VerifyNotEmpty(nameof(subject.Description));
    }
    public async Task GivenBlockChain_WhenFile_WillRoundTrip()
    {
        ContractClient client = TestApplication.GetContractClient();

        var documentId = new DocumentId("test/unit-tests-smart/contract1");

        await Delete(documentId, false);

        var blkHeader = new BlkHeader
        {
            PrincipalId = "dev/user/[email protected]",
            DocumentId  = (string)documentId,
            Creator     = "test",
            Description = "test description",
        };

        BlockChain blockChain = new BlockChainBuilder()
                                .SetPrincipleId(blkHeader.PrincipalId)
                                .Build()
                                .Add(blkHeader, blkHeader.PrincipalId);

        BlockChainModel signedBlockChainModel = await client.Sign(blockChain.ToBlockChainModel());

        signedBlockChainModel.Should().NotBeNull();

        await client.Set(documentId, signedBlockChainModel);

        BlockChainModel readBlockChainModel = await client.Get(documentId);

        readBlockChainModel.Should().NotBeNull();

        readBlockChainModel.Blocks.Count.Should().Be(signedBlockChainModel.Blocks.Count);
        readBlockChainModel.Blocks
        .Zip(signedBlockChainModel.Blocks)
        .All(x => x.First == x.Second)
        .Should().BeTrue();

        bool isValid = await client.Validate(documentId);

        isValid.Should().BeTrue();

        isValid = await client.Validate(readBlockChainModel);

        isValid.Should().BeTrue();

        await Delete(documentId, true);
    }
Exemple #5
0
        public async Task <IActionResult> Create([FromBody] Document entry, CancellationToken token)
        {
            if (!entry.IsHashVerify())
            {
                return(BadRequest());
            }

            switch (entry.ObjectClass)
            {
            case "BlkHeader":
                BlkHeader blkHeader = entry.DeserializeData <BlkHeader>();
                await _contractService.Create(blkHeader, token);

                break;

            default:
                return(BadRequest());
            }

            return(Ok());
        }
    public async Task GivenBlockChain_WhenSigned_FailedWithWrongSignature()
    {
        ContractClient client = TestApplication.GetContractClient();

        var documentId = new DocumentId("test/unit-tests-smart/contract1");

        var blkHeader = new BlkHeader
        {
            PrincipalId = "dev/user/[email protected]",
            DocumentId  = (string)documentId,
            Creator     = "test",
            Description = "test description",
        };

        BlockChain blockChain = new BlockChainBuilder()
                                .SetPrincipleId(blkHeader.PrincipalId)
                                .Build()
                                .Add(blkHeader, blkHeader.PrincipalId);

        BlockChainModel signedBlockChainModel = await client.Sign(blockChain.ToBlockChainModel());

        signedBlockChainModel.Should().NotBeNull();

        bool isValid = await client.Validate(signedBlockChainModel);


        // Modify signature
        signedBlockChainModel.Blocks[1] = signedBlockChainModel.Blocks[1] with {
            DataBlock = signedBlockChainModel.Blocks[1].DataBlock with {
                JwtSignature = "junk"
            }
        };

        isValid = await client.Validate(signedBlockChainModel);

        isValid.Should().BeFalse();
    }