Module: Solace::SquadsSmartAccounts::VaultIndex

Extended by:
VaultIndex
Included in:
VaultIndex
Defined in:
lib/solace/squads_smart_accounts/vault_index.rb

Overview

Reverse lookup from a smart-account (vault) address back to its index and settings address.

Vault addresses are derived one-way from an index (settings_seed), so the only way to invert the mapping is to precompute it. This builds a compact on-disk table — one 32-byte vault pubkey per record, where the record at 0-based offset ‘o` is the default vault (account_index 0) of `settings_seed = o + 1` — and queries it.

Caveats: the table is a snapshot covering indices ‘1..count`, so an address with a higher index (or created after the build) is not found — rebuild with a larger `count` to extend. Only the default vault (account_index 0) is indexed.

Examples:

Build once, then look up

VaultIndex.build(count: 500_000)
VaultIndex.lookup(vault_address) # => { index: 1500, settings_address: "41gq..." }

Constant Summary collapse

RECORD_SIZE =

Bytes per record — a raw ed25519 public key.

32
DEFAULT_FILENAME =

Default table filename, written in the current working directory (wherever the caller runs from). Pass ‘path:` to #build/#lookup to put it elsewhere.

'vault-index.bin'

Instance Method Summary collapse

Instance Method Details

#build(count: 500_000, path: default_path, progress: nil) ⇒ String

Derives the default vault for each seed in 1..count and writes the raw pubkeys to ‘path`. The write is atomic, so an interrupted build leaves no partial cache.

Parameters:

  • count (Integer) (defaults to: 500_000)

    Number of indices to cover (seeds 1..count).

  • path (String) (defaults to: default_path)

    Output file path (default: #default_path).

  • progress (Proc, nil) (defaults to: nil)

    Optional ‘progress.call(done, count)`, every 50k seeds.

Returns:

  • (String)

    The path written.



45
46
47
48
49
50
51
52
53
# File 'lib/solace/squads_smart_accounts/vault_index.rb', line 45

def build(count: 500_000, path: default_path, progress: nil)
  write_atomically(path) do |io|
    (1..count).each do |seed|
      io.write(default_vault_pubkey(seed))
      progress&.call(seed, count) if (seed % 50_000).zero?
    end
  end
  path
end

#default_pathString

Returns The default table path: DEFAULT_FILENAME in the current directory.

Returns:

  • (String)

    The default table path: DEFAULT_FILENAME in the current directory.



34
35
36
# File 'lib/solace/squads_smart_accounts/vault_index.rb', line 34

def default_path
  File.join(Dir.pwd, DEFAULT_FILENAME)
end

#lookup(vault_address, path: default_path) ⇒ Hash?

Resolves a vault address to its index and settings address.

Parameters:

  • vault_address (#to_s)

    The smart-account (vault) address.

  • path (String) (defaults to: default_path)

    The table file path (default: #default_path).

Returns:

  • (Hash, nil)

    ‘{ index:, settings_address: }`, or nil if not in the table.

Raises:

  • (RuntimeError)

    If the table file does not exist.



61
62
63
64
65
66
67
# File 'lib/solace/squads_smart_accounts/vault_index.rb', line 61

def lookup(vault_address, path: default_path)
  seed = table(path)[packed_pubkey(vault_address)]
  return unless seed

  settings_address, = settings_for(seed)
  { index: seed, settings_address: }
end