Class: EphemeralCalc::Registration

Inherits:
Object
  • Object
show all
Defined in:
lib/ephemeral_calc/registration.rb

Constant Summary collapse

DEFAULT_NAMESPACE =
"e3dd811dd3bbe49e630a"
DEFAULT_ROTATION_EXP =

2^12 = ~68 minutes

12

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, rotation_exp: nil, namespace: nil, instance: nil, beacon_private_key: nil) ⇒ Registration

Returns a new instance of Registration.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ephemeral_calc/registration.rb', line 16

def initialize(name: nil,
               rotation_exp: nil,
               namespace: nil,
               instance: nil,
               beacon_private_key: nil)
  @description = name
  @rotation_exp = (rotation_exp || DEFAULT_ROTATION_EXP).to_i
  @namespace = namespace || DEFAULT_NAMESPACE
  @instance = instance || random_instance
  @beacon_private_key = beacon_private_key || EphemeralCalc::KeyPair.generate_private_key
  @beacon_keypair = EphemeralCalc::KeyPair.new(@beacon_private_key)
end

Instance Attribute Details

#beacon_keypairObject (readonly)

Returns the value of attribute beacon_keypair.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def beacon_keypair
  @beacon_keypair
end

#beacon_nameObject (readonly)

Returns the value of attribute beacon_name.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def beacon_name
  @beacon_name
end

#beacon_private_keyObject (readonly)

Returns the value of attribute beacon_private_key.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def beacon_private_key
  @beacon_private_key
end

#beacon_time_zeroObject (readonly)

Returns the value of attribute beacon_time_zero.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def beacon_time_zero
  @beacon_time_zero
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def description
  @description
end

#instanceObject (readonly)

Returns the value of attribute instance.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def instance
  @instance
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def namespace
  @namespace
end

#rotation_expObject (readonly)

Returns the value of attribute rotation_exp.



7
8
9
# File 'lib/ephemeral_calc/registration.rb', line 7

def rotation_exp
  @rotation_exp
end

Instance Method Details

#api_clientObject



29
30
31
# File 'lib/ephemeral_calc/registration.rb', line 29

def api_client
  @api_client ||= ProximityBeacon::Client.new
end

#as_yamlObject



72
73
74
75
76
77
78
79
# File 'lib/ephemeral_calc/registration.rb', line 72

def as_yaml
  {
    beacon_name: beacon_name,
    identity_key: identity_key,
    rotation_exp: rotation_exp,
    initial_time: encryptor.initial_time,
  }
end

#eid_paramsObject



33
34
35
# File 'lib/ephemeral_calc/registration.rb', line 33

def eid_params
  @eid_params ||= api_client.eidparams
end

#encryptorObject



45
46
47
# File 'lib/ephemeral_calc/registration.rb', line 45

def encryptor
  @encryptor ||= EphemeralCalc::Encryptor.new(identity_key, rotation_exp)
end

#identity_keyObject



41
42
43
# File 'lib/ephemeral_calc/registration.rb', line 41

def identity_key
  beacon_keypair.identity_key(resolver_public_key: resolver_public_key)
end

#initial_eidObject



49
50
51
# File 'lib/ephemeral_calc/registration.rb', line 49

def initial_eid
  [encryptor.get_identifier(0)].pack("H*")
end

#registerObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ephemeral_calc/registration.rb', line 53

def register
  beacon = ProximityBeacon::Beacon.new(
    ephemeral_id_registration: {
      beaconEcdhPublicKey: Base64.strict_encode64(beacon_keypair.public_key),
      serviceEcdhPublicKey: eid_params["serviceEcdhPublicKey"],
      rotationPeriodExponent: rotation_exp,
      initialClockValue: encryptor.beacon_time,
      initialEid: Base64.strict_encode64(initial_eid)
    },
    advertised_id: ProximityBeacon::AdvertisedId.new(
      eddystone_ids: [namespace, instance]
    ),
    status: "ACTIVE",
    description: self.description,
  )
  registered_beacon = api_client.beacons.register(beacon)
  @beacon_name = registered_beacon.name
end

#resolver_public_keyObject



37
38
39
# File 'lib/ephemeral_calc/registration.rb', line 37

def resolver_public_key
  Base64.decode64(eid_params["serviceEcdhPublicKey"])
end

#to_yamlObject



81
82
83
# File 'lib/ephemeral_calc/registration.rb', line 81

def to_yaml
  as_yaml.to_yaml
end