class AccountKey::Key

Public Instance Methods

close() click to toggle source
static VALUE account_close(VALUE self) {
    convex_account_p account = NULL;
    Data_Get_Struct(self, convex_account_t , account);
    if (!account) {
        return 0;
    }
    convex_account_close((void *) account);
    return self;
}
export_to_text(p1) click to toggle source
static VALUE account_export_to_text(VALUE self, VALUE password) {

    VALUE result = 0;

    convex_account_p account = NULL;
    Data_Get_Struct(self, convex_account_t , account);
    if (!account) {
        return 0;
    }

    if (!account->key) {
      return 0;
    }

    // make sure the password is a string
    Check_Type(password, T_STRING);
    char *password_ptr = StringValueCStr(password);

    const EVP_CIPHER *cipher = EVP_aes_256_cfb();
    if (!cipher) {
        return 0;
    }

    BIO *memory = BIO_new(BIO_s_mem());

    // char *kstr = strdup(password);
    PEM_write_bio_PKCS8PrivateKey(memory, account->key, cipher, NULL, 0, 0, (void *)password_ptr);
    // free(kstr);

    BUF_MEM *bptr;
    long data_length = BIO_get_mem_data(memory, &bptr);

    result = rb_str_new((const char *)bptr, data_length);

    BIO_free_all(memory);
    return result;
}
public_key() click to toggle source
static VALUE account_public_key(VALUE self) {
    convex_account_p account = NULL;
    Data_Get_Struct(self, convex_account_t , account);
    VALUE result = 0;
    if(!account) {
        return 0;
    }
    char buffer[(CONVEX_ACCOUNT_PUBLIC_KEY_LENGTH * 2) + 1];
    int buffer_length = sizeof(buffer);
    convex_utils_public_key_to_hex(account->public_key, CONVEX_ACCOUNT_PUBLIC_KEY_LENGTH, buffer, &buffer_length);
    result = rb_str_new_cstr(buffer);
    return result;
}
sign(p1) click to toggle source
static VALUE convex_account_sign_data(VALUE self, VALUE hash_hex) {

    VALUE result = 0;

    convex_account_p account = NULL;
    Data_Get_Struct(self, convex_account_t , account);
    if (!account) {
        return 0;
    }

    if (!account->key) {
      return 0;
    }

    char *hash_hex_ptr = StringValueCStr(hash_hex);
    int hash_buffer_size = strlen(hash_hex_ptr) / 2;
    if ( hash_buffer_size > 1024) {
        // buffer to big
        return 0;
    }
    unsigned char *hash_buffer = malloc(hash_buffer_size);
    convex_utils_hex_to_bytes(hash_hex_ptr, hash_buffer, &hash_buffer_size);


    size_t sign_length = EVP_PKEY_size(account->key);
    unsigned char *sign_buffer = malloc(sign_length);

    EVP_PKEY_CTX *key_ctx = NULL;
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    if (!ctx) {
        return 0;
    }

    EVP_DigestSignInit(ctx, &key_ctx, NULL, NULL, account->key);
    EVP_DigestSign(ctx, sign_buffer, &sign_length, hash_buffer, hash_buffer_size);
    EVP_MD_CTX_free(ctx);
    free(hash_buffer);

    int sign_hex_buffer_length = (sign_length * 2) + 1;
    char *sign_hex_buffer = malloc(sign_hex_buffer_length);
    convex_utils_bytes_to_hex(sign_buffer, sign_length, sign_hex_buffer, &sign_hex_buffer_length);

    free(sign_buffer);
    result = rb_str_new_cstr(sign_hex_buffer);

    free(sign_hex_buffer);

    return result;
}