aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/diagnostics-reporter.js
blob: aa4ca6e26f2d367b9febbe8b77f9dc7609341851 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class DiagnosticsReporter {

  constructor ({ firstTimeInfo, version }) {
    this.firstTimeInfo = firstTimeInfo
    this.version = version
  }

  async reportOrphans(orphans) {
    try {
      return await this.submit({
        accounts: Object.keys(orphans),
        metadata: {
          type: 'orphans',
        },
      })
    } catch (err) {
      console.error('DiagnosticsReporter - "reportOrphans" encountered an error:')
      console.error(err)
    }
  }

  async reportMultipleKeyrings(rawKeyrings) {
    try {
      const keyrings = await Promise.all(rawKeyrings.map(async (keyring, index) => {
        return {
          index,
          type: keyring.type,
          accounts: await keyring.getAccounts(),
        }
      }))
      return await this.submit({
        accounts: [],
        metadata: {
          type: 'keyrings',
          keyrings,
        },
      })
    } catch (err) {
      console.error('DiagnosticsReporter - "reportMultipleKeyrings" encountered an error:')
      console.error(err)
    }
  }

  async submit (message) {
    try {
      // add metadata
      message.metadata.version = this.version
      message.metadata.firstTimeInfo = this.firstTimeInfo
      return await postData(message)
    } catch (err) {
      console.error('DiagnosticsReporter - "submit" encountered an error:')
      throw err
    }
  }

}

function postData(data) {
  const uri = 'https://diagnostics.metamask.io/v1/orphanedAccounts'
  return fetch(uri, {
    body: JSON.stringify(data), // must match 'Content-Type' header
    credentials: 'same-origin', // include, same-origin, *omit
    headers: {
      'content-type': 'application/json',
    },
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, cors, *same-origin
  })
}

module.exports = DiagnosticsReporter