Skip to content

Commit

Permalink
Set Sentry user id to hashed identifier (#904)
Browse files Browse the repository at this point in the history
We calculate the ID of the user by hashing the address. This way we
won't store any sensitive information in Sentry.
  • Loading branch information
r-czajkowski authored Nov 28, 2024
2 parents cb3f90a + e61a8e1 commit c217341
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
38 changes: 38 additions & 0 deletions dapp/src/sentry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it, vi } from "vitest"
import * as Sentry from "@sentry/react"
import sentry from "./sentry"

describe("sentry", () => {
describe("setUser", () => {
vi.mock("@sentry/react")

const testCases = [
{ bitcoinAddress: undefined, expectedResult: null },
{ bitcoinAddress: "", expectedResult: null },
{ bitcoinAddress: " ", expectedResult: null },
{
bitcoinAddress: "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem",
expectedResult: { id: "1f520a9757" },
},
{
bitcoinAddress: "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
expectedResult: { id: "6cd42dab02" },
},
{
bitcoinAddress: "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4",
expectedResult: { id: "6cd42dab02" },
},
]

describe.each(testCases)(
"when address is $bitcoinAddress",
({ bitcoinAddress, expectedResult }) => {
it("should set expected user in Sentry", () => {
sentry.setUser(bitcoinAddress)

expect(Sentry.setUser).toHaveBeenCalledWith(expectedResult)
})
},
)
})
})
21 changes: 20 additions & 1 deletion dapp/src/sentry/index.ts → dapp/src/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Sentry from "@sentry/react"
import { sha256, toUtf8Bytes } from "ethers"

const initialize = (dsn: string) => {
Sentry.init({
Expand All @@ -17,8 +18,26 @@ const initialize = (dsn: string) => {
})
}

/**
* Sets the user in Sentry with an ID from hashed Bitcoin address.
* The Bitcoin address is first converted to lowercase and then hashed using SHA-256.
* The resulting hash is then converted to a hexadecimal string and the first 10
* characters are set as the user ID.
*
* @param bitcoinAddress - The Bitcoin address of the user. If undefined, the user
* is set to null in Sentry.
*/
const setUser = (bitcoinAddress: string | undefined) => {
Sentry.setUser(bitcoinAddress ? { id: bitcoinAddress } : null)
if (!bitcoinAddress) {
Sentry.setUser(null)
return
}

const hashedBitcoinAddress = sha256(toUtf8Bytes(bitcoinAddress.toLowerCase()))
// Remove the 0x prefix and take the first 10 characters.
const id = hashedBitcoinAddress.slice(2, 12)

Sentry.setUser({ id })
}

const captureException = (exception: unknown) =>
Expand Down

0 comments on commit c217341

Please sign in to comment.