-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignedCommandMapper.php
65 lines (56 loc) · 2.02 KB
/
SignedCommandMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php declare(strict_types=1);
namespace Kadena\DataMappers;
use InvalidArgumentException;
use JsonException;
use Kadena\Contracts\DataMappers\SignedCommandMapper as SignedCommandMapperContract;
use Kadena\ValueObjects\Command\SignedCommand;
use Kadena\ValueObjects\Signer\Signature;
use Kadena\ValueObjects\Signer\SignatureCollection;
final class SignedCommandMapper implements SignedCommandMapperContract
{
/**
* @throws JsonException
*/
public static function toArray(SignedCommand $command): array
{
return [
'cmd' => CommandMapper::toString($command->command),
'hash' => $command->hash,
'sigs' => array_map(static function (Signature $signature) {
return ['sig' => $signature->signature];
}, $command->signatures->toArray())
];
}
/**
* @throws JsonException
*/
public static function toString(SignedCommand $command): string
{
return json_encode(self::toArray($command), JSON_THROW_ON_ERROR);
}
/**
* @throws JsonException
*/
public static function fromString(string $commandJson): SignedCommand
{
$command = json_decode($commandJson, false, 512, JSON_THROW_ON_ERROR);
if (! isset($command->hash, $command->sigs, $command->cmd)) {
throw new InvalidArgumentException('Invalid Signed Command JSON string given');
}
return new SignedCommand(
hash: $command->hash,
signatures: self::getSignatures((array) $command->sigs, $command->hash),
command: CommandMapper::fromString($command->cmd),
);
}
private static function getSignatures(array $signatures, string $hash): SignatureCollection
{
return new SignatureCollection(...array_map(static function (array|object $signature) use ($hash) {
$signature = (array) $signature;
return new Signature(
hash: $hash,
signature: $signature['sig']
);
}, $signatures));
}
}