Class: IcAgent::Identity
- Inherits:
-
Object
- Object
- IcAgent::Identity
- Defined in:
- lib/ic_agent/identity.rb
Instance Attribute Summary collapse
-
#der_pubkey ⇒ Object
readonly
Returns the value of attribute der_pubkey.
-
#key_type ⇒ Object
readonly
Returns the value of attribute key_type.
-
#privkey ⇒ Object
readonly
Returns the value of attribute privkey.
-
#pubkey ⇒ Object
readonly
Returns the value of attribute pubkey.
-
#sk ⇒ Object
readonly
Returns the value of attribute sk.
-
#vk ⇒ Object
readonly
Returns the value of attribute vk.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(privkey = '', type = 'ed25519', anonymous = false) ⇒ Identity
constructor
A new instance of Identity.
- #sender ⇒ Object
- #sign(msg) ⇒ Object
- #to_pem ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #verify(msg, sig) ⇒ Object
Constructor Details
#initialize(privkey = '', type = 'ed25519', anonymous = false) ⇒ Identity
Returns a new instance of Identity.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ic_agent/identity.rb', line 14 def initialize(privkey = '', type = 'ed25519', anonymous = false) privkey = [privkey].pack('H*') @anonymous = anonymous if @anonymous return end @key_type = type if type == 'secp256k1' data = privkey.length > 0 ? privkey : Random.new.bytes(32) @sk = Secp256k1::PrivateKey.from_data(data) @privkey = @sk.data.str2hex context = Secp256k1::Context.create @vk = context.key_pair_from_private_key(data) @pubkey = @vk.public_key.uncompressed.str2hex @der_pubkey = "#{IcAgent::IC_PUBKEY_SECP_DER_HERD}#{@pubkey}".hex2str elsif type == 'ed25519' @sk = privkey.length > 0 ? Ed25519::SigningKey.new(privkey) : Ed25519::SigningKey.generate @privkey = @sk.keypair.unpack1('H*')[0..63] @vk = @sk.verify_key @pubkey = @vk.to_bytes.unpack1('H*') @der_pubkey = "#{IcAgent::IC_PUBKEY_ED_DER_HEAD}#{@vk.to_bytes.unpack1('H*')}".hex2str else raise 'unsupported identity type' end end |
Instance Attribute Details
#der_pubkey ⇒ Object (readonly)
Returns the value of attribute der_pubkey.
12 13 14 |
# File 'lib/ic_agent/identity.rb', line 12 def der_pubkey @der_pubkey end |
#key_type ⇒ Object (readonly)
Returns the value of attribute key_type.
12 13 14 |
# File 'lib/ic_agent/identity.rb', line 12 def key_type @key_type end |
#privkey ⇒ Object (readonly)
Returns the value of attribute privkey.
12 13 14 |
# File 'lib/ic_agent/identity.rb', line 12 def privkey @privkey end |
#pubkey ⇒ Object (readonly)
Returns the value of attribute pubkey.
12 13 14 |
# File 'lib/ic_agent/identity.rb', line 12 def pubkey @pubkey end |
#sk ⇒ Object (readonly)
Returns the value of attribute sk.
12 13 14 |
# File 'lib/ic_agent/identity.rb', line 12 def sk @sk end |
#vk ⇒ Object (readonly)
Returns the value of attribute vk.
12 13 14 |
# File 'lib/ic_agent/identity.rb', line 12 def vk @vk end |
Class Method Details
Instance Method Details
#sender ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/ic_agent/identity.rb', line 47 def sender if @anonymous IcAgent::Principal.anonymous else IcAgent::Principal.self_authenticating(@der_pubkey) end end |
#sign(msg) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ic_agent/identity.rb', line 55 def sign(msg) if @anonymous [nil, nil] elsif @key_type == 'ed25519' sig = @sk.sign(msg) [@der_pubkey, sig] elsif @key_type == 'secp256k1' context = Secp256k1::Context.create sig = context.sign(@sk, Digest::SHA256.digest(msg)).compact [@der_pubkey, sig] end end |
#to_pem ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/ic_agent/identity.rb', line 76 def to_pem der = @key_type == 'secp256k1' ? "#{IcAgent::IC_PUBKEY_SECP_DER_HERD}#{@sk.data.unpack1('H*')}".hex2str : "#{IcAgent::IC_PUBKEY_ED_DER_HEAD}#{@sk.to_bytes.unpack1('H*')}".hex2str b64 = Base64.strict_encode64(der) lines = ["-----BEGIN PRIVATE KEY-----\n"] lines.concat(b64.chars.each_slice(64).map(&:join).map { |line| "#{line}\n" }) lines << "-----END PRIVATE KEY-----\n" lines.join end |
#to_s ⇒ Object Also known as: inspect
85 86 87 |
# File 'lib/ic_agent/identity.rb', line 85 def to_s "(#{@key_type}, #{@privkey}, #{@pubkey})" end |
#verify(msg, sig) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/ic_agent/identity.rb', line 68 def verify(msg, sig) if @anonymous false else @vk.verify(sig, msg) end end |