Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 2x 2x 2x 1x 1x 1x 1x 2x 1x 2x | import { decodeToken, decryptECIES } from 'micro-stacks/crypto';
import { hexToJSON } from 'micro-stacks/common';
import type { AuthResponsePayload, StacksSessionState } from './types';
export function getDIDType(decentralizedID: string) {
const didParts = decentralizedID.split(':');
Iif (didParts.length !== 3) throw new TypeError('Decentralized IDs must have 3 parts');
Iif (didParts[0].toLowerCase() !== 'did')
throw new TypeError('Decentralized IDs must start with "did"');
return didParts[1].toLowerCase();
}
export function getAddressFromDID(decentralizedID: string): string | undefined {
return decentralizedID && getDIDType(decentralizedID) === 'btc-addr'
? decentralizedID.split(':')[2]
: undefined;
}
export async function decodeAuthResponse(
authResponseToken: string,
transitPrivateKey: string
): Promise<StacksSessionState> {
const token = decodeToken(authResponseToken);
const payload = token?.payload;
const authResponse = payload as unknown as AuthResponsePayload;
let appPrivateKey;
Iif (authResponse.private_key) {
try {
const cipherObject = hexToJSON(authResponse.private_key);
appPrivateKey = (await decryptECIES({
privateKey: transitPrivateKey,
cipherObject,
})) as string;
} catch (e) {
console.error('[micro-stacks] failed to decrypt appPrivateKey');
}
}
const sessionState: StacksSessionState = {
addresses: authResponse.profile.stxAddress,
appPrivateKey,
associationToken: authResponse.associationToken,
hubUrl: authResponse.hubUrl,
public_keys: authResponse.public_keys,
profile: authResponse['profile'],
profile_url: authResponse.profile_url,
username: authResponse.username,
version: authResponse.version,
decentralizedID: authResponse.iss,
identityAddress: getAddressFromDID(authResponse.iss),
};
return sessionState;
}
|