Exemplo n.º 1
0
        /// <summary>
        /// Generates a new <see cref="RawSignedModel"/> in order to apply for a card registration.
        /// </summary>
        /// <param name="cardParams">The instance of <see cref="CardParams"/> class.
        /// It should has mandatory parameters: identity, public key, private key</param>
        /// <returns>A new self signed instance of <see cref="RawSignedModel"/> class.</returns>
        public RawSignedModel GenerateRawCard(CardParams cardParams)
        {
            ValidateCardParams(cardParams, true);
            var timeNow = DateTime.UtcNow;

            //to truncate milliseconds and microseconds
            timeNow = timeNow.AddTicks(-timeNow.Ticks % TimeSpan.TicksPerSecond);
            var model = RawSignedModelUtils.Generate(CardCrypto, cardParams, timeNow);

            ModelSigner.SelfSign(model, cardParams.PrivateKey, cardParams.ExtraFields);
            return(model);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Publish a new Card using specified <see cref="CardParams"/>.
        /// </summary>
        /// <param name="cardParams">The instance of <see cref="CardParams"/> class.
        /// It should has mandatory parameters: public key, private key</param>
        /// <returns>The instance of newly published <see cref="Card"/> class.</returns>
        public async Task <Card> PublishCardAsync(CardParams cardParams)
        {
            ValidateCardParams(cardParams);

            var tokenContext = new TokenContext(cardParams.Identity, "publish");

            var token = await this.AccessTokenProvider.GetTokenAsync(tokenContext);

            var rawSignedModel = GenerateRawCard(new CardParams()
            {
                Identity       = token.Identity(),
                PrivateKey     = cardParams.PrivateKey,
                PublicKey      = cardParams.PublicKey,
                PreviousCardId = cardParams.PreviousCardId,
                ExtraFields    = cardParams.ExtraFields
            });

            return(await PublishRawSignedModel(rawSignedModel, tokenContext, token));
        }
Exemplo n.º 3
0
        private static void ValidateCardParams(CardParams cardParams, bool validateIdentity = false)
        {
            if (cardParams == null)
            {
                throw new ArgumentNullException(nameof(cardParams));
            }
            if (validateIdentity && String.IsNullOrWhiteSpace(cardParams.Identity))
            {
                throw new ArgumentException($"{nameof(cardParams.Identity)} property is mandatory");
            }
            if (cardParams.PublicKey == null)
            {
                throw new ArgumentException($"{nameof(cardParams.PublicKey)} property is mandatory");
            }

            if (cardParams.PrivateKey == null)
            {
                throw new ArgumentException($"{nameof(cardParams.PrivateKey)} property is mandatory");
            }
        }