edge-core-js
Advanced tools
Comparing version 1.2.0 to 1.3.0
# edge-core-js | ||
## v1.3.0 (2023-06-16) | ||
- added: Add an `EdgeAccount.getPin` method. | ||
- fixed: Allow the `EdgeAccount.username` property to update after calling `changeUsername`. | ||
## v1.2.0 (2023-06-15) | ||
@@ -4,0 +9,0 @@ |
@@ -70,6 +70,13 @@ function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } | ||
export function makeAccountApi(ai, accountId) { | ||
const accountState = () => ai.props.state.accounts[accountId] | ||
const { accountWalletInfo, loginType, loginTree } = accountState() | ||
const { username } = loginTree | ||
// We don't want accountState to be undefined when we log out, | ||
// so preserve a snapshot of our last state: | ||
let lastState = ai.props.state.accounts[accountId] | ||
const accountState = () => { | ||
const nextState = ai.props.state.accounts[accountId] | ||
if (nextState != null) lastState = nextState | ||
return lastState | ||
} | ||
const { accountWalletInfo, loginType } = accountState() | ||
// Plugin config API's: | ||
@@ -150,3 +157,3 @@ const currencyConfigs = {} | ||
get loggedIn() { | ||
return accountState() != null | ||
return ai.props.state.accounts[accountId] != null | ||
}, | ||
@@ -164,2 +171,3 @@ | ||
lockdown() | ||
const { loginTree } = accountState() | ||
return base58.stringify(loginTree.loginId) | ||
@@ -169,3 +177,4 @@ }, | ||
get username() { | ||
return username | ||
const { loginTree } = accountState() | ||
return loginTree.username | ||
}, | ||
@@ -263,2 +272,7 @@ | ||
async getPin() { | ||
const { login, loginTree } = accountState() | ||
return _nullishCoalesce(login.pin, () => ( loginTree.pin)) | ||
}, | ||
// ---------------------------------------------------------------- | ||
@@ -331,2 +345,3 @@ // Remove credentials: | ||
async approveVoucher(voucherId) { | ||
const { loginTree } = accountState() | ||
return await changeVoucherStatus(ai, loginTree, { | ||
@@ -338,2 +353,3 @@ approvedVouchers: [voucherId] | ||
async rejectVoucher(voucherId) { | ||
const { loginTree } = accountState() | ||
return await changeVoucherStatus(ai, loginTree, { | ||
@@ -340,0 +356,0 @@ rejectedVouchers: [voucherId] |
@@ -15,3 +15,5 @@ import { asMaybe, asObject, uncleaner } from 'cleaners' | ||
asChangePasswordPayload, | ||
asChangePin2IdPayload, | ||
asChangePin2Payload, | ||
asChangeRecovery2IdPayload, | ||
asChangeRecovery2Payload, | ||
@@ -483,4 +485,96 @@ asChangeSecretPayload, | ||
export const vouchersRoute = withLogin2(async request => { | ||
const usernameRoute = withLogin2(async request => { | ||
const { db, login, payload } = request | ||
const cleanPassword = asMaybe(asChangePasswordPayload)(payload) | ||
const cleanPin2 = asMaybe(asChangePin2IdPayload)(payload) | ||
const cleanRecovery2 = asMaybe(asChangeRecovery2IdPayload)(payload) | ||
const cleanUsername = asMaybe(asChangeUsernamePayload)(payload) | ||
// Validate the payload selection: | ||
if (login.passwordAuth != null && cleanPassword == null) { | ||
return await statusResponse( | ||
statusCodes.invalidRequest, | ||
'Missing password payload' | ||
) | ||
} | ||
if (login.pin2Auth != null && cleanPin2 == null) { | ||
return await statusResponse( | ||
statusCodes.invalidRequest, | ||
'Missing pin2Id payload' | ||
) | ||
} | ||
if (login.recovery2Auth != null && cleanRecovery2 == null) { | ||
return await statusResponse( | ||
statusCodes.invalidRequest, | ||
'Missing recovery2Id payload' | ||
) | ||
} | ||
if (login.parentBox == null && cleanUsername == null) { | ||
return await statusResponse( | ||
statusCodes.invalidRequest, | ||
'Missing username payload' | ||
) | ||
} | ||
// Do we have a password? | ||
if (cleanPassword != null) { | ||
login.passwordAuth = cleanPassword.passwordAuth | ||
login.passwordAuthBox = cleanPassword.passwordAuthBox | ||
login.passwordAuthSnrp = cleanPassword.passwordAuthSnrp | ||
login.passwordBox = cleanPassword.passwordBox | ||
login.passwordKeySnrp = cleanPassword.passwordKeySnrp | ||
} | ||
// Do we have a PIN? | ||
if (cleanPin2 != null) { | ||
if (login.pin2Auth == null) { | ||
return await statusResponse( | ||
statusCodes.invalidRequest, | ||
'Login lacks pin2' | ||
) | ||
} | ||
const existing = await db.getLoginByPin2Id(cleanPin2.pin2Id) | ||
if (existing != null) { | ||
return await statusResponse(statusCodes.conflict) | ||
} | ||
login.pin2Id = cleanPin2.pin2Id | ||
} | ||
// Do we have recovery? | ||
if (cleanRecovery2 != null) { | ||
if (login.recovery2Auth == null) { | ||
return await statusResponse( | ||
statusCodes.invalidRequest, | ||
'Login lacks recovery2' | ||
) | ||
} | ||
const existing = await db.getLoginByRecovery2Id(cleanRecovery2.recovery2Id) | ||
if (existing != null) { | ||
return await statusResponse(statusCodes.conflict) | ||
} | ||
login.recovery2Id = cleanRecovery2.recovery2Id | ||
} | ||
// Are we the root login? | ||
if (cleanUsername != null) { | ||
if (login.parentBox != null) { | ||
return await statusResponse( | ||
statusCodes.invalidRequest, | ||
'Only top-level logins can have usernames' | ||
) | ||
} | ||
const existing = await db.getLoginByUserId(cleanUsername.userId) | ||
if (existing != null) { | ||
return await statusResponse(statusCodes.conflict) | ||
} | ||
login.userId = cleanUsername.userId | ||
login.userTextBox = cleanUsername.userTextBox | ||
} | ||
return await payloadResponse(wasLoginPayload(makeLoginPayload(db, login))) | ||
}) | ||
const vouchersRoute = withLogin2(async request => { | ||
const { db, login, payload } = request | ||
const clean = asMaybe(asChangeVouchersPayload)(payload) | ||
@@ -685,2 +779,5 @@ if (clean == null) return await statusResponse(statusCodes.invalidRequest) | ||
}), | ||
'/api/v2/login/username/?': pickMethod({ | ||
POST: withApiKey(usernameRoute) | ||
}), | ||
'/api/v2/login/vouchers/?': pickMethod({ | ||
@@ -687,0 +784,0 @@ POST: withApiKey(vouchersRoute) |
@@ -1370,2 +1370,3 @@ // @flow | ||
+checkPin: (pin: string) => Promise<boolean>; | ||
+getPin: () => Promise<string | void>; | ||
@@ -1372,0 +1373,0 @@ // Remove credentials: |
@@ -1705,1 +1705,2 @@ | ||
{ | ||
"name": "edge-core-js", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Edge account & wallet management library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1364,2 +1364,3 @@ import type { Disklet } from 'disklet' | ||
readonly checkPin: (pin: string) => Promise<boolean> | ||
readonly getPin: () => Promise<string | undefined> | ||
@@ -1366,0 +1367,0 @@ // Remove credentials: |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
3374084
31815