Пример #1
0
        public Task <byte[]> ForgeOperationGroupAsync(string branch, IEnumerable <ManagerOperationContent> contents)
        {
            var branchBytes  = Base58.Parse(branch, Prefix.B.Length);
            var contentBytes = Bytes.Concat(contents.Select(ForgeOperation).ToArray());

            return(Task.FromResult(branchBytes.Concat(contentBytes)));
        }
Пример #2
0
        public Task <byte[]> ForgeOperationAsync(string branch, OperationContent content)
        {
            var branchBytes  = Base58.Parse(branch, Prefix.B.Length);
            var contentBytes = ForgeOperation(content);

            return(Task.FromResult(branchBytes.Concat(contentBytes)));
        }
Пример #3
0
        static byte[] ForgePublicKey(string value)
        {
            var prefix = value.Substring(0, 4);
            var res    = Base58.Parse(value);

            res = res.GetBytes(4, res.Length - 4);

            switch (prefix)
            {
            case "edpk":
                res = new byte[] { 0 }.Concat(res);
                break;

            case "sppk":
                res = new byte[] { 1 }.Concat(res);
                break;

            case "p2pk":
                res = new byte[] { 2 }.Concat(res);
                break;

            default:
                throw new ArgumentException($"Value address exception. Invalid prefix {prefix}");
            }

            return(res);
        }
Пример #4
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is MichelineString micheStr)
            {
                byte[] res;
                switch (micheStr.Value.Substring(0, 3))
                {
                case "eds":
                case "sps":
                    res = Base58.Parse(micheStr.Value, 5);
                    break;

                case "p2s":
                    res = Base58.Parse(micheStr.Value, 4);
                    break;

                case "sig":
                    res = Base58.Parse(micheStr.Value, 3);
                    break;

                default:
                    throw FormatException(value);
                }
                return(new MichelineBytes(res));
            }

            return(value);
        }
Пример #5
0
        static byte[] ForgeSource(string value)
        {
            var prefix = value.Substring(0, 3);

            var res = Base58.Parse(value);

            res = res.GetBytes(3, res.Length - 3);

            switch (prefix)
            {
            case "tz1":
                res = new byte[] { 0 }.Concat(res);
                break;

            case "tz2":
                res = new byte[] { 1 }.Concat(res);
                break;

            case "tz3":
                res = new byte[] { 2 }.Concat(res);
                break;

            default:
                throw new ArgumentException($"Value address exception. Invalid prefix {prefix}");
            }

            return(res);
        }
Пример #6
0
 static byte[] ForgeInlinedPreendorsement(InlinedPreendorsement op)
 {
     return(Bytes.Concat(
                Base58.Parse(op.Branch, 2),
                ForgePreendorsement(op.Operations),
                Base58.Parse(op.Signature, 3)));
 }
Пример #7
0
        public static byte[] ForgeAddress(string value)
        {
            var prefix = value.Substring(0, 3);
            var res    = Base58.Parse(value, 3);

            switch (prefix)
            {
            case "tz1": return(new byte[] { 0, 0 }.Concat(res));

            case "tz2": return(new byte[] { 0, 1 }.Concat(res));

            case "tz3": return(new byte[] { 0, 2 }.Concat(res));

            case "KT1": return(new byte[] { 1 }.Concat(res).Concat(new byte[] { 0 }));

            case "txr" when value.StartsWith("txr1"): return(new byte[]
                {
                    2
                }

                                                             .Concat(res).Concat(new byte[] { 0 }));

            default:
                throw new ArgumentException($"Invalid address prefix {prefix}");
            }
        }
Пример #8
0
        public static PubKey FromBase58(string base58)
        {
            var curve = Curves.GetCurve(base58.Substring(0, 4));
            var bytes = Base58.Parse(base58, curve.PrivateKeyPrefix);

            return(new PubKey(bytes, curve.Kind, true));
        }
Пример #9
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is MichelineString micheStr)
            {
                var bytes = Base58.Parse(micheStr.Value, 3);
                var res   = new byte[21];

                switch (micheStr.Value.Substring(0, 3))
                {
                case "tz1":
                    res[0] = 0;
                    break;

                case "tz2":
                    res[0] = 1;
                    break;

                case "tz3":
                    res[0] = 2;
                    break;

                default:
                    throw FormatException(value);
                }

                bytes.CopyTo(res, 1);
                return(new MichelineBytes(res));
            }

            return(value);
        }
Пример #10
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is MichelineString micheStr)
            {
                var    bytes = Base58.Parse(micheStr.Value, 4);
                byte[] res;

                switch (micheStr.Value.Substring(0, 4))
                {
                case "edpk":
                    res    = new byte[33];
                    res[0] = 0;
                    break;

                case "sppk":
                    res    = new byte[34];
                    res[0] = 1;
                    break;

                case "p2pk":
                    res    = new byte[34];
                    res[0] = 2;
                    break;

                default:
                    throw FormatException(value);
                }

                bytes.CopyTo(res, 1);
                return(new MichelineBytes(res));
            }

            return(value);
        }
Пример #11
0
        static byte[] ForgeBlockHeader(BlockHeader header)
        {
            var bh1 = new byte[] { };

            bh1 = bh1.Concat(ForgeInt32(header.Level));
            bh1 = bh1.Concat(ForgeInt32(header.Proto, 1));
            bh1 = bh1.Concat(Base58.Parse(header.Predecessor, BranchPrefix));
            var timestamp1 = (int)header.Timestamp.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

            bh1 = bh1.Concat(ForgeLong(timestamp1));
            bh1 = bh1.Concat(ForgeInt32(header.ValidationPass, 1));
            bh1 = bh1.Concat(Base58.Parse(header.OperationsHash, OperationPrefix));
            var fit1 = new byte[] { };

            foreach (var f in header.Fitness)
            {
                fit1 = fit1.Concat(ForgeArray(Hex.Parse(f)));
            }
            bh1 = bh1.Concat(ForgeArray(fit1));
            bh1 = bh1.Concat(Base58.Parse(header.Context, ContextPrefix));
            bh1 = bh1.Concat(ForgeInt32(header.Priority, 2));
            bh1 = bh1.Concat(Hex.Parse(header.ProofOfWorkNonce));
            bh1 = bh1.Concat(ForgeSign(header.Signature));

            return(ForgeArray(bh1));
        }
 static byte[] ForgeTxRollupDispatchTickets(TxRollupDispatchTicketsContent operation)
 {
     return(Bytes.Concat(
                ForgeTag(OperationTag.TxRollupDispatchTickets),
                ForgeTzAddress(operation.Source),
                ForgeMicheNat(operation.Fee),
                ForgeMicheNat(operation.Counter),
                ForgeMicheNat(operation.GasLimit),
                ForgeMicheNat(operation.StorageLimit),
                Base58.Parse(operation.Rollup, Prefix.txr1),
                ForgeInt32(operation.Level),
                Base58.Parse(operation.ContextHash, Prefix.Co),
                ForgeInt32(operation.MessageIndex),
                ForgeArray(operation.MessageResultPath
                           .Select(x => Base58.Parse(x, Prefix.txM))
                           .SelectMany(x => x)
                           .ToArray()),
                ForgeArray(operation.TicketsInfo.SelectMany(ti => Bytes.Concat(
                                                                ForgeArray(ForgeMicheline(ti.Contents)),
                                                                ForgeArray(ForgeMicheline(ti.Type)),
                                                                ForgeAddress(ti.Ticketer),
                                                                ForgeVarLong(ti.Amount),
                                                                ForgeTzAddress(ti.Claimer)))
                           .ToArray())));
 }
Пример #13
0
        public static string GetBlindedAddress(string address, string secret)
        {
            var pkh   = Base58.Parse(address).GetBytes(3, 20);
            var key   = Hex.Parse(secret);
            var blind = Blake2b.ComputeHash(20, key, pkh);

            return(Base58.Convert(blind, Prefix));
        }
Пример #14
0
 static byte[] ForgeInlinedEndorsement(InlinedEndorsement op)
 {
     return(Bytes.Concat(
                Base58.Parse(op.Branch, 2),
                ForgeMicheNat((int)OperationTag.Endorsement),
                ForgeInt32(op.Operations.Level),
                Base58.Parse(op.Signature, 3)));
 }
 static byte[] ForgeEndorsement(EndorsementContent operation)
 {
     return(Bytes.Concat(
                ForgeTag(OperationTag.Endorsement),
                ForgeInt32(operation.Slot, 2),
                ForgeInt32(operation.Level),
                ForgeInt32(operation.Round),
                Base58.Parse(operation.PayloadHash, Prefix.vh)));
 }
Пример #16
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is MichelineString micheStr)
            {
                return(new MichelineBytes(Base58.Parse(micheStr.Value, 3)));
            }

            return(value);
        }
Пример #17
0
 static byte[] ForgeBallot(BallotContent operation)
 {
     return(Bytes.Concat(
                ForgeMicheNat((int)OperationTag.Ballot),
                ForgeTzAddress(operation.Source),
                ForgeInt32(operation.Period),
                Base58.Parse(operation.Proposal, 2),
                new[] { (byte)operation.Ballot }));
 }
Пример #18
0
 static byte[] ForgePreendorsement(PreendorsementContent operation)
 {
     return(Bytes.Concat(
                ForgeMicheNat((int)OperationTag.Preendorsement),
                ForgeInt32(operation.Slot, 2),
                ForgeInt32(operation.Level),
                ForgeInt32(operation.Round),
                Base58.Parse(operation.PayloadHash, Prefix.vh)));
 }
Пример #19
0
        public bool Verify(string message, string signature)
        {
            using (Store.Unlock())
            {
                var messageBytes   = Encoding.UTF8.GetBytes(message);
                var signatureBytes = Base58.Parse(signature, Curve.SignaturePrefix);

                return(Curve.Verify(messageBytes, signatureBytes, Store.Secret));
            }
        }
Пример #20
0
        static byte[] ForgeEndorsementOperation(DoubleEndorsementOperationContent op)
        {
            var op1 = new byte[] { };

            op1 = op1.Concat(Base58.Parse(op.Branch, BranchPrefix));
            op1 = op1.Concat(ForgeNat(OperationTags[op.Operations.Kind]));
            op1 = op1.Concat(ForgeInt32(op.Operations.Level));
            op1 = op1.Concat(Base58.Parse(op.Signature, SigPrefix));
            return(ForgeArray(op1));
        }
Пример #21
0
        public Task <byte[]> ForgeOperationGroupAsync(string branch, IEnumerable <ManagerOperationContent> contents)
        {
            var res = string.IsNullOrWhiteSpace(branch) ? new byte[] {} : Base58.Parse(branch, BranchPrefix);

            foreach (var operation in contents)
            {
                res = res.Concat(ForgeOperation(operation));
            }

            return(Task.FromResult(res));
        }
Пример #22
0
        static byte[] ForgeBallot(BallotContent operation)
        {
            var res = ForgeNat(OperationTags[operation.Kind]);

            res = res.Concat(ForgeSource(operation.Source));
            res = res.Concat(ForgeInt32(operation.Period));
            res = res.Concat(Base58.Parse(operation.Proposal, ProposalPrefix));
            res = res.Concat(new[] { (byte)operation.Ballot });

            return(res);
        }
 static byte[] ForgeTxRollupReturnBond(TxRollupReturnBondContent operation)
 {
     return(Bytes.Concat(
                ForgeTag(OperationTag.TxRollupReturnBond),
                ForgeTzAddress(operation.Source),
                ForgeMicheNat(operation.Fee),
                ForgeMicheNat(operation.Counter),
                ForgeMicheNat(operation.GasLimit),
                ForgeMicheNat(operation.StorageLimit),
                Base58.Parse(operation.Rollup, Prefix.txr1)));
 }
Пример #24
0
 static byte[] ForgeProposals(ProposalsContent operation)
 {
     return(Bytes.Concat(
                ForgeMicheNat((int)OperationTag.Proposals),
                ForgeTzAddress(operation.Source),
                ForgeInt32(operation.Period),
                ForgeArray(operation.Proposals
                           .Select(x => Base58.Parse(x, 2))
                           .SelectMany(x => x)
                           .ToArray())));
 }
Пример #25
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is MichelineString micheStr)
            {
                var address         = micheStr.Value.Length > 36 ? micheStr.Value.Substring(0, 36) : micheStr.Value;
                var addressBytes    = Base58.Parse(address, 3);
                var entrypointBytes = micheStr.Value.Length > 37
                    ? Utf8.Parse(micheStr.Value.Substring(37))
                    : null;

                var res = new byte[22 + (entrypointBytes?.Length ?? 0)];

                switch (address.Substring(0, 3))
                {
                case "tz1":
                    addressBytes.CopyTo(res, 2);
                    res[0] = 0;
                    res[1] = 0;
                    break;

                case "tz2":
                    addressBytes.CopyTo(res, 2);
                    res[0] = 0;
                    res[1] = 1;
                    break;

                case "tz3":
                    addressBytes.CopyTo(res, 2);
                    res[0] = 0;
                    res[1] = 2;
                    break;

                case "KT1":
                    addressBytes.CopyTo(res, 1);
                    res[0]  = 1;
                    res[21] = 0;
                    break;

                default:
                    throw FormatException(value);
                }

                if (entrypointBytes != null)
                {
                    entrypointBytes.CopyTo(res, 22);
                }

                return(new MichelineBytes(res));
            }

            return(value);
        }
Пример #26
0
        public static Key FromBase58(string base58)
        {
            if (base58.Length != 54 && base58.Length != 98)
            {
                throw new ArgumentException($"Base58 string must be 54 or 98 characters length");
            }

            var curve = Curve.FromPrefix(base58.Substring(0, 4));
            var bytes = Base58.Parse(base58, curve.PrivateKeyPrefix);

            return(base58.Length == 54
                ? new Key(bytes, curve.Kind, true)
                : new Key(bytes.GetBytes(0, 32), curve.Kind, true));
        }
 static byte[] ForgeTxRollupSubmitBatch(TxRollupSubmitBatchContent operation)
 {
     return(Bytes.Concat(
                ForgeTag(OperationTag.TxRollupSubmitBatch),
                ForgeTzAddress(operation.Source),
                ForgeMicheNat(operation.Fee),
                ForgeMicheNat(operation.Counter),
                ForgeMicheNat(operation.GasLimit),
                ForgeMicheNat(operation.StorageLimit),
                Base58.Parse(operation.Rollup, Prefix.txr1),
                ForgeArray(operation.Content),
                operation.BurnLimit is long value
             ? Bytes.Concat(ForgeBool(true), ForgeMicheNat(value))
             : ForgeBool(false)));
 }
Пример #28
0
        static byte[] ForgeProposals(ProposalsContent operation)
        {
            var res = ForgeNat(OperationTags[operation.Kind]);

            res = res.Concat(ForgeSource(operation.Source));
            res = res.Concat(ForgeInt32(operation.Period));

            var array = new byte[] {};

            foreach (var proposal in operation.Proposals)
            {
                array = array.Concat(Base58.Parse(proposal, ProposalPrefix));
            }

            return(res.Concat(ForgeArray(array)));
        }
Пример #29
0
 static byte[] ForgeBlockHeader(BlockHeader header)
 {
     return(Bytes.Concat(
                ForgeInt32(header.Level),
                ForgeInt32(header.Proto, 1),
                Base58.Parse(header.Predecessor, 2),
                ForgeInt64(header.Timestamp.ToUnixTime()),
                ForgeInt32(header.ValidationPass, 1),
                Base58.Parse(header.OperationsHash, 3),
                ForgeArray(header.Fitness.Select(x => ForgeArray(Hex.Parse(x))).SelectMany(x => x).ToArray()),
                Base58.Parse(header.Context, 2),
                ForgeInt32(header.Priority, 2),
                Hex.Parse(header.ProofOfWorkNonce),
                ForgeSeedNonce(header.SeedNonceHash),
                Base58.Parse(header.Signature, 3)));
 }
Пример #30
0
        public static byte[] ForgeTzAddress(string value)
        {
            var prefix = value.Substring(0, 3);
            var res    = Base58.Parse(value, 3);

            switch (prefix)
            {
            case "tz1": return(new byte[] { 0 }.Concat(res));

            case "tz2": return(new byte[] { 1 }.Concat(res));

            case "tz3": return(new byte[] { 2 }.Concat(res));

            default:
                throw new ArgumentException($"Invalid source prefix {prefix}");
            }
        }