Skip to content

Commit adc92b6

Browse files
committed
feat(sdk-core): add derivePasskeyPrfKey function
- fetch keychain webauthn devices and build PRF eval map - trigger WebAuthn assertion via provider - derive hex wallet passphrase from PRF output Ticket: WCN-411
1 parent 94da3fc commit adc92b6

6 files changed

Lines changed: 199 additions & 3 deletions

File tree

Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ COPY --from=builder /tmp/bitgo/modules/express /var/bitgo-express/
4343
#COPY_START
4444
COPY --from=builder /tmp/bitgo/modules/abstract-lightning /var/modules/abstract-lightning/
4545
COPY --from=builder /tmp/bitgo/modules/sdk-core /var/modules/sdk-core/
46+
COPY --from=builder /tmp/bitgo/modules/passkey-crypto /var/modules/passkey-crypto/
47+
COPY --from=builder /tmp/bitgo/modules/sjcl /var/modules/sjcl/
4648
COPY --from=builder /tmp/bitgo/modules/sdk-lib-mpc /var/modules/sdk-lib-mpc/
4749
COPY --from=builder /tmp/bitgo/modules/sdk-opensslbytes /var/modules/sdk-opensslbytes/
48-
COPY --from=builder /tmp/bitgo/modules/sjcl /var/modules/sjcl/
4950
COPY --from=builder /tmp/bitgo/modules/secp256k1 /var/modules/secp256k1/
5051
COPY --from=builder /tmp/bitgo/modules/statics /var/modules/statics/
5152
COPY --from=builder /tmp/bitgo/modules/utxo-lib /var/modules/utxo-lib/
@@ -145,9 +146,10 @@ COPY --from=builder /tmp/bitgo/modules/sdk-coin-zec /var/modules/sdk-coin-zec/
145146

146147
RUN cd /var/modules/abstract-lightning && yarn link && \
147148
cd /var/modules/sdk-core && yarn link && \
149+
cd /var/modules/passkey-crypto && yarn link && \
150+
cd /var/modules/sjcl && yarn link && \
148151
cd /var/modules/sdk-lib-mpc && yarn link && \
149152
cd /var/modules/sdk-opensslbytes && yarn link && \
150-
cd /var/modules/sjcl && yarn link && \
151153
cd /var/modules/secp256k1 && yarn link && \
152154
cd /var/modules/statics && yarn link && \
153155
cd /var/modules/utxo-lib && yarn link && \
@@ -250,9 +252,10 @@ cd /var/modules/sdk-coin-zec && yarn link
250252
RUN cd /var/bitgo-express && \
251253
yarn link @bitgo/abstract-lightning && \
252254
yarn link @bitgo/sdk-core && \
255+
yarn link @bitgo/passkey-crypto && \
256+
yarn link @bitgo/sjcl && \
253257
yarn link @bitgo/sdk-lib-mpc && \
254258
yarn link @bitgo/sdk-opensslbytes && \
255-
yarn link @bitgo/sjcl && \
256259
yarn link @bitgo/secp256k1 && \
257260
yarn link @bitgo/statics && \
258261
yarn link @bitgo/utxo-lib && \

modules/sdk-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
]
4141
},
4242
"dependencies": {
43+
"@bitgo/passkey-crypto": "^0.1.0",
4344
"@bitgo/public-types": "5.97.0",
4445
"@bitgo/sdk-lib-mpc": "^10.11.1",
4546
"@bitgo/secp256k1": "^1.11.0",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
buildEvalByCredential,
3+
matchDeviceByCredentialId,
4+
derivePassword,
5+
type WebAuthnProvider,
6+
} from '@bitgo/passkey-crypto';
7+
import type { BitGoBase } from '../bitgoBase';
8+
import type { IWallet } from '../wallet/iWallet';
9+
10+
/**
11+
* Derives a wallet passphrase from a passkey PRF output.
12+
*
13+
* Fetches the wallet's user keychain, triggers a WebAuthn assertion with PRF
14+
* evaluation, and returns a hex-encoded passphrase suitable for use as
15+
* walletPassphrase in signing calls.
16+
*/
17+
export async function derivePasskeyPrfKey(params: {
18+
bitgo: BitGoBase;
19+
wallet: IWallet;
20+
provider: WebAuthnProvider;
21+
}): Promise<string> {
22+
const { wallet, provider } = params;
23+
24+
// Fetch the wallet's user keychain to get webauthnDevices
25+
const keychain = await wallet.getEncryptedUserKeychain();
26+
const devices = keychain.webauthnDevices;
27+
28+
if (!devices || devices.length === 0) {
29+
throw new Error('No passkey devices available');
30+
}
31+
32+
// Build PRF eval map from devices
33+
const { evalByCredential } = buildEvalByCredential(devices as Parameters<typeof buildEvalByCredential>[0]);
34+
35+
if (Object.keys(evalByCredential).length === 0) {
36+
throw new Error('No passkey devices available with a valid PRF salt');
37+
}
38+
39+
// Trigger WebAuthn assertion with PRF evaluation
40+
const result = await provider.get({
41+
publicKey: { challenge: new Uint8Array() } as PublicKeyCredentialRequestOptions,
42+
evalByCredential,
43+
});
44+
45+
// Verify the credential matches a known device
46+
matchDeviceByCredentialId(devices as Parameters<typeof matchDeviceByCredentialId>[0], result.credentialId);
47+
48+
// Derive and return hex-encoded wallet passphrase
49+
if (!result.prfResult) {
50+
throw new Error('PRF output was not returned by the authenticator');
51+
}
52+
53+
return derivePassword(result.prfResult);
54+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { derivePasskeyPrfKey } from './derivePasskeyPrfKey';
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import * as assert from 'assert';
2+
import * as sinon from 'sinon';
3+
import { derivePasskeyPrfKey } from '../../../../src/bitgo/passkey/derivePasskeyPrfKey';
4+
5+
describe('derivePasskeyPrfKey', function () {
6+
const mockDevices = [
7+
{
8+
otpDeviceId: 'device-1',
9+
authenticatorInfo: { credID: 'cred-aaa', fmt: 'none' as const, publicKey: 'pk-1' },
10+
prfSalt: 'salt-aaa',
11+
encryptedPrv: 'enc-prv-1',
12+
},
13+
{
14+
otpDeviceId: 'device-2',
15+
authenticatorInfo: { credID: 'cred-bbb', fmt: 'none' as const, publicKey: 'pk-2' },
16+
prfSalt: 'salt-bbb',
17+
encryptedPrv: 'enc-prv-2',
18+
},
19+
];
20+
21+
function makeWallet(devices: typeof mockDevices | undefined) {
22+
return {
23+
getEncryptedUserKeychain: sinon.stub().resolves({
24+
id: 'keychain-id',
25+
pub: 'xpub123',
26+
encryptedPrv: 'encrypted-prv',
27+
type: 'independent',
28+
webauthnDevices: devices,
29+
}),
30+
};
31+
}
32+
33+
afterEach(function () {
34+
sinon.restore();
35+
});
36+
37+
it('should return a hex string on happy path', async function () {
38+
const prfResult = new Uint8Array([0xde, 0xad, 0xbe, 0xef]).buffer;
39+
40+
const mockProvider = {
41+
create: sinon.stub(),
42+
get: sinon.stub().resolves({
43+
prfResult,
44+
credentialId: 'cred-aaa',
45+
otpCode: 'otp-123',
46+
}),
47+
};
48+
49+
const wallet = makeWallet(mockDevices);
50+
51+
const result = await derivePasskeyPrfKey({
52+
bitgo: {} as any,
53+
wallet: wallet as any,
54+
provider: mockProvider,
55+
});
56+
57+
// derivePassword converts ArrayBuffer to hex
58+
assert.strictEqual(result, 'deadbeef');
59+
assert.ok(mockProvider.get.calledOnce);
60+
// Verify evalByCredential was passed
61+
const getCallArgs = mockProvider.get.firstCall.args[0];
62+
assert.strictEqual(getCallArgs.evalByCredential['cred-aaa'], 'salt-aaa');
63+
assert.strictEqual(getCallArgs.evalByCredential['cred-bbb'], 'salt-bbb');
64+
});
65+
66+
it("should throw 'No passkey devices available' when no devices", async function () {
67+
const wallet = makeWallet(undefined);
68+
const mockProvider = { create: sinon.stub(), get: sinon.stub() };
69+
70+
await assert.rejects(
71+
() => derivePasskeyPrfKey({ bitgo: {} as any, wallet: wallet as any, provider: mockProvider }),
72+
(err: Error) => {
73+
assert.strictEqual(err.message, 'No passkey devices available');
74+
return true;
75+
}
76+
);
77+
});
78+
79+
it("should throw 'No passkey devices available' when devices array is empty", async function () {
80+
const wallet = makeWallet([] as any);
81+
const mockProvider = { create: sinon.stub(), get: sinon.stub() };
82+
83+
await assert.rejects(
84+
() => derivePasskeyPrfKey({ bitgo: {} as any, wallet: wallet as any, provider: mockProvider }),
85+
(err: Error) => {
86+
assert.strictEqual(err.message, 'No passkey devices available');
87+
return true;
88+
}
89+
);
90+
});
91+
92+
it("should throw 'No passkey devices available with a valid PRF salt' when no device has prfSalt", async function () {
93+
const devicesWithoutSalt = [
94+
{
95+
otpDeviceId: 'device-1',
96+
authenticatorInfo: { credID: 'cred-aaa', fmt: 'none' as const, publicKey: 'pk-1' },
97+
prfSalt: '', // empty — buildEvalByCredential skips falsy prfSalt
98+
encryptedPrv: 'enc-prv-1',
99+
},
100+
];
101+
102+
const wallet = makeWallet(devicesWithoutSalt as any);
103+
const mockProvider = { create: sinon.stub(), get: sinon.stub() };
104+
105+
await assert.rejects(
106+
() => derivePasskeyPrfKey({ bitgo: {} as any, wallet: wallet as any, provider: mockProvider }),
107+
(err: Error) => {
108+
assert.strictEqual(err.message, 'No passkey devices available with a valid PRF salt');
109+
return true;
110+
}
111+
);
112+
});
113+
114+
it("should throw 'Could not identify which passkey device was used' when credentialId not found", async function () {
115+
const mockProvider = {
116+
create: sinon.stub(),
117+
get: sinon.stub().resolves({
118+
prfResult: new ArrayBuffer(32),
119+
credentialId: 'unknown-cred-id',
120+
otpCode: 'otp-123',
121+
}),
122+
};
123+
124+
const wallet = makeWallet(mockDevices);
125+
126+
await assert.rejects(
127+
() => derivePasskeyPrfKey({ bitgo: {} as any, wallet: wallet as any, provider: mockProvider }),
128+
(err: Error) => {
129+
assert.strictEqual(err.message, 'Could not identify which passkey device was used');
130+
return true;
131+
}
132+
);
133+
});
134+
});

modules/sdk-core/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"include": ["src/**/*", "test/**/*"],
1111
"exclude": ["node_modules"],
1212
"references": [
13+
{
14+
"path": "../passkey-crypto"
15+
},
1316
{
1417
"path": "../statics"
1518
},

0 commit comments

Comments
 (0)