• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hicommonwealth / commonwealth / 14870193493

06 May 2025 09:35PM UTC coverage: 43.081% (-0.6%) from 43.654%
14870193493

Pull #12056

github

web-flow
Merge 7a94be300 into 2bbbdfe0a
Pull Request #12056: Judged contest fixes

1685 of 4323 branches covered (38.98%)

Branch coverage included in aggregate %.

4 of 16 new or added lines in 4 files covered. (25.0%)

156 existing lines in 14 files now uncovered.

3050 of 6668 relevant lines covered (45.74%)

37.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.77
/libs/model/src/utils/findBaseAddress.ts
1
import { InvalidInput } from '@hicommonwealth/core';
2
import { ChainBase, ChainType } from '@hicommonwealth/shared';
3
import { Op } from 'sequelize';
4
import { models } from '../database';
5

6
/**
7
 * Finds a compatible base address for the given user and chain base/type.
8
 * Addresses are created when users authenticate with a wallet for the first time, or when
9
 * users join a community.
10
 *
11
 * @param user_id id of user account
12
 * @param address current address
13
 * @param base base chain
14
 * @param type chain type
15
 * @returns compatible base address for the given actor
16
 */
17
export async function findCompatibleAddress(
18
  user_id: number,
19
  address: string,
20
  base: ChainBase,
21
  type: ChainType = ChainType.Offchain, // when joining we don't need to check the type
12✔
22
) {
23
  // First try to find if the current actor's address is compatible with the given base,
24
  // and use it to create or join the community
25
  const found = await models.Address.scope('withPrivateData').findOne({
15✔
26
    where: { user_id, address },
27
    include: [
28
      {
29
        model: models.Community,
30
        where: { base },
31
        required: true,
32
      },
33
    ],
34
  });
35
  if (found) return found;
15✔
36

37
  if (base === ChainBase.NEAR) throw new InvalidInput('Invalid Base');
5!
38

39
  // --------------------------------------------------------------------------------
40
  // When current address not found, try to find any other compatible address for this user,
41
  // (from other communities with same base)
42
  // --------------------------------------------------------------------------------
43

44
  if (base === ChainBase.Ethereum)
5✔
45
    return await models.Address.scope('withPrivateData').findOne({
2✔
46
      where: { user_id, address: { [Op.startsWith]: '0x' } },
47
      include: [
48
        {
49
          model: models.Community,
50
          where: { base },
51
          required: true,
52
        },
53
      ],
54
    });
55

56
  if (base === ChainBase.Solana)
3!
57
    return await models.Address.scope('withPrivateData').findOne({
×
58
      where: {
59
        user_id,
60
        address: {
61
          // This is the regex formatting for solana addresses per their website
62
          [Op.regexp]: '[1-9A-HJ-NP-Za-km-z]{32,44}',
63
        },
64
      },
65
      include: [
66
        {
67
          model: models.Community,
68
          where: { base },
69
          required: true,
70
        },
71
      ],
72
    });
73

74
  if (base === ChainBase.Sui)
3!
UNCOV
75
    return await models.Address.scope('withPrivateData').findOne({
×
76
      where: {
77
        user_id,
78
        address: {
79
          // Sui addresses are 0x followed by 64 hex characters
80
          [Op.regexp]: '0x[a-fA-F0-9]{64}',
81
        },
82
      },
83
      include: [
84
        {
85
          model: models.Community,
86
          where: { base },
87
          required: true,
88
        },
89
      ],
90
    });
91

92
  // Onchain community can be created by Admin only, but we allow Offchain to have any creator
93
  // if signed in with Keplr or Magic
94
  if (base === ChainBase.CosmosSDK && type === ChainType.Offchain)
3✔
95
    return await models.Address.scope('withPrivateData').findOne({
1✔
96
      where: { user_id },
97
      include: [
98
        {
99
          model: models.Community,
100
          where: { base },
101
          required: true,
102
        },
103
      ],
104
    });
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc