Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user methods: update user data, and get email #130

Merged
merged 15 commits into from
Aug 9, 2022
Merged
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
uses: actions/checkout@v2
with:
repository: CirclesUBI/circles-docker.git
ref: main
ref: update-users-feature
path: circles-docker

- name: Setup docker repo
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@circles/core",
"version": "2.11.2",
"version": "2.12.0-pre-4140b0a",
"description": "Common methods to interact with the Circles ecosystem",
"main": "lib/index.js",
"contributors": [
Expand Down
61 changes: 61 additions & 0 deletions src/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,67 @@ export default function createUserModule(web3, contracts, utils) {
return true;
},

/**
* Update username, email address, and/or image url, connected (or not) to a deployed Safe address.
*
* @namespace core.user.update
*
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.safeAddress - owned Safe address
* @param {string} userOptions.username - alphanumerical username
* @param {string} userOptions.email - email address
* @param {string} userOptions.avatarUrl - url of the avatar image
*
* @return {boolean} - Returns true when successful
*/
update: async (account, userOptions) => {
checkAccount(web3, account);

const options = checkOptions(userOptions, {
safeAddress: {
type: web3.utils.checkAddressChecksum,
},
username: {
type: 'string',
default: '',
},
email: {
type: 'string',
default: '',
},
avatarUrl: {
type: 'string',
default: '',
},
});

const { address } = account;
const { avatarUrl, safeAddress, username, email } = options;

const { signature } = web3.eth.accounts.sign(
[address, safeAddress, username].join(''),
account.privateKey,
);

await utils.requestAPI({
path: ['users', safeAddress],
method: 'POST',
data: {
address: account.address,
signature,
data: {
email,
safeAddress,
username,
avatarUrl,
},
},
});

return true;
},

/**
* Find multiple user entries by Safe address and username.
*
Expand Down
21 changes: 21 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createCore from './helpers/core';
import getAccount from './helpers/account';
import { deploySafeAndToken } from './helpers/transactions';

let account;
let core;
Expand Down Expand Up @@ -58,5 +59,25 @@ describe('User', () => {

expect(result.data[0].username).toEqual(username);
});

it('should be resolveable after changing username', async () => {
// The Safe must be deployed and signedup to the Hub before trying to change the username
const result = await deploySafeAndToken(core, account);

const newUsername = `dolfin${new Date().getTime()}`;
expect(
await core.user.update(account, {
email,
safeAddress: result.safeAddress,
username: newUsername,
}),
).toBe(true);

const first = await core.user.resolve(account, {
usernames: [newUsername],
});

expect(first.data[0].username).toEqual(newUsername);
});
});
});