Skip to content

Commit

Permalink
Allow to specify keypair and matching public key
Browse files Browse the repository at this point in the history
  • Loading branch information
Meta Head committed Jul 26, 2024
1 parent d9f06dd commit d0ad5de
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/near_api/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def initialize(signer_id = ENV.fetch('NEAR_SIGNER_ID'),
key_pair: ENV.fetch('NEAR_KEYPAIR', nil),
public_key: ENV.fetch('NEAR_PUBLIC_KEY', nil))
@signer_id = signer_id
if (!key_pair.nil? && !public_key.nil?) || (key_pair.nil? && public_key.nil?)
raise ArgumentError, 'please specify one of: key_pair or public_key'
if key_pair.nil? && public_key.nil?
raise ArgumentError, 'please specify key_pair or public_key'
end

unless key_pair.nil?
Expand All @@ -31,7 +31,11 @@ def initialize(signer_id = ENV.fetch('NEAR_SIGNER_ID'),
public_key
end
bytestring = NearApi::Base58.decode(key_value)
@public_key = Ed25519::VerifyKey.new(bytestring).to_bytes
verify_key = Ed25519::VerifyKey.new(bytestring).to_bytes
unless @public_key.nil?
raise ArgumentError, 'public_key does not match keypair' if @public_key != verify_key
end
@public_key = verify_key
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/near_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module NearApi
VERSION = '0.2.2'
VERSION = '0.2.3'
end
51 changes: 51 additions & 0 deletions spec/near_api/key_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe NearApi::Key do
describe '#initialize' do
subject(:key) { described_class.new('test.near', key_pair: key_pair, public_key: public_key) }
let(:key_pair) { 'ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw' }
let(:public_key) { nil }

it 'initialize without and error' do
expect { key }.not_to raise_exception
end

context 'with public_key only' do
let(:key_pair) { nil }
let(:public_key) { 'ed25519:22skMptHjFWNyuEWY22ftn2AbLPSYpmYwGJRGwpNHbTV' }

it 'initialize without and error' do
expect { key }.not_to raise_exception
end
end

context 'with keypair and matching public key' do
let(:key_pair) { 'ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw' }
let(:public_key) { 'ed25519:22skMptHjFWNyuEWY22ftn2AbLPSYpmYwGJRGwpNHbTV' }

it 'initialize without and error' do
expect { key }.not_to raise_exception
end
end

context 'when keypair and public_key are missing' do
let(:key_pair) { nil }
let(:public_key) { nil }

it 'raises an error' do
expect { key }.to raise_exception ArgumentError, 'please specify key_pair or public_key'
end
end

context 'when public_key does not match keypair' do
let(:key_pair) { 'ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw' }
let(:public_key) { 'ed25519:BEU6Smwd5nADfTknr4r5E751Me6L4fNL7obQHYbh4FCL' }

it 'raises an error' do
expect { key }.to raise_exception ArgumentError, 'public_key does not match keypair'
end
end
end
end

0 comments on commit d0ad5de

Please sign in to comment.