class IcAgent::Identity
Attributes
Public Class Methods
Creates a new Identity
instance from a seed phrase (mnemonic).
Parameters:
-
mnemonic: The seed phrase (mnemonic) used to generate the identity.
Returns: The Identity
instance.
# File lib/ic_agent/identity.rb, line 52 def self.from_seed(mnemonic) seed = Bitcoin::Trezor::Mnemonic.to_seed(mnemonic) privkey = seed[0..63] key_type = 'ed25519' Identity.new(privkey = privkey, type = key_type) end
Initializes a new instance of the Identity
class.
Parameters:
-
privkey: The private key of the identity in hexadecimal format. Defaults to an empty string.
-
type: The key type of the identity. Defaults to 'ed25519'.
-
anonymous: A flag indicating whether the identity is anonymous. Defaults to false.
# File lib/ic_agent/identity.rb, line 20 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
Public Instance Methods
Signs a message using the Identity
.
Parameters:
-
msg: The message to sign.
Returns: An array containing the DER-encoded public key and the signature.
# File lib/ic_agent/identity.rb, line 76 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
Returns the PEM-encoded private key of the Identity
.
Returns: The PEM-encoded private key.
# File lib/ic_agent/identity.rb, line 107 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
# File lib/ic_agent/identity.rb, line 116 def to_s "(#{@key_type}, #{@privkey}, #{@pubkey})" end
Verifies a message signature using the Identity
.
Parameters:
-
msg: The message to verify.
-
sig: The signature to verify.
Returns: `true` if the signature is valid, otherwise `false`.
# File lib/ic_agent/identity.rb, line 96 def verify(msg, sig) if @anonymous false else @vk.verify(sig, msg) end end