Example #1
0
        protected override Memory <byte> ParseUtxoConfidential(ushort version, Memory <byte> spanBody, out UtxoConfidentialBase utxoConfidentialBase)
        {
            UtxoConfidentialBase block = null;

            if (version == 1)
            {
                int readBytes = 0;

                ReadCommitmentAndProof(ref spanBody, ref readBytes, out byte[] assetCommitment, out SurjectionProof surjectionProof);
                ReadCommitmentAndProof(ref spanBody, ref readBytes, out byte[] affiliationAssetCommitment, out SurjectionProof affiliationSurjectionProof);

                ushort affiliationKeysCount = BinaryPrimitives.ReadUInt16LittleEndian(spanBody.Span.Slice(readBytes));
                readBytes += sizeof(ushort);

                byte[][] affiliationKeys = new byte[affiliationKeysCount][];
                for (int i = 0; i < affiliationKeysCount; i++)
                {
                    affiliationKeys[i] = spanBody.Slice(readBytes, Globals.NODE_PUBLIC_KEY_SIZE).ToArray();
                    readBytes         += Globals.NODE_PUBLIC_KEY_SIZE;
                }

                ReadBorromeanRingSignature(ref spanBody, ref readBytes, out BorromeanRingSignature borromeanRingSignature);

                ReadSurjectionProof(ref spanBody, ref readBytes, out SurjectionProof surjectionEvidenceProof);

                ReadEcdhTupleCA(ref spanBody, ref readBytes, out EcdhTupleCA ecdhTuple);

                block = new TransitionAffiliatedAssetTransfer
                {
                    AssetCommitment                    = assetCommitment,
                    SurjectionProof                    = surjectionProof,
                    AffiliationCommitment              = affiliationAssetCommitment,
                    AffiliationSurjectionProof         = affiliationSurjectionProof,
                    AffiliationKeys                    = affiliationKeys,
                    AffiliationBorromeanSignature      = borromeanRingSignature,
                    AffiliationEvidenceSurjectionProof = surjectionEvidenceProof,
                    EcdhTuple = ecdhTuple
                };

                utxoConfidentialBase = block;

                return(spanBody.Slice(readBytes));
            }

            throw new BlockVersionNotSupportedException(version, BlockType);
        }
Example #2
0
        public void TransitionAssetTransferParserTest()
        {
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash = BinaryHelper.GetPowHash(1234);
            ushort version = 1;

            byte[] body;
            byte[] transactionPublicKey = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] destinationKey       = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] destinationKey2      = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] keyImage             = BinaryHelper.GetRandomPublicKey();
            byte[] assetCommitment      = ConfidentialAssetsHelper.GetRandomSeed();
            ushort pubKeysCount         = 10;

            byte[][] assetCommitments = new byte[pubKeysCount][];
            byte[][] pubKeys          = new byte[pubKeysCount][];

            byte[] secretKey      = null;
            ushort secretKeyIndex = 5;

            byte[]   e = ConfidentialAssetsHelper.GetRandomSeed();
            byte[][] s = new byte[pubKeysCount][];


            ushort affiliationCommitmentsCount = 7;

            byte[][] affiliationCommitments = new byte[affiliationCommitmentsCount][];
            byte[]   affiliationE           = ConfidentialAssetsHelper.GetRandomSeed();
            byte[][] affiliationS           = new byte[affiliationCommitmentsCount][];

            byte[] mask = ConfidentialAssetsHelper.GetRandomSeed();

            for (int i = 0; i < pubKeysCount; i++)
            {
                pubKeys[i] = BinaryHelper.GetRandomPublicKey(out byte[] secretKeyTemp);
                if (i == secretKeyIndex)
                {
                    secretKey = secretKeyTemp;
                }
                assetCommitments[i] = ConfidentialAssetsHelper.GetRandomSeed();
                s[i] = ConfidentialAssetsHelper.GetRandomSeed();
            }

            for (int i = 0; i < affiliationCommitmentsCount; i++)
            {
                affiliationCommitments[i] = ConfidentialAssetsHelper.GetRandomSeed();
                affiliationS[i]           = ConfidentialAssetsHelper.GetRandomSeed();
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(assetCommitment);

                    bw.Write(pubKeysCount);
                    for (int i = 0; i < pubKeysCount; i++)
                    {
                        bw.Write(assetCommitments[i]);
                    }
                    bw.Write(e);
                    for (int i = 0; i < pubKeysCount; i++)
                    {
                        bw.Write(s[i]);
                    }

                    bw.Write(affiliationCommitmentsCount);
                    for (int i = 0; i < affiliationCommitmentsCount; i++)
                    {
                        bw.Write(affiliationCommitments[i]);
                    }
                    bw.Write(affiliationE);
                    for (int i = 0; i < affiliationCommitmentsCount; i++)
                    {
                        bw.Write(affiliationS[i]);
                    }

                    bw.Write(mask);
                    bw.Write(assetCommitment);
                }

                body = ms.ToArray();
            }

            byte[] packet = BinaryHelper.GetUtxoConfidentialPacket(PacketType.UtxoConfidential, syncBlockHeight, nonce, powHash, version,
                                                                   BlockTypes.UtxoConfidential_NonQuantitativeAssetTransfer, keyImage, destinationKey, destinationKey2, transactionPublicKey, body, pubKeys, secretKey, secretKeyIndex,
                                                                   out RingSignature[] ringSignatures);
            TransitionAssetTransferParser     parser = new TransitionAssetTransferParser(_identityKeyProvidersRegistry);
            TransitionAffiliatedAssetTransfer block  = (TransitionAffiliatedAssetTransfer)parser.Parse(packet);

            Assert.Equal(syncBlockHeight, block.SyncBlockHeight);
            Assert.Equal(nonce, block.Nonce);
            Assert.Equal(powHash, block.PowHash);
            Assert.Equal(version, block.Version);
            Assert.Equal(keyImage, block.KeyImage.Value.ToArray());
            Assert.Equal(destinationKey, block.DestinationKey);
            Assert.Equal(destinationKey2, block.DestinationKey2);
            Assert.Equal(transactionPublicKey, block.TransactionPublicKey);
            Assert.Equal(assetCommitment, block.AssetCommitment);
            Assert.Equal(pubKeysCount, block.SurjectionProof.AssetCommitments.Length);

            for (int i = 0; i < pubKeysCount; i++)
            {
                Assert.Equal(assetCommitments[i], block.SurjectionProof.AssetCommitments[i]);
                Assert.Equal(s[i], block.SurjectionProof.Rs.S[i]);
                Assert.Equal(ringSignatures[i].C, block.Signatures[i].C);
                Assert.Equal(ringSignatures[i].R, block.Signatures[i].R);
            }

            Assert.Equal(e, block.SurjectionProof.Rs.E);
            Assert.Equal(mask, block.EcdhTuple.Mask);
            Assert.Equal(assetCommitment, block.EcdhTuple.AssetId);
        }