Esempio n. 1
0
        public void IsP2pkh_Success()
        {
            KeyEncoder keyEncoder = new KeyEncoder(111);

            // Valid
            Assert.Equal(true, keyEncoder.IsP2pkh("mfiCwNxuFYMtb5ytCacgzDAineD2GNCnYo"));
            // Wrong checksum
            Assert.Equal(false, keyEncoder.IsP2pkh("mfiCwNxuFYMtb5ytCacgzDAineD2GNCnYp"));
            // Invalid size
            Assert.Equal(false, keyEncoder.IsP2pkh("2Qx7aDxjSh772"));
            // Empty
            Assert.Equal(false, keyEncoder.IsP2pkh(""));
            // Invalid version byte
            Assert.Equal(false, keyEncoder.IsP2pkh("1CCW1yPxC8meB7JzF8xEwaad4DxksFqhrQ"));
        }
Esempio n. 2
0
        /// <summary>
        /// Parses a permission set from a JSON string.
        /// </summary>
        /// <param name="json">The JSON string to parse.</param>
        /// <param name="path">The path on which these permissions apply.</param>
        /// <param name="keyEncoder">The key encoder to use in the parsed <see cref="Acl"/> objects.</param>
        /// <returns>The parsed list of <see cref="Acl"/> objects.</returns>
        public static IReadOnlyList <Acl> Parse(string json, LedgerPath path, KeyEncoder keyEncoder)
        {
            JArray document = JArray.Parse(json);

            return(((IEnumerable <JToken>)document).Select(root =>
                                                           new Acl(
                                                               ((JArray)root["subjects"]).Children().Select(subject =>
                                                                                                            new P2pkhSubject(((JArray)subject["addresses"]).Select(key => (string)key), (int)subject["required"], keyEncoder)),
                                                               path,
                                                               (bool?)root["recursive"] ?? true,
                                                               new StringPattern((string)root["record_name"] ?? string.Empty, (PatternMatchingStrategy)Enum.Parse(typeof(PatternMatchingStrategy), (string)root["record_name_matching"] ?? "Prefix")),
                                                               new PermissionSet(
                                                                   accountNegative: Parse(root["permissions"]["account_negative"]),
                                                                   accountSpend: Parse(root["permissions"]["account_spend"]),
                                                                   accountModify: Parse(root["permissions"]["account_modify"]),
                                                                   dataModify: Parse(root["permissions"]["data_modify"]))))
                   .ToList());
        }
Esempio n. 3
0
 public P2pkhImplicitLayout(KeyEncoder keyEncoder)
 {
     this.keyEncoder = keyEncoder;
 }
 public DynamicPermissionLayout(IStorageEngine store, KeyEncoder keyEncoder)
 {
     this.store      = store;
     this.keyEncoder = keyEncoder;
 }
Esempio n. 5
0
        public static IMutationValidator CreateRulesValidator(IServiceProvider serviceProvider)
        {
            IConfiguration configuration = serviceProvider.GetService<IConfiguration>().GetSection("validator_mode");
            ILogger logger = serviceProvider.GetService<ILogger>();

            string rootUrl = configuration["root_url"];
            if (rootUrl != null)
            {
                if (!Uri.IsWellFormedUriString(rootUrl, UriKind.Absolute))
                {
                    string errorMessage = $"The server root URL is not a valid URL: '{rootUrl}'. Please make sure it is configured correctly.";
                    throw new InvalidOperationException(errorMessage);
                }

                logger.LogInformation("Current mode: Validator mode");
                logger.LogInformation($"Namespace: {rootUrl}");
                IConfiguration validator = configuration.GetSection("validator");

                switch (validator["type"])
                {
                    case "PermissionBased":
                        byte versionByte = byte.Parse(validator["version_byte"]);
                        KeyEncoder keyEncoder = new KeyEncoder(versionByte);

                        P2pkhSubject[] adminAddresses = validator
                            .GetSection("admin_addresses")
                            .GetChildren()
                            .Select(key => key.Value)
                            .Select(address => new P2pkhSubject(new[] { address }, 1, keyEncoder))
                            .ToArray();

                        List<Acl> pathPermissions = new List<Acl>()
                        {
                            // Admins have full rights
                            new Acl(adminAddresses, LedgerPath.Parse("/"), true, StringPattern.MatchAll, PermissionSet.AllowAll)
                        };

                        foreach (IConfigurationSection section in validator.GetSection("issuers").GetChildren())
                        {
                            LedgerPath assetPath = LedgerPath.Parse(section["path"]);

                            P2pkhSubject[] addresses = section
                                .GetSection("addresses")
                                .GetChildren()
                                .Select(child => child.Value)
                                .Select(address => new P2pkhSubject(new[] { address }, 1, keyEncoder))
                                .ToArray();

                            pathPermissions.Add(new Acl(
                                addresses,
                                assetPath,
                                true,
                                StringPattern.MatchAll,
                                new PermissionSet(accountSpend: Access.Permit, dataModify: Access.Permit)));

                            pathPermissions.Add(new Acl(
                                addresses,
                                assetPath,
                                true,
                                new StringPattern(DynamicPermissionLayout.AclResourceName, PatternMatchingStrategy.Exact),
                                new PermissionSet(dataModify: Access.Deny)));

                            pathPermissions.Add(new Acl(
                                new[] { EveryoneSubject.Instance },
                                assetPath,
                                true,
                                new StringPattern(assetPath.FullPath, PatternMatchingStrategy.Prefix),
                                new PermissionSet(accountModify: Access.Permit)));

                            pathPermissions.Add(new Acl(
                                addresses,
                                LedgerPath.Parse("/"),
                                true,
                                new StringPattern(assetPath.FullPath, PatternMatchingStrategy.Prefix),
                                new PermissionSet(accountNegative: Access.Permit)));
                        }

                        List<IPermissionsProvider> permissionProviders = new List<IPermissionsProvider>();

                        if (bool.Parse(validator["allow_third_party_assets"]))
                            permissionProviders.Add(new P2pkhIssuanceImplicitLayout(keyEncoder));

                        if (bool.Parse(validator["allow_p2pkh_accounts"]))
                            permissionProviders.Add(new P2pkhImplicitLayout(keyEncoder));

                        permissionProviders.Add(new StaticPermissionLayout(pathPermissions));
                        permissionProviders.Add(new DynamicPermissionLayout(serviceProvider.GetRequiredService<IStorageEngine>(), keyEncoder));

                        return new PermissionBasedValidator(permissionProviders);
                    case "Disabled":
                        return ActivatorUtilities.CreateInstance<NullValidator>(serviceProvider, true);
                    default:
                        return null;
                }
            }
            else
            {
                logger.LogInformation("Transaction validation mode disabled (Slave mode)");
                return null;
            }
        }
Esempio n. 6
0
 public P2pkhSubject(IEnumerable <string> addresses, int signaturesRequired, KeyEncoder keyEncoder)
 {
     this.Addresses          = addresses.ToList().AsReadOnly();
     this.SignaturesRequired = signaturesRequired;
     this.keyEncoder         = keyEncoder;
 }
Esempio n. 7
0
 public P2pkhImplicitLayout(KeyEncoder keyEncoder)
 {
     this.keyEncoder = keyEncoder;
 }
 public DynamicPermissionLayout(IStorageEngine store, KeyEncoder keyEncoder)
 {
     this.store = store;
     this.keyEncoder = keyEncoder;
 }
Esempio n. 9
0
 public P2pkhSubject(IEnumerable<string> addresses, int signaturesRequired, KeyEncoder keyEncoder)
 {
     this.Addresses = addresses.ToList().AsReadOnly();
     this.SignaturesRequired = signaturesRequired;
     this.keyEncoder = keyEncoder;
 }
Esempio n. 10
0
        public void GetPubKeyHash_Success()
        {
            KeyEncoder keyEncoder = new KeyEncoder(111);

            Assert.Equal("n12RA1iohYEerfXiBixSoERZG8TP8xQFL2", keyEncoder.GetPubKeyHash(ByteString.Parse("abcdef")));
        }