Module: NanocurrencyExt

Defined in:
ext/nanocurrency_ext/rbext.c

Class Method Summary collapse

Class Method Details

.compute_work(rbHash) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'ext/nanocurrency_ext/rbext.c', line 140

VALUE nanocurrency_compute_work(VALUE self, VALUE rbHash) {

  Check_Type(rbHash, T_STRING);
  uint8_t block_hash_bytes[BLOCK_HASH_LENGTH];
  const char *hex = RSTRING_PTR(rbHash);
  hex_to_bytes(hex, block_hash_bytes);

  char stack_string[WORK_LENGTH * 2];
  uint8_t work_[WORK_LENGTH];
  int res = work(block_hash_bytes, 0, 1, work_);
  bytes_to_hex(work_, WORK_LENGTH, stack_string);

  if (res == 0) {
    return rb_str_new(stack_string, WORK_LENGTH * 2);
  } else {
    return Qnil;
  }
}

.public_key(rbSecret_key) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/nanocurrency_ext/rbext.c', line 99

VALUE nanocurrency_public_key(VALUE self, VALUE rbSecret_key) {
  Check_Type(rbSecret_key, T_STRING);
  if (RSTRING_LEN(rbSecret_key) != 32) {
    rb_raise(rb_eArgError, "The secret key must be 32 bytes in length");
    return Qnil;
  }

  unsigned char *secret_key = RSTRING_PTR(rbSecret_key);

  ed25519_public_key public_key;
  ed25519_publickey(secret_key, public_key);

  return rb_str_new(public_key, 32);
}

.sign(rbHash, rbSecret_key) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/nanocurrency_ext/rbext.c', line 114

VALUE nanocurrency_sign(VALUE self, VALUE rbHash, VALUE rbSecret_key) {
  Check_Type(rbSecret_key, T_STRING);
  Check_Type(rbHash, T_STRING);

  if (RSTRING_LEN(rbSecret_key) != 32) {
    rb_raise(rb_eArgError, "The secret key must be 32 bytes in length");
    return Qnil;
  }

  if (RSTRING_LEN(rbHash) != 32) {
    rb_raise(rb_eArgError, "The hash must be 32 bytes in length");
    return Qnil;
  }

  unsigned char *secret_key = RSTRING_PTR(rbSecret_key);
  unsigned char *message = RSTRING_PTR(rbHash);
  ed25519_public_key public_key;
  ed25519_publickey(secret_key, public_key);
  size_t message_len = 32;

  ed25519_signature sig;
  ed25519_sign(message, message_len, secret_key, public_key, sig);

  return rb_str_new(sig, 64);
}