コード例 #1
0
        public void JsonSerialization()
        {
            Cid a    = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4";
            var json = JsonConvert.SerializeObject(a);

            Assert.AreEqual($"\"{a.Encode()}\"", json);
            var b = JsonConvert.DeserializeObject <Cid>(json);

            Assert.AreEqual(a, b);

            a    = null;
            json = JsonConvert.SerializeObject(a);
            b    = JsonConvert.DeserializeObject <Cid>(json);
            Assert.IsNull(b);

            var x = new CidAndX {
                Cid = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4", X = 42
            };

            json = JsonConvert.SerializeObject(x);
            var y = JsonConvert.DeserializeObject <CidAndX>(json);

            Assert.AreEqual(x.Cid, y.Cid);
            Assert.AreEqual(x.X, y.X);

            x.Cid = null;
            json  = JsonConvert.SerializeObject(x);
            y     = JsonConvert.DeserializeObject <CidAndX>(json);
            Assert.AreEqual(x.Cid, y.Cid);
            Assert.AreEqual(x.X, y.X);
        }
コード例 #2
0
        static async Task <Stream> CreateDagProtoBufStreamAsync(
            Cid id,
            IBlockApi blockService,
            KeyChain keyChain,
            CancellationToken cancel)
        {
            var block = await blockService.GetAsync(id, cancel).ConfigureAwait(false);

            var dag = new DagNode(block.DataStream);
            var dm  = Serializer.Deserialize <DataMessage>(dag.DataStream);

            if (dm.Type != DataType.File)
            {
                throw new Exception($"'{id.Encode()}' is not a file.");
            }

            if (dm.Fanout.HasValue)
            {
                throw new NotImplementedException("files with a fanout");
            }

            // Is it a simple node?
            if (dm.BlockSizes == null && !dm.Fanout.HasValue)
            {
                return(new MemoryStream(buffer: dm.Data ?? emptyData, writable: false));
            }

            if (dm.BlockSizes != null)
            {
                return(new ChunkedStream(blockService, keyChain, dag));
            }

            throw new Exception($"Cannot determine the file format of '{id}'.");
        }
コード例 #3
0
        public async Task <Cid> RemoveAsync(Cid id,
                                            bool ignoreNonexistent   = false,
                                            CancellationToken cancel = default)
        {
            if (id.Hash.IsIdentityHash)
            {
                return(id);
            }

            if (await Store.ExistsAsync(id, cancel).ConfigureAwait(false))
            {
                await Store.RemoveAsync(id, cancel).ConfigureAwait(false);

                await PinApi.RemoveAsync(id, false, cancel).ConfigureAwait(false);

                return(id);
            }

            if (ignoreNonexistent)
            {
                return(null);
            }

            throw new KeyNotFoundException($"Block '{id.Encode()}' does not exist.");
        }
コード例 #4
0
        public void Implicit_Conversion_From_V0_String()
        {
            var hash = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4";
            Cid cid  = hash;

            Assert.AreEqual(0, cid.Version);
            Assert.AreEqual("dag-pb", cid.ContentType);
            Assert.AreEqual("base58btc", cid.Encoding);
            Assert.AreEqual(hash, cid.Encode());
        }
コード例 #5
0
        public async Task Remove_Inline_CID()
        {
            var cid = new Cid
            {
                ContentType = "raw",
                Hash        = MultiHash.ComputeHash(blob, "identity")
            };
            var removedCid = await ipfs.Block.RemoveAsync(cid);

            Assert.AreEqual(cid.Encode(), removedCid.Encode());
        }
コード例 #6
0
        public void Immutable()
        {
            Cid cid = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4";

            Assert.AreEqual("QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4", cid.Encode());
            ExceptionAssert.Throws <NotSupportedException>(() => cid.ContentType = "dag-cbor");
            ExceptionAssert.Throws <NotSupportedException>(() => cid.Encoding    = "base64");
            ExceptionAssert.Throws <NotSupportedException>(() =>
                                                           cid.Hash          = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L5");
            ExceptionAssert.Throws <NotSupportedException>(() => cid.Version = 0);
        }
コード例 #7
0
        public void Encode_Upgrade_to_V1_Encoding()
        {
            var cid = new Cid
            {
                Encoding = "base64",
                Hash     = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
            };

            Assert.AreEqual(1, cid.Version);
            Assert.AreEqual("mAXASILlNJ7mTTT4IpS5S19p9q/rEhO/jelOA7pCI96zi783p", cid.Encode());
        }
コード例 #8
0
        public void Encode_V1_Invalid_Encoding()
        {
            var cid = new Cid
            {
                Version     = 1,
                ContentType = "raw",
                Encoding    = "unknown",
                Hash        = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
            };

            Assert.ThrowsException <KeyNotFoundException>(() => cid.Encode());
        }
コード例 #9
0
        public void Decode_Invalid_Version()
        {
            var cid = new Cid
            {
                Version     = 32767,
                ContentType = "raw",
                Encoding    = "base58btc",
                Hash        = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
            };
            var s = cid.Encode();

            Assert.ThrowsException <FormatException>(() => Cid.Decode(s));
        }
コード例 #10
0
        public async Task Stat_Inline_CID()
        {
            var cts = new CancellationTokenSource(300);
            var cid = new Cid
            {
                ContentType = "raw",
                Hash        = MultiHash.ComputeHash(blob, "identity")
            };
            var info = await ipfs.Block.StatAsync(cid, cts.Token);

            Assert.AreEqual(cid.Encode(), (string)info.Id);
            Assert.AreEqual(5, info.Size);
        }
コード例 #11
0
        public async Task Get_Inline_CID()
        {
            var cts = new CancellationTokenSource(300);
            var cid = new Cid
            {
                ContentType = "raw",
                Hash        = MultiHash.ComputeHash(blob, "identity")
            };
            var block = await ipfs.Block.GetAsync(cid, cts.Token);

            Assert.AreEqual(cid.Encode(), block.Id.Encode());
            Assert.AreEqual(blob.Length, block.Size);
            CollectionAssert.AreEqual(blob, block.DataBytes);
        }
コード例 #12
0
        public async Task <Cid> PutAsync(Stream data, string contentType = "dag-pb", string multiHash = "sha2-256", CancellationToken cancel = default(CancellationToken))
        {
            var cid = new Cid
            {
                ContentType = contentType,
                Hash        = MultiHash.ComputeHash(data, multiHash),
                Version     = (contentType == "dag-pb" && multiHash == "sha2-256") ? 0 : 1
            };

            // Store the key in the repository.
            using (var repo = await ipfs.Repository(cancel))
            {
                var block = await repo.BlockInfos
                            .Where(b => b.Cid == cid.Encode())
                            .FirstOrDefaultAsync(cancel);

                if (block != null)
                {
                    log.DebugFormat("Block '{0}' already present", cid);
                    return(cid);
                }

                // TODO: Ineffecient in memory usage.  Might be better to do all
                // the work in the byte[] method.
                var bytes = new byte[data.Length];
                data.Position = 0;
                data.Read(bytes, 0, (int)data.Length);
                var blockInfo = new Repository.BlockInfo
                {
                    Cid      = cid,
                    Pinned   = false,
                    DataSize = data.Length
                };
                var blockValue = new Repository.BlockValue
                {
                    Cid  = cid,
                    Data = bytes
                };
                await repo.AddAsync(blockInfo, cancel);

                await repo.AddAsync(blockValue, cancel);

                await repo.SaveChangesAsync(cancel);

                log.DebugFormat("Added block '{0}'", cid);
            }

            // TODO: Send to bitswap
            return(cid);
        }
コード例 #13
0
        public void Encode_Upgrade_to_V1_Hash()
        {
            var hello = Encoding.UTF8.GetBytes("Hello, world.");
            var mh    = MultiHash.ComputeHash(hello, "sha2-512");
            var cid   = new Cid
            {
                Hash = mh
            };

            Assert.AreEqual(1, cid.Version);
            Assert.AreEqual("base32", cid.Encoding);
            Assert.AreEqual(
                "bafybgqfnbq34ghljwmk7hka7cpem3zybbffnsfzfxinq3qyztsuxcntbxaua23xx42hrgptcchrolkndcucelv3pc4eoarjbwdxagtylboxsm",
                cid.Encode());
        }
コード例 #14
0
        public void Encode_V0()
        {
            var hash = "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V";
            Cid cid  = new MultiHash(hash);

            Assert.AreEqual(hash, cid.Encode());
            Assert.AreEqual(0, cid.Version);

            cid = new Cid
            {
                ContentType = "dag-pb",
                Encoding    = "base58btc",
                Hash        = hash
            };
            Assert.AreEqual(hash, cid.Encode());
            Assert.AreEqual(0, cid.Version);
        }
コード例 #15
0
        public async Task <IDataBlock> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
        {
            using (var repo = await ipfs.Repository(cancel))
            {
                var block = await repo.BlockInfos
                            .Where(b => b.Cid == id.Encode())
                            .FirstOrDefaultAsync(cancel);

                if (block == null) // TODO: call on bitswap
                {
                    return(null);
                }
                return(new DataBlock
                {
                    Id = id,
                    Size = block.DataSize
                });
            }
        }
コード例 #16
0
        public async Task <Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken))
        {
            if (id.Hash.IsIdentityHash)
            {
                return(id);
            }
            if (await Store.ExistsAsync(id, cancel))
            {
                await Store.RemoveAsync(id, cancel);

                await ipfs.Pin.RemoveAsync(id, recursive : false, cancel : cancel);

                return(id);
            }
            if (ignoreNonexistent)
            {
                return(null);
            }
            throw new KeyNotFoundException($"Block '{id.Encode()}' does not exist.");
        }
コード例 #17
0
        public void Encode_V1()
        {
            var cid = new Cid
            {
                Version     = 1,
                ContentType = "raw",
                Encoding    = "base58btc",
                Hash        = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
            };

            Assert.AreEqual("zb2rhj7crUKTQYRGCRATFaQ6YFLTde2YzdqbbhAASkL9uRDXn", cid.Encode());

            cid = new Cid
            {
                ContentType = "raw",
                Hash        = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
            };
            Assert.AreEqual(1, cid.Version);
            Assert.AreEqual("base32", cid.Encoding);
            Assert.AreEqual("bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e", cid.Encode());
        }
コード例 #18
0
        public void Encode_Upgrade_to_V1_ContentType()
        {
            var cid = new Cid
            {
                ContentType = "raw",
                Hash        = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
            };

            Assert.AreEqual(1, cid.Version);
            Assert.AreEqual("base32", cid.Encoding);
            Assert.AreEqual("bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e", cid.Encode());
        }
コード例 #19
0
ファイル: NameApi.cs プロジェクト: avanaur/net-ipfs-api
 public Task <NamedContent> PublishAsync(Cid id, string key = "self", TimeSpan?lifetime = null, CancellationToken cancel = default(CancellationToken))
 {
     return(PublishAsync("/ipfs/" + id.Encode(), false, key, lifetime, cancel));
 }