Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions src/Enums/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ enum Methods

case Verify;

case Username;

public function name(): string
{
return match ($this) {
Methods::Transfer => 'transfer',
Methods::Vote => 'vote',
Methods::Sign => 'sign',
Methods::Verify => 'verify',
Methods::Username => 'username',
};
}
}
25 changes: 19 additions & 6 deletions src/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ public function generateTransfer(string $recipient, array $options = []): string
return $this->generateUrl($options);
}

public function generateVote(string $subject): string
public function generateVote(string $validatorPublicKey, ?string $username = null): string
{
$options = [
'method' => Methods::Vote->name(),
'method' => Methods::Vote->name(),
'publicKey' => $validatorPublicKey,
];

if (strlen($subject) === 66) {
$options['publicKey'] = $subject;
} else {
$options['delegate'] = $subject;
if ($username) {
$options['username'] = $username;
Comment thread
alexbarnsley marked this conversation as resolved.
Outdated
}

return $this->generateUrl($options);
Expand Down Expand Up @@ -102,6 +101,20 @@ public function generateMessageVerify(string $message, string $signatory, string
return $this->generateUrl($options);
}

public function generateUsername(string $username)
{
if (! $username) {
throw new InvalidArgumentException('Username is required');
}

$options = [
'method' => Methods::Username->name(),
'username' => $username,
];

return $this->generateUrl($options);
}

private function generateUrl(array $options): string
{
$options = [
Expand Down
20 changes: 15 additions & 5 deletions tests/UrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988&method=transfer&recipient=recipient');
});

it('should generate username url', function () {
$builder = new URLBuilder();

expect($builder->generateUsername('alfy'))
->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988&method=username&username=alfy');
});

it('should not allow invalid amounts', function ($amount) {
$builder = new URLBuilder();

Expand Down Expand Up @@ -107,24 +114,27 @@
->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988&method=transfer&recipient=recipient&memo=This+is+a+memo+with+newlines%0A%0Aand+special+characters+like+%C3%A4%C3%B6%C3%BC%C3%9F%0A%0AIt+should+be+encoded+correctly.');
});

it('should generate a vote url from delegate', function () {
it('should generate a vote url from validator public key', function () {
$builder = new UrlBuilder();

expect($builder->generateVote('benchdark'))->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988&method=vote&delegate=benchdark');
expect($builder->generateVote(validatorPublicKey: '03a461f557c88612328c8e6d69991eaa7916359dfd2c6a65fd988b672a8bb780c4'))->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988&method=vote&publicKey=03a461f557c88612328c8e6d69991eaa7916359dfd2c6a65fd988b672a8bb780c4');
});

it('should generate a vote url from delegate public key', function () {
it('should generate a vote url with username', function () {
$builder = new UrlBuilder();

expect($builder->generateVote('0296893488d335ff818391da7c450cfeb7821a4eb535b15b95808ea733915fbfb1'))->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988&method=vote&publicKey=0296893488d335ff818391da7c450cfeb7821a4eb535b15b95808ea733915fbfb1');
expect($builder->generateVote(
validatorPublicKey: '03a461f557c88612328c8e6d69991eaa7916359dfd2c6a65fd988b672a8bb780c4',
username: 'alfy'
))->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988&method=vote&publicKey=03a461f557c88612328c8e6d69991eaa7916359dfd2c6a65fd988b672a8bb780c4&username=alfy');
Comment thread
alexbarnsley marked this conversation as resolved.
Outdated
});

it('should generate a vote url on a different network', function () {
$builder = new UrlBuilder();

$builder->setNetwork(Networks::ARKDevnet);

expect($builder->generateVote('benchdark'))->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867&method=vote&delegate=benchdark');
expect($builder->generateVote(validatorPublicKey: '03a461f557c88612328c8e6d69991eaa7916359dfd2c6a65fd988b672a8bb780c4'))->toBe('https://app.arkvault.io/#/?coin=ARK&nethash=2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867&method=vote&publicKey=03a461f557c88612328c8e6d69991eaa7916359dfd2c6a65fd988b672a8bb780c4');
});

it('should generate sign message url', function () {
Expand Down