Skip to content

Commit 08aea9b

Browse files
taylanpinceattente
authored andcommitted
Add extractProjectIdFromAccessKey in utils
1 parent e228736 commit 08aea9b

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

packages/utils/src/access-key.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const extractProjectIdFromAccessKey = (accessKey: string): number => {
2+
// Convert URL-safe base64 string to standard base64 string
3+
const base64String = accessKey.replace(/-/g, '+').replace(/_/g, '/');
4+
// Decode the base64 string to a binary string
5+
const binaryString = atob(base64String);
6+
7+
// Convert the binary string to a byte array (Uint8Array)
8+
const byteArray = new Uint8Array(binaryString.length);
9+
for (let i = 0; i < binaryString.length; i++) {
10+
byteArray[i] = binaryString.charCodeAt(i);
11+
}
12+
13+
if (byteArray[0] !== 1) {
14+
throw new Error('UnsupportedVersion');
15+
}
16+
17+
// Extract the project ID from bytes 2 to 9 (8 bytes)
18+
const projectIdBytes = byteArray.slice(1, 9);
19+
const projectId = projectIdBytes[7] | projectIdBytes[6]<<8 | projectIdBytes[5]<<16 | projectIdBytes[4]<<24 |
20+
projectIdBytes[3]<<32 | projectIdBytes[2]<<40 | projectIdBytes[1]<<48 | projectIdBytes[0]<<56;
21+
22+
return projectId;
23+
}

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './access-key'
12
export * from './base64'
23
export * from './big-number'
34
export * from './digest'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect } from 'chai'
2+
import { extractProjectIdFromAccessKey } from '@0xsequence/utils'
3+
4+
describe('access-key', function () {
5+
it('extractProjectIdFromAccessKey', () => {
6+
const accessKey = 'AQAAAAAAADVH8R2AGuQhwQ1y8NaEf1T7PJM'
7+
8+
const projectId = extractProjectIdFromAccessKey(accessKey)
9+
expect(projectId).to.equal(13639)
10+
})
11+
})

0 commit comments

Comments
 (0)