mailcycle 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 335f0b7efcc3477484330570f7429ab5b02110bddcf744da2c22b5fc924b7dbe
4
- data.tar.gz: c119a992721686b72afa4973aa0504066d0b41cdc6c20bad031b5fce460fd1e0
3
+ metadata.gz: af0a5621cd2d8fd6f9c31bbf137463ebde367f530c06b5b285fbc81190b13660
4
+ data.tar.gz: 9da1a431696d248b521ed612c635843d62d257586ea44df19198617c17b15670
5
5
  SHA512:
6
- metadata.gz: c41cce333ffe2bf99e02528ee4e95ddefea75d268d97c22a5a18474ddacb72bc6690d893e15eb6052c77f570cd2f0a99f285d490a19ae79fd19ce2b280bcf877
7
- data.tar.gz: 682bb34d733d5872529ff1bb80e223e419f52cb63e755f74e9e0f4f35512eab22b0dccd896f7123c5a3732cab1eb79d47b5cf72cd5278a0eef242e2a114c1090
6
+ metadata.gz: 18f3215df579c6628e68b5a8f8a4ac0a78a8533c6999bdf4cc603da75ba50f0daedc4adc5f09ff2506f2199a23b0e1df8f410d991ad912f9c76a87b42e138852
7
+ data.tar.gz: 158998c0cf0ce99ba62ac67b08edf8a22089d610f738d71695db23646a30d190cd97cd9f9f52fd4dcd4d633be77a60998e392d0dc4df0293855b70bcf3d839d3
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  A Ruby client for the Mailcycle API that does the encryption on your machine. Create addresses that receive mail, read what arrives, send, and listen for events. Mailcycle stores mail sealed to keys derived from your recovery phrase, and this gem derives them locally; the phrase never leaves your machine.
4
4
 
5
+ Home page and examples: https://mailcycle.email/sdks/ruby/
6
+
7
+ Guide: https://docs.mailcycle.email/guides/sdk-ruby/
8
+
5
9
  Needs Ruby 3.1 or later. It has no dependencies beyond Ruby's own standard library.
6
10
 
7
11
  ```sh
@@ -36,6 +40,21 @@ puts address.email_address
36
40
 
37
41
  The address's public key is derived from the phrase and sent with it, so the mail server can seal what arrives. The label is sealed on your machine, and the server stores it as ciphertext.
38
42
 
43
+ ## Export and rotate an address key
44
+
45
+ ```ruby
46
+ exported = mc.addresses.export_key(address.id) # the current key epoch
47
+ old = mc.addresses.export_key(address.id, 0) # or any epoch from 0 to the current one
48
+ puts exported.private_key # base64url, 32 bytes
49
+ puts exported.public_key
50
+
51
+ mc.addresses.rotate_key(address.id) # => #<struct AddressRotation address_id=..., epoch=1>
52
+ ```
53
+
54
+ `export_key` returns one address's private key, to give a helper that reads that one address's mail, such as the [decrypt Worker](https://docs.mailcycle.email/guides/decrypt-worker/) for n8n, Make, Zapier and Pipedream. It opens that address's mail and nothing else, so treat it like the mail itself. The export is recorded in the account's history before the key is derived, and only the address id is sent. An epoch outside 0 to the current one raises `invalid_epoch`.
55
+
56
+ `rotate_key` moves the address to its next key epoch, so an exported key stops opening new mail, and hands the new key to the device or agent that holds the address. An address shared with team members raises `team_address`: rotate it in the web app, which also gives each member the new key. Both need the phrase and a signed-in session.
57
+
39
58
  ## Reading mail
40
59
 
41
60
  ```ruby
@@ -205,4 +224,4 @@ The encryption is checked against Mailcycle's published test vectors (https://do
205
224
 
206
225
  ## Licence
207
226
 
208
- Apache License 2.0. Copyright (c) 2026 Northlab Studios Ltd. You may use, modify and redistribute this gem, including inside your own applications, under the terms in [LICENSE](LICENSE). Using the Mailcycle API itself is governed by the [Terms of Service](https://mailcycle.email/legal/terms/). Never ship an API key in software you distribute.
227
+ Apache License 2.0. Copyright (c) 2026 Northlab Studios Ltd. You may use, modify and redistribute this gem, including inside your own applications, under the terms in the LICENSE file included with it. Using the Mailcycle API itself is governed by the [Terms of Service](https://mailcycle.email/legal/terms/). Never ship an API key in software you distribute.
@@ -81,8 +81,7 @@ module Mailcycle
81
81
  # that this machine holds the matching private one.
82
82
  #
83
83
  # Every constructor takes the same options: `api_url` (the live API by
84
- # default), `passphrase` (empty unless the account was made with one) and
85
- # `timeout` in seconds (30).
84
+ # default) and `timeout` in seconds (30).
86
85
  class Client
87
86
  attr_reader :http, :keys
88
87
 
@@ -96,8 +95,8 @@ module Mailcycle
96
95
  # The same challenge-response the app uses, in which nothing secret
97
96
  # crosses the wire. Sessions last 30 days; keep `session_token` and pass
98
97
  # it to `resume` rather than deriving the keys again every run.
99
- def self.sign_in(phrase, api_url: Http::DEFAULT_API_URL, passphrase: "", timeout: Http::DEFAULT_TIMEOUT_SECONDS)
100
- keys = Keys.from_phrase(phrase, passphrase)
98
+ def self.sign_in(phrase, api_url: Http::DEFAULT_API_URL, timeout: Http::DEFAULT_TIMEOUT_SECONDS)
99
+ keys = Keys.from_phrase(phrase)
101
100
  http = Http.new(api_url, nil, timeout)
102
101
 
103
102
  challenge = Wire.object(http.request("POST", "/accounts/challenge", body: { "accountId" => keys.account_id }))
@@ -116,9 +115,9 @@ module Mailcycle
116
115
  #
117
116
  # The phrase is still needed to open mail, and is checked against the
118
117
  # session's account before it is used.
119
- def self.resume(session_token, phrase, api_url: Http::DEFAULT_API_URL, passphrase: "",
118
+ def self.resume(session_token, phrase, api_url: Http::DEFAULT_API_URL,
120
119
  timeout: Http::DEFAULT_TIMEOUT_SECONDS)
121
- keys = Keys.from_phrase(phrase, passphrase)
120
+ keys = Keys.from_phrase(phrase)
122
121
  client = new(Http.new(api_url, session_token, timeout), keys, api_key: false)
123
122
  client.__send__(:check_phrase_matches, "session")
124
123
  client
@@ -128,9 +127,9 @@ module Mailcycle
128
127
  #
129
128
  # With the phrase as well, the client can open mail and create addresses
130
129
  # that receive it; the phrase is checked against the key's account first.
131
- def self.with_api_key(api_key, phrase: nil, api_url: Http::DEFAULT_API_URL, passphrase: "",
130
+ def self.with_api_key(api_key, phrase: nil, api_url: Http::DEFAULT_API_URL,
132
131
  timeout: Http::DEFAULT_TIMEOUT_SECONDS)
133
- keys = phrase.nil? ? nil : Keys.from_phrase(phrase, passphrase)
132
+ keys = phrase.nil? ? nil : Keys.from_phrase(phrase)
134
133
  client = new(Http.new(api_url, api_key, timeout), keys, api_key: true)
135
134
  client.__send__(:check_phrase_matches, "API key") if keys
136
135
  client
@@ -140,10 +139,10 @@ module Mailcycle
140
139
  #
141
140
  # Keep the phrase. Nothing can recover the account without it, and nobody
142
141
  # at Mailcycle can help.
143
- def self.create_account(phrase = nil, api_url: Http::DEFAULT_API_URL, passphrase: "",
142
+ def self.create_account(phrase = nil, api_url: Http::DEFAULT_API_URL,
144
143
  timeout: Http::DEFAULT_TIMEOUT_SECONDS)
145
144
  words = phrase || generate_phrase
146
- keys = Keys.from_phrase(words, passphrase)
145
+ keys = Keys.from_phrase(words)
147
146
  http = Http.new(api_url, nil, timeout)
148
147
  session = Wire.object(http.request("POST", "/accounts", body: {
149
148
  "accountId" => keys.account_id,
@@ -419,6 +418,58 @@ module Mailcycle
419
418
  nil
420
419
  end
421
420
 
421
+ # One address's private key at a key epoch (the current one unless `epoch`
422
+ # is given), base64url. It opens that address's mail and nothing else: it
423
+ # cannot sign in, create keys or send. Treat it as the address's mail itself.
424
+ #
425
+ # Needs the phrase and a signed-in session. The export is recorded in the
426
+ # account's history before the key is derived; only the address id is
427
+ # sent. The key is derived on this machine.
428
+ def export_key(address_id, epoch = nil)
429
+ keys = @client.require_keys("export an address key")
430
+ inbox = find_inbox(address_id)
431
+ current = Mail.key_epoch(inbox)
432
+ at = epoch.nil? ? current : epoch
433
+ unless at.is_a?(Integer) && at.between?(0, current)
434
+ raise ApiError.new(400, "invalid_epoch", "That address has key epochs 0 to #{current}.")
435
+ end
436
+
437
+ @client.http.request("POST", "/inboxes/#{Mailcycle.path_segment(address_id)}/key-exported", body: {})
438
+ raw = keys.address_private_key(address_id, epoch: at)
439
+ begin
440
+ AddressKeyExport.new(address_id: address_id, epoch: at, private_key: Encoding.to_b64u(raw),
441
+ public_key: keys.address_public_key(address_id, epoch: at))
442
+ ensure
443
+ raw.replace("\0".b * raw.bytesize) unless raw.frozen?
444
+ end
445
+ end
446
+
447
+ # Moves an address to its next key epoch: mail from now on is sealed to a
448
+ # new key, so an exported key stops opening new mail. A device or agent
449
+ # holding the address is handed the new key. Needs the phrase and a
450
+ # signed-in session. An address shared with team members is refused with a
451
+ # 409 `team_address`: rotate it in the web app, which also gives each
452
+ # member the new key.
453
+ def rotate_key(address_id)
454
+ keys = @client.require_keys("give an address new keys")
455
+ inbox = find_inbox(address_id)
456
+ ensure_not_shared(address_id)
457
+ epoch = Mail.key_epoch(inbox) + 1
458
+ @client.http.request("POST", "/inboxes/#{Mailcycle.path_segment(address_id)}/rotate",
459
+ body: { "epoch" => epoch, "publicKey" => keys.address_public_key(address_id, epoch: epoch),
460
+ "grants" => [] })
461
+ holder = inbox["assignedWorkerId"]
462
+ if holder.is_a?(String) && !holder.empty?
463
+ begin
464
+ # A holder reads the new epoch once it has the new key; one that cannot be reached is left as it was.
465
+ @client.devices.hand_over_keys(holder)
466
+ rescue Error
467
+ nil
468
+ end
469
+ end
470
+ AddressRotation.new(address_id: address_id, epoch: epoch)
471
+ end
472
+
422
473
  # How many addresses the plan allows and how many are left.
423
474
  def allocation
424
475
  @client.http.request("GET", "/inboxes/allocation")
@@ -426,6 +477,29 @@ module Mailcycle
426
477
 
427
478
  private
428
479
 
480
+ def find_inbox(address_id)
481
+ list = Wire.array(Wire.object(@client.http.request("GET", "/inboxes"))["inboxes"])
482
+ found = list.find { |item| item.is_a?(Hash) && item["id"] == address_id }
483
+ raise ApiError.new(404, "not_found", "That address is not on this account.") if found.nil?
484
+
485
+ found
486
+ end
487
+
488
+ def ensure_not_shared(address_id)
489
+ members = Wire.array(Wire.object(@client.http.request("GET", "/team"))["members"])
490
+ shared = members.any? do |member|
491
+ Wire.array(Wire.object(member)["grants"]).any? { |grant| Wire.object(grant)["inboxId"] == address_id }
492
+ end
493
+ return unless shared
494
+
495
+ raise ApiError.new(409, "team_address",
496
+ "This address is shared with team members. Give it new keys in the web app, " \
497
+ "which also gives them the new key.")
498
+ rescue ApiError => e
499
+ # A plan without Teams has no members to keep.
500
+ raise unless e.status == 402
501
+ end
502
+
429
503
  def to_address(wire)
430
504
  wire = Wire.object(wire)
431
505
  id = Wire.text(wire["id"])
@@ -90,8 +90,8 @@ module Mailcycle
90
90
  )
91
91
  end
92
92
 
93
- def identity_from_phrase(phrase, passphrase = "")
94
- identity_from_seed(Mnemonic.phrase_to_seed(phrase, passphrase))
93
+ def identity_from_phrase(phrase)
94
+ identity_from_seed(Mnemonic.phrase_to_seed(phrase))
95
95
  end
96
96
 
97
97
  # The verifier the server is allowed to store.
@@ -19,9 +19,7 @@ module Mailcycle
19
19
  # Derivation cost.
20
20
  #
21
21
  # Changing it changes every account id ever derived, so this number is
22
- # part of the format, not a tuning knob. Stretching buys little over a
23
- # 128-bit phrase and earns its keep only for the optional passphrase,
24
- # which may be low entropy.
22
+ # part of the format, not a tuning knob.
25
23
  SEED_ITERATIONS = 120_000
26
24
  SEED_BYTES = 64
27
25
  SEED_SALT_PREFIX = "mailcycle-seed"
@@ -112,14 +110,9 @@ module Mailcycle
112
110
  end
113
111
 
114
112
  # Phrase to 64-byte root seed.
115
- #
116
- # The optional passphrase is BIP-39's "25th word": it changes which
117
- # account the phrase opens, and nothing anywhere records whether one was
118
- # used.
119
- def phrase_to_seed(phrase, passphrase = "")
113
+ def phrase_to_seed(phrase)
120
114
  words = normalize(phrase).join(" ")
121
- salt = SEED_SALT_PREFIX.b + passphrase.to_s.b
122
- Primitives.pbkdf2_sha256(words, salt, SEED_ITERATIONS, SEED_BYTES)
115
+ Primitives.pbkdf2_sha256(words, SEED_SALT_PREFIX.b, SEED_ITERATIONS, SEED_BYTES)
123
116
  end
124
117
  end
125
118
  end
@@ -9,11 +9,11 @@ module Mailcycle
9
9
  # the iteration count is the format either way, and changing it would change
10
10
  # every account id ever derived.
11
11
  class Keys
12
- def self.from_phrase(phrase, passphrase = "")
12
+ def self.from_phrase(phrase)
13
13
  problem = Crypto::Mnemonic.validate(phrase)
14
14
  raise PhraseError, problem unless problem.nil?
15
15
 
16
- new(Crypto::Identity.identity_from_seed(Crypto::Mnemonic.phrase_to_seed(phrase, passphrase)))
16
+ new(Crypto::Identity.identity_from_seed(Crypto::Mnemonic.phrase_to_seed(phrase)))
17
17
  end
18
18
 
19
19
  def initialize(identity)
@@ -61,6 +61,23 @@ module Mailcycle
61
61
  Address = Struct.new(:id, :email_address, :status, :created_at, :retention_days, :assigned_worker_id,
62
62
  :paused_at, :label, :sender_name, :receives, keyword_init: true)
63
63
 
64
+ # An address's private key at one key epoch, as `addresses.export_key` returns it.
65
+ # `private_key` and `public_key` are base64url, 32 bytes each. The private key
66
+ # opens that address's mail and attachments and nothing else; treat it as the mail itself.
67
+ AddressKeyExport = Struct.new(:address_id, :epoch, :private_key, :public_key, keyword_init: true) do
68
+ def inspect
69
+ "#<struct Mailcycle::AddressKeyExport address_id=#{address_id.inspect} epoch=#{epoch}>"
70
+ end
71
+ alias_method :to_s, :inspect
72
+
73
+ def pretty_print(printer)
74
+ printer.text(inspect)
75
+ end
76
+ end
77
+
78
+ # The result of `addresses.rotate_key`: the address and the epoch it moved to.
79
+ AddressRotation = Struct.new(:address_id, :epoch, keyword_init: true)
80
+
64
81
  # `reserved` is true for a domain that belongs to this account alone.
65
82
  Domain = Struct.new(:domain, :reserved, :extra, keyword_init: true) do
66
83
  def self.from_wire(wire)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mailcycle
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -223,8 +223,8 @@ module Mailcycle
223
223
 
224
224
  first = @buffer.getbyte(0)
225
225
  second = @buffer.getbyte(1)
226
- raise Broken, "the server masked a frame" if second & 0x80 != 0
227
- raise Broken, "a frame used reserved bits" if first & 0x70 != 0
226
+ raise Broken, "the server masked a frame" if second.anybits?(0x80)
227
+ raise Broken, "a frame used reserved bits" if first.anybits?(0x70)
228
228
 
229
229
  length = second & 0x7f
230
230
  offset = 2
@@ -244,7 +244,7 @@ module Mailcycle
244
244
 
245
245
  payload = @buffer.byteslice(offset, length)
246
246
  @buffer = @buffer.byteslice(offset + length, @buffer.bytesize - offset - length)
247
- [first & 0x80 != 0, first & 0x0f, payload]
247
+ [first.anybits?(0x80), first & 0x0f, payload]
248
248
  end
249
249
 
250
250
  # A Frame for a whole message or a close, nil for anything handled here.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailcycle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Northlab Studios Ltd
@@ -23,8 +23,8 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '3.0'
26
- description: Create addresses that receive mail, read and decrypt it on your machine,
27
- send, and listen for events.
26
+ description: Create addresses that receive email, read and decrypt messages on your
27
+ machine, send email, verify webhooks and stream events.
28
28
  email:
29
29
  - hello@mailcycle.email
30
30
  executables: []
@@ -56,12 +56,12 @@ files:
56
56
  - lib/mailcycle/version.rb
57
57
  - lib/mailcycle/webhook_signature.rb
58
58
  - lib/mailcycle/websocket.rb
59
- homepage: https://mailcycle.email/
59
+ homepage: https://mailcycle.email/sdks/ruby/
60
60
  licenses:
61
61
  - Apache-2.0
62
62
  metadata:
63
- homepage_uri: https://mailcycle.email/
64
- documentation_uri: https://docs.mailcycle.email/
63
+ homepage_uri: https://mailcycle.email/sdks/ruby/
64
+ documentation_uri: https://docs.mailcycle.email/guides/sdk-ruby/
65
65
  rubygems_mfa_required: 'true'
66
66
  rdoc_options: []
67
67
  require_paths:
@@ -79,5 +79,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  requirements: []
80
80
  rubygems_version: 4.0.20
81
81
  specification_version: 4
82
- summary: Ruby client for the Mailcycle API.
82
+ summary: 'Ruby email API client: receive, read and send email, verify webhooks.'
83
83
  test_files: []