-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-api + sdk): enable auth headers for admin api routes (#354)
- Loading branch information
Showing
12 changed files
with
108 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { unmarshalPrivateKey } from '@libp2p/crypto/keys'; | ||
import { PrivateKey } from '@libp2p/interface'; | ||
import bs58 from 'bs58'; | ||
import { WalletType } from '../api/nodeApi'; | ||
import { ClientKey } from '../types/storage'; | ||
import { getStorageClientKey } from '../storage/storage'; | ||
|
||
export interface Header { | ||
[key: string]: string; | ||
} | ||
|
||
export async function createAuthHeader( | ||
payload: string, | ||
contextId: string | ||
): Promise<Header | null> { | ||
const privateKey: PrivateKey = await getPrivateKey(); | ||
|
||
if (!privateKey) { | ||
return null; | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
const contentBuff = encoder.encode(payload); | ||
|
||
const signingKey = bs58.encode(privateKey.public.bytes); | ||
|
||
const hashBuffer = await crypto.subtle.digest('SHA-256', contentBuff); | ||
const hashArray = new Uint8Array(hashBuffer); | ||
|
||
const signature = await privateKey.sign(hashArray); | ||
const signatureBase58 = bs58.encode(signature); | ||
const contentBase58 = bs58.encode(hashArray); | ||
|
||
const headers: Header = { | ||
wallet_type: JSON.stringify(WalletType.NEAR), | ||
signing_key: signingKey, | ||
signature: signatureBase58, | ||
challenge: contentBase58, | ||
context_id: contextId, | ||
}; | ||
|
||
return headers; | ||
} | ||
|
||
export async function getPrivateKey(): Promise<PrivateKey | null> { | ||
try { | ||
const clientKey: ClientKey | null = getStorageClientKey(); | ||
if (!clientKey) { | ||
return null; | ||
} | ||
return await unmarshalPrivateKey(bs58.decode(clientKey.privateKey)); | ||
} catch (error) { | ||
console.error('Error extracting private key:', error); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './headers'; | ||
export * from './ed25519'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/calimero-sdk/src/wallets/MetamaskLogin/LoginWithMetamask.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters