Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ COPY --from=builder /tmp/bitgo/modules/express /var/bitgo-express/

#COPY_START
COPY --from=builder /tmp/bitgo/modules/abstract-lightning /var/modules/abstract-lightning/
COPY --from=builder /tmp/bitgo/modules/sdk-api /var/modules/sdk-api/
COPY --from=builder /tmp/bitgo/modules/argon2 /var/modules/argon2/
COPY --from=builder /tmp/bitgo/modules/sdk-core /var/modules/sdk-core/
COPY --from=builder /tmp/bitgo/modules/sdk-lib-mpc /var/modules/sdk-lib-mpc/
COPY --from=builder /tmp/bitgo/modules/sdk-opensslbytes /var/modules/sdk-opensslbytes/
Expand All @@ -56,14 +58,12 @@ COPY --from=builder /tmp/bitgo/modules/statics /var/modules/statics/
COPY --from=builder /tmp/bitgo/modules/utxo-lib /var/modules/utxo-lib/
COPY --from=builder /tmp/bitgo/modules/blake2b /var/modules/blake2b/
COPY --from=builder /tmp/bitgo/modules/blake2b-wasm /var/modules/blake2b-wasm/
COPY --from=builder /tmp/bitgo/modules/sdk-hmac /var/modules/sdk-hmac/
COPY --from=builder /tmp/bitgo/modules/unspents /var/modules/unspents/
COPY --from=builder /tmp/bitgo/modules/logger /var/modules/logger/
COPY --from=builder /tmp/bitgo/modules/bitgo /var/modules/bitgo/
COPY --from=builder /tmp/bitgo/modules/abstract-utxo /var/modules/abstract-utxo/
COPY --from=builder /tmp/bitgo/modules/blockapis /var/modules/blockapis/
COPY --from=builder /tmp/bitgo/modules/sdk-api /var/modules/sdk-api/
COPY --from=builder /tmp/bitgo/modules/argon2 /var/modules/argon2/
COPY --from=builder /tmp/bitgo/modules/sdk-hmac /var/modules/sdk-hmac/
COPY --from=builder /tmp/bitgo/modules/unspents /var/modules/unspents/
COPY --from=builder /tmp/bitgo/modules/utxo-core /var/modules/utxo-core/
COPY --from=builder /tmp/bitgo/modules/utxo-descriptors /var/modules/utxo-descriptors/
COPY --from=builder /tmp/bitgo/modules/utxo-ord /var/modules/utxo-ord/
Expand Down Expand Up @@ -152,6 +152,8 @@ COPY --from=builder /tmp/bitgo/modules/sdk-coin-xlm /var/modules/sdk-coin-xlm/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-zec /var/modules/sdk-coin-zec/

RUN cd /var/modules/abstract-lightning && yarn link && \
cd /var/modules/sdk-api && yarn link && \
cd /var/modules/argon2 && yarn link && \
cd /var/modules/sdk-core && yarn link && \
cd /var/modules/sdk-lib-mpc && yarn link && \
cd /var/modules/sdk-opensslbytes && yarn link && \
Expand All @@ -162,14 +164,12 @@ cd /var/modules/statics && yarn link && \
cd /var/modules/utxo-lib && yarn link && \
cd /var/modules/blake2b && yarn link && \
cd /var/modules/blake2b-wasm && yarn link && \
cd /var/modules/sdk-hmac && yarn link && \
cd /var/modules/unspents && yarn link && \
cd /var/modules/logger && yarn link && \
cd /var/modules/bitgo && yarn link && \
cd /var/modules/abstract-utxo && yarn link && \
cd /var/modules/blockapis && yarn link && \
cd /var/modules/sdk-api && yarn link && \
cd /var/modules/argon2 && yarn link && \
cd /var/modules/sdk-hmac && yarn link && \
cd /var/modules/unspents && yarn link && \
cd /var/modules/utxo-core && yarn link && \
cd /var/modules/utxo-descriptors && yarn link && \
cd /var/modules/utxo-ord && yarn link && \
Expand Down Expand Up @@ -261,6 +261,8 @@ cd /var/modules/sdk-coin-zec && yarn link
#LINK_START
RUN cd /var/bitgo-express && \
yarn link @bitgo/abstract-lightning && \
yarn link @bitgo/sdk-api && \
yarn link @bitgo/argon2 && \
yarn link @bitgo/sdk-core && \
yarn link @bitgo/sdk-lib-mpc && \
yarn link @bitgo/sdk-opensslbytes && \
Expand All @@ -271,14 +273,12 @@ RUN cd /var/bitgo-express && \
yarn link @bitgo/utxo-lib && \
yarn link @bitgo/blake2b && \
yarn link @bitgo/blake2b-wasm && \
yarn link @bitgo/sdk-hmac && \
yarn link @bitgo/unspents && \
yarn link @bitgo/logger && \
yarn link bitgo && \
yarn link @bitgo/abstract-utxo && \
yarn link @bitgo/blockapis && \
yarn link @bitgo/sdk-api && \
yarn link @bitgo/argon2 && \
yarn link @bitgo/sdk-hmac && \
yarn link @bitgo/unspents && \
yarn link @bitgo/utxo-core && \
yarn link @bitgo/utxo-descriptors && \
yarn link @bitgo/utxo-ord && \
Expand Down
35 changes: 35 additions & 0 deletions modules/abstract-lightning/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
# BitGo abstract-lightning

## Custodial userAuth key reset

A custody lightning wallet's userAuth key is encrypted with the wallet password. If the customer forgets it, they
can replace the key, with BitGo support approving the request.

```ts
const wallet = new CustodialLightningWallet(sdkWallet);

// 1. Start a reset with the NEW passphrase.
const { requestId, status, verificationCode } = await wallet.requestUserAuthKeyReset({
passphrase: newPassphrase,
encryptionVersion,
});
// show `verificationCode` to the customer; they read it to BitGo support

// 2. Show progress. Rejects with an error whose `status` is 404 when there is no request.
const { status, completedAt, releasedAt } = await wallet.getUserAuthKeyResetRequest();

// 3. Cancel while the request is still `requested`.
await wallet.cancelUserAuthKeyResetRequest(requestId);
```

`requestUserAuthKeyReset` creates the new key locally, encrypts it with the passphrase, registers the encrypted key, signs
a proof with the new key and queues the request. The private key and the passphrase are never sent unencrypted. The
verification code is computed locally and is never taken from a server response.

`status` is one of `requested`, `canceled`, `expired`, `pending`, `dnr_held`, `wp_swapped`, `completed`, `failed`.
"Released" is not a status: the wallet is usable again once `releasedAt` is set on a `completed` request.

After a successful reset, other wallet members lose access until the customer re-shares the wallet to them with
`wallets.reshareWalletWithSpenders(walletId, passphrase, encryptionVersion, { includeAdmins: true, onlyNeedsRecovery: true })`.

`computeUserAuthResetVerificationCode(walletId, newUserAuthKeyPub)` and `compareUserAuthResetVerificationCodes(a, b)` are
exported so BitGo services and tools compute and compare the code identically.
3 changes: 3 additions & 0 deletions modules/abstract-lightning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"io-ts-types": "^0.5.16",
"macaroon": "^3.0.4"
},
"devDependencies": {
"@bitgo/sdk-api": "^4.0.0"
},
"gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c",
"files": [
"dist"
Expand Down
1 change: 1 addition & 0 deletions modules/abstract-lightning/src/codecs/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './payment';
export * from './withdraw';
export * from './transaction';
export * from './wallet';
export * from './userAuthKeyReset';
71 changes: 71 additions & 0 deletions modules/abstract-lightning/src/codecs/api/userAuthKeyReset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as t from 'io-ts';

/**
* The client proof for a userAuth key reset: the new key signs this exact set of fields (canonical JSON),
* which proves the client holds the new private key and binds the request to one wallet.
*
* Exact: a missing field fails and so does any extra field. (`t.exact` would silently strip extras.)
*/
const UserAuthKeyResetSignedRequestFields = t.type({
walletId: t.string,
oldUserAuthKeyPub: t.string,
newUserAuthKeyPub: t.string,
newUserAuthKeyId: t.string,
createdAt: t.string,
});
const signedRequestFieldCount = Object.keys(UserAuthKeyResetSignedRequestFields.props).length;

export const UserAuthKeyResetSignedRequest = t.refinement(
UserAuthKeyResetSignedRequestFields,
(request) => Object.keys(request).length === signedRequestFieldCount,
'UserAuthKeyResetSignedRequest'
);
export type UserAuthKeyResetSignedRequest = t.TypeOf<typeof UserAuthKeyResetSignedRequest>;

/**
* Status of a reset request as tracked by lightning-services.
* "Released" is not a status: it is `releasedAt !== null` on a `completed` or `failed` request.
*/
export const UserAuthKeyResetStatus = t.keyof({
requested: null,
canceled: null,
expired: null,
pending: null,
dnr_held: null,
wp_swapped: null,
completed: null,
failed: null,
});
export type UserAuthKeyResetStatus = t.TypeOf<typeof UserAuthKeyResetStatus>;

/** Body of POST /api/v2/:coin/wallet/:walletId/lightning/userAuthKey/resetRequest */
export const UserAuthKeyResetRequest = t.type({
newUserAuthKeyId: t.string,
signedRequest: UserAuthKeyResetSignedRequest,
signature: t.string,
});
export type UserAuthKeyResetRequest = t.TypeOf<typeof UserAuthKeyResetRequest>;

/** Response of POST .../resetRequest */
export const UserAuthKeyResetResponse = t.type({
requestId: t.string,
status: UserAuthKeyResetStatus,
createdAt: t.string,
});
export type UserAuthKeyResetResponse = t.TypeOf<typeof UserAuthKeyResetResponse>;

/** Response of GET .../resetRequest: the caller's latest request. Never contains the verification code. */
export const UserAuthKeyResetRequestInfo = t.type({
requestId: t.string,
status: UserAuthKeyResetStatus,
createdAt: t.string,
completedAt: t.union([t.string, t.null]),
releasedAt: t.union([t.string, t.null]),
});
export type UserAuthKeyResetRequestInfo = t.TypeOf<typeof UserAuthKeyResetRequestInfo>;

/** Response of DELETE .../resetRequest/:requestId */
export const CancelUserAuthKeyResetResponse = t.type({
status: t.literal('canceled'),
});
export type CancelUserAuthKeyResetResponse = t.TypeOf<typeof CancelUserAuthKeyResetResponse>;
19 changes: 17 additions & 2 deletions modules/abstract-lightning/src/lightning/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import * as utxolib from '@bitgo/utxo-lib';
import * as sdkcore from '@bitgo/sdk-core';
import { canonicalizeObject, Signable } from './signableJson';

/**
* Parses a BIP32 lightning key.
*
* Lightning auth keys are generated with the default (bitcoin) network, so `tlnbtc` keys are still
* `xpub`/`xprv` strings even though the coin's network is testnet. Parsing them against the testnet
* version bytes would throw, so `xpub`/`xprv` keys are always parsed with the bitcoin versions.
* Any other prefix (e.g. `tpub`) is parsed with the supplied network, as before.
*/
function parseLightningKey(key: string, network: utxolib.Network): utxolib.BIP32Interface {
if (key.startsWith('xpub') || key.startsWith('xprv')) {
return utxolib.bip32.fromBase58(key, utxolib.networks.bitcoin);
}
return utxolib.bip32.fromBase58(key, network);
}

/**
* Verifies a signature for a given message.
*
Expand All @@ -18,7 +33,7 @@ export function verifyMessageSignature(
network: utxolib.Network = utxolib.networks.bitcoin
): boolean {
const messageString = JSON.stringify(canonicalizeObject(message));
const pubKey = utxolib.bip32.fromBase58(pub, network).publicKey;
const pubKey = parseLightningKey(pub, network).publicKey;
const signatureBuffer = Buffer.from(signature, 'hex');
return sdkcore.verifyMessage(messageString, pubKey, signatureBuffer, network);
}
Expand All @@ -37,6 +52,6 @@ export function createMessageSignature(
network: utxolib.Network = utxolib.networks.bitcoin
): string {
const requestString = JSON.stringify(canonicalizeObject(message));
const prvKey = utxolib.bip32.fromBase58(xprv, network);
const prvKey = parseLightningKey(xprv, network);
return sdkcore.signMessage(requestString, prvKey, network).toString('hex');
}
114 changes: 112 additions & 2 deletions modules/abstract-lightning/src/wallet/custodialLightning.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
import { ILightningWallet, LightningWallet } from './lightning';
import { getLightningAuthKeychains, ILightningWallet, LightningWallet } from './lightning';
import * as sdkcore from '@bitgo/sdk-core';
import {
CancelUserAuthKeyResetResponse,
UserAuthKeyResetRequest,
UserAuthKeyResetRequestInfo,
UserAuthKeyResetResponse,
UserAuthKeyResetSignedRequest,
UserAuthKeyResetStatus,
} from '../codecs';
import { createMessageSignature, getUtxolibNetwork } from '../lightning';
import { computeUserAuthResetVerificationCode } from './userAuthKeyReset';

export type ICustodialLightningWallet = ILightningWallet;
export interface ICustodialLightningWallet extends ILightningWallet {
/**
* Starts a customer-initiated userAuth key reset for a forgotten wallet password.
*
* Generates a new userAuth key locally, encrypts it with the new passphrase, registers it as an unattached key,
* signs a proof with it and queues the request. The customer reads the returned verification code to BitGo support.
* The private key and the passphrase never leave this method.
*
* @param params.passphrase the NEW wallet passphrase that encrypts the new key
* @param params.encryptionVersion encryption version used for the new key
* @returns the request id and status from the server, and the verification code computed locally
*/
requestUserAuthKeyReset(params: {
passphrase: string;
encryptionVersion?: sdkcore.EncryptionVersion;
}): Promise<{ requestId: string; status: UserAuthKeyResetStatus; verificationCode: string }>;

/**
* Gets the caller's latest userAuth key reset request. Rejects with an error whose `status` is 404 when there is none.
*/
getUserAuthKeyResetRequest(): Promise<UserAuthKeyResetRequestInfo>;

/**
* Cancels a userAuth key reset request. Only possible while the request is still `requested`.
*/
cancelUserAuthKeyResetRequest(requestId: string): Promise<CancelUserAuthKeyResetResponse>;
}

export class CustodialLightningWallet extends LightningWallet implements ICustodialLightningWallet {
constructor(wallet: sdkcore.IWallet) {
Expand All @@ -10,4 +46,78 @@ export class CustodialLightningWallet extends LightningWallet implements ICustod
throw new Error(`Invalid lightning wallet type for custodial lightning: ${wallet.subType()}`);
}
}

private resetRequestUrl(requestId?: string): string {
const suffix = requestId === undefined ? '' : `/${encodeURIComponent(requestId)}`;
return this.wallet.baseCoin.url(`/wallet/${this.wallet.id()}/lightning/userAuthKey/resetRequest${suffix}`);
}

async requestUserAuthKeyReset(params: {
passphrase: string;
encryptionVersion?: sdkcore.EncryptionVersion;
}): Promise<{ requestId: string; status: UserAuthKeyResetStatus; verificationCode: string }> {
const coin = this.wallet.baseCoin;
const { userAuthKey } = await getLightningAuthKeychains(this.wallet);

const { pub, prv } = coin.keychains().create();
if (!pub || !prv) {
throw new Error('Failed to generate user auth key');
}

// Same shape wallet creation uses for the userAuth key; no passcode encryption code is sent.
const newKeychain = await coin.keychains().add({
pub,
encryptedPrv: await this.wallet.bitgo.encrypt({
password: params.passphrase,
input: prv,
encryptionVersion: params.encryptionVersion,
}),
source: 'user',
keyType: 'independent',
coinSpecific: { [coin.getChain()]: { purpose: 'userAuth' } },
});

const signedRequest: UserAuthKeyResetSignedRequest = {
walletId: this.wallet.id(),
oldUserAuthKeyPub: userAuthKey.pub,
newUserAuthKeyPub: pub,
newUserAuthKeyId: newKeychain.id,
createdAt: new Date().toISOString(),
};
const body: UserAuthKeyResetRequest = {
newUserAuthKeyId: newKeychain.id,
signedRequest,
signature: createMessageSignature(signedRequest, prv, getUtxolibNetwork(coin.getChain())),
};

const response = await this.wallet.bitgo
.post(this.resetRequestUrl())
.send(UserAuthKeyResetRequest.encode(body))
.result();
const result = sdkcore.decodeOrElse(UserAuthKeyResetResponse.name, UserAuthKeyResetResponse, response, () => {
// DON'T throw errors from decodeOrElse. It could leak sensitive information.
throw new Error('Invalid user auth key reset response');
});

return {
requestId: result.requestId,
status: result.status,
// Never taken from the server: the customer compares it against what support is shown.
verificationCode: computeUserAuthResetVerificationCode(this.wallet.id(), pub),
};
}

async getUserAuthKeyResetRequest(): Promise<UserAuthKeyResetRequestInfo> {
const response = await this.wallet.bitgo.get(this.resetRequestUrl()).result();
return sdkcore.decodeOrElse(UserAuthKeyResetRequestInfo.name, UserAuthKeyResetRequestInfo, response, () => {
throw new Error('Invalid user auth key reset request response');
});
}

async cancelUserAuthKeyResetRequest(requestId: string): Promise<CancelUserAuthKeyResetResponse> {
const response = await this.wallet.bitgo.del(this.resetRequestUrl(requestId)).send().result();
return sdkcore.decodeOrElse(CancelUserAuthKeyResetResponse.name, CancelUserAuthKeyResetResponse, response, () => {
throw new Error('Invalid user auth key reset cancel response');
});
}
}
1 change: 1 addition & 0 deletions modules/abstract-lightning/src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './lightning';
export * from './custodialLightning';
export * from './selfCustodialLightning';
export * from './wallet';
export * from './userAuthKeyReset';
Loading
Loading