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:, rotation_exp: nil, namespace: nil, instance: nil, beacon_private_key: nil) ⇒ Registration

Returns a new instance of Registration.



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

def initialize(name:,
               rotation_exp: nil,
               namespace: nil,
               instance: nil,
               beacon_private_key: nil)
  @name = 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_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

#instanceObject (readonly)

Returns the value of attribute instance.



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

def instance
  @instance
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
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



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

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

#as_yamlObject



70
71
72
73
74
75
76
# File 'lib/ephemeral_calc/registration.rb', line 70

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

#eid_paramsObject



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

def eid_params
  @eid_params ||= api_client.eidparams
end

#encryptorObject



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

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

#identity_keyObject



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

def identity_key
  beacon_keypair.identity_key(resolver_public_key: resolver_public_key)
end

#initial_eidObject



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

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

#registerObject



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

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.name,
  )
  api_client.beacons.register(beacon)
end

#resolver_public_keyObject



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

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

#to_yamlObject



78
79
80
# File 'lib/ephemeral_calc/registration.rb', line 78

def to_yaml
  as_yaml.to_yaml
end