Class: Secp256k1::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
ext/rbsecp256k1/rbsecp256k1.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_data(in_public_key_data) ⇒ Secp256k1::PublicKey

Loads a public key from compressed or uncompressed binary data.

Parameters:

  • in_public_key_data (String)

    binary string with compressed or uncompressed public key data.

Returns:

Raises:



567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 567

static VALUE
PublicKey_from_data(VALUE klass, VALUE in_public_key_data)
{
  unsigned char *public_key_data;

  Check_Type(in_public_key_data, T_STRING);

  public_key_data = (unsigned char*)StringValuePtr(in_public_key_data);
  return PublicKey_create_from_data(
    public_key_data,
    (int)RSTRING_LEN(in_public_key_data)
  );
}

Instance Method Details

#==(other) ⇒ Boolean

Compares two public keys.

Public keys are considered equal if their compressed representations match.

Parameters:

Returns:

  • (Boolean)

    true if the public keys are identical, false otherwise.



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 664

static VALUE
PublicKey_equals(VALUE self, VALUE other)
{
  PublicKey *lhs;
  PublicKey *rhs;
  unsigned char lhs_compressed[33];
  unsigned char rhs_compressed[33];
  size_t lhs_len;
  size_t rhs_len;

  lhs_len = 33;
  rhs_len = 33;

  TypedData_Get_Struct(self, PublicKey, &PublicKey_DataType, lhs);
  TypedData_Get_Struct(other, PublicKey, &PublicKey_DataType, rhs);

  secp256k1_ec_pubkey_serialize(
    secp256k1_context_static,
    lhs_compressed,
    &lhs_len,
    &(lhs->pubkey),
    SECP256K1_EC_COMPRESSED
  );
  secp256k1_ec_pubkey_serialize(
    secp256k1_context_static,
    rhs_compressed,
    &rhs_len,
    &(rhs->pubkey),
    SECP256K1_EC_COMPRESSED
  );

  if (lhs_len == rhs_len &&
      memcmp(lhs_compressed, rhs_compressed, lhs_len) == 0)
  {
    return Qtrue;
  }

  return Qfalse;
}

#compressedString

Returns binary string containing the compressed representation of this public key.

Returns:

  • (String)

    binary string containing the compressed representation of this public key.



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 608

static VALUE
PublicKey_compressed(VALUE self)
{
  // TODO: Cache value after first computation
  PublicKey *public_key;
  size_t serialized_pubkey_len = COMPRESSED_PUBKEY_SIZE_BYTES;
  unsigned char serialized_pubkey[COMPRESSED_PUBKEY_SIZE_BYTES];

  TypedData_Get_Struct(self, PublicKey, &PublicKey_DataType, public_key);

  secp256k1_ec_pubkey_serialize(secp256k1_context_static,
                                serialized_pubkey,
                                &serialized_pubkey_len,
                                &(public_key->pubkey),
                                SECP256K1_EC_COMPRESSED);

  return rb_str_new((char*)serialized_pubkey, serialized_pubkey_len);
}

#to_xonlySecp256k1::XOnlyPublicKey

Returns the x-only public key equivalent of this public key.

Returns:



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 632

static VALUE
PublicKey_to_xonly(VALUE self)
{
  PublicKey *public_key;
  XOnlyPublicKey *xonly_pubkey;
  VALUE result;

  TypedData_Get_Struct(self, PublicKey, &PublicKey_DataType, public_key);

  result = XOnlyPublicKey_alloc(Secp256k1_XOnlyPublicKey_class);
  TypedData_Get_Struct(result, XOnlyPublicKey, &XOnlyPublicKey_DataType, xonly_pubkey);

  if (secp256k1_xonly_pubkey_from_pubkey(secp256k1_context_static,
                                         &xonly_pubkey->pubkey,
                                         NULL,
                                         &public_key->pubkey) != 1)
  {
    rb_raise(Secp256k1_Error_class, "failed to convert pubkey to x-only pubkey");
    return Qnil;
  }

  return result;
}

#uncompressedString

Returns binary string containing the uncompressed representation of this public key.

Returns:

  • (String)

    binary string containing the uncompressed representation of this public key.



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 585

static VALUE
PublicKey_uncompressed(VALUE self)
{
  // TODO: Cache value after first computation
  PublicKey *public_key;
  size_t serialized_pubkey_len = UNCOMPRESSED_PUBKEY_SIZE_BYTES;
  unsigned char serialized_pubkey[UNCOMPRESSED_PUBKEY_SIZE_BYTES];

  TypedData_Get_Struct(self, PublicKey, &PublicKey_DataType, public_key);

  secp256k1_ec_pubkey_serialize(secp256k1_context_static,
                                serialized_pubkey,
                                &serialized_pubkey_len,
                                &(public_key->pubkey),
                                SECP256K1_EC_UNCOMPRESSED);

  return rb_str_new((char*)serialized_pubkey, serialized_pubkey_len);
}