Class: Secp256k1::RecoverableSignature

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

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

Compares two recoverable signatures.

Two recoverable signatures their secp256k1_ecdsa_recoverable_signature data is identical.

Parameters:

Returns:

  • (Boolean)

    true if the recoverable signatures are identical, false otherwise.



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1307

static VALUE
RecoverableSignature_equals(VALUE self, VALUE other)
{
  RecoverableSignature *lhs;
  RecoverableSignature *rhs;

  TypedData_Get_Struct(
    self, RecoverableSignature, &RecoverableSignature_DataType, lhs
  );
  TypedData_Get_Struct(
    other, RecoverableSignature, &RecoverableSignature_DataType, rhs
  );

  // NOTE: It is safe to directly compare these data structures rather than
  // first serializing and then comparing.
  if (memcmp(&(lhs->sig),
             &(rhs->sig),
             sizeof(secp256k1_ecdsa_recoverable_signature)) == 0)
  {
    return Qtrue;
  }

  return Qfalse;
}

#compactArray

Returns the compact encoding of recoverable signature.

Returns:

  • (Array)

    first element is the 64 byte compact encoding of signature, the second element is the integer recovery ID.

Raises:



1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1177

static VALUE
RecoverableSignature_compact(VALUE self)
{
  RecoverableSignature *recoverable_signature;
  unsigned char compact_sig[64];
  int recovery_id;
  VALUE result;

  TypedData_Get_Struct(
    self,
    RecoverableSignature,
    &RecoverableSignature_DataType,
    recoverable_signature
  );

  if (secp256k1_ecdsa_recoverable_signature_serialize_compact(
        recoverable_signature->ctx,
        compact_sig,
        &recovery_id,
        &(recoverable_signature->sig)) != 1)
  {
    rb_raise(
      Secp256k1_SerializationError_class,
      "unable to serialize recoverable signature"
    );
    return Qnil;
  }

  // Create a new array with room for 2 elements and push data onto it
  result = rb_ary_new2(2);
  rb_ary_push(result, rb_str_new((char*)compact_sig, 64));
  rb_ary_push(result, rb_int_new(recovery_id));

  return result;
}

#recover_public_key(in_hash32) ⇒ Secp256k1::PublicKey

Attempts to recover the public key associated with this signature.

Parameters:

  • in_hash32 (String)

    32-byte SHA-256 hash of data.

Returns:

Raises:



1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1259

static VALUE
RecoverableSignature_recover_public_key(VALUE self, VALUE in_hash32)
{
  RecoverableSignature *recoverable_signature;
  PublicKey *public_key;
  VALUE result;
  unsigned char *hash32;

  Check_Type(in_hash32, T_STRING);
  if (RSTRING_LEN(in_hash32) != 32)
  {
    rb_raise(Secp256k1_Error_class, "in_hash32 is not 32 bytes in length");
    return Qnil;
  }

  TypedData_Get_Struct(
    self,
    RecoverableSignature,
    &RecoverableSignature_DataType,
    recoverable_signature
  );
  hash32 = (unsigned char*)StringValuePtr(in_hash32);

  result = PublicKey_alloc(Secp256k1_PublicKey_class);
  TypedData_Get_Struct(result, PublicKey, &PublicKey_DataType, public_key);

  if (secp256k1_ecdsa_recover(recoverable_signature->ctx,
                              &(public_key->pubkey),
                              &(recoverable_signature->sig),
                              hash32) == 1)
  {
    return result;
  }

  rb_raise(Secp256k1_DeserializationError_class, "unable to recover public key");
}

#to_signatureSecp256k1::Signature

Convert a recoverable signature to a non-recoverable signature.

Returns:



1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1219

static VALUE
RecoverableSignature_to_signature(VALUE self)
{
  RecoverableSignature *recoverable_signature;
  Signature *signature;
  VALUE result;

  TypedData_Get_Struct(
    self,
    RecoverableSignature,
    &RecoverableSignature_DataType,
    recoverable_signature
  );

  result = Signature_alloc(Secp256k1_Signature_class);
  TypedData_Get_Struct(
    result,
    Signature,
    &Signature_DataType,
    signature
  );

  // NOTE: This method cannot fail
  secp256k1_ecdsa_recoverable_signature_convert(
    recoverable_signature->ctx,
    &(signature->sig),
    &(recoverable_signature->sig));

  return result;
}