Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Keypair
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 generate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 from
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 fromSecretKey
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 fromSeed
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getPublicKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSecretKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Attestto\SolanaPhpSdk;
4
5use SodiumException;
6use Attestto\SolanaPhpSdk\Util\Buffer;
7use Attestto\SolanaPhpSdk\Util\HasPublicKey;
8use Attestto\SolanaPhpSdk\Util\HasSecretKey;
9
10/**
11 * An account keypair used for signing transactions.
12 *  @property PublicKey $publicKey The public key for this keypair
13 *  @property PublicKey $secretKey The raw secret key for this keypair
14 *   
15 */
16class Keypair implements HasPublicKey, HasSecretKey
17{
18    public PublicKey $publicKey;
19    public Buffer $secretKey;
20
21
22    public function __construct($publicKey = null, $secretKey = null)
23    {
24        if ($publicKey == null && $secretKey == null) {
25            $keypair = sodium_crypto_sign_keypair();
26
27            $publicKey = sodium_crypto_sign_publickey($keypair);
28            $secretKey = sodium_crypto_sign_secretkey($keypair);
29        }
30
31        // $this->publicKey = Buffer::from($publicKey);
32        // $this->secretKey = Buffer::from($secretKey);
33        $this->publicKey = new PublicKey($publicKey);
34        $this->secretKey = new Buffer($secretKey);
35    }
36
37    /**
38     * @return Keypair
39     * @throws SodiumException
40     */
41    public static function generate(): Keypair
42    {
43        $keypair = sodium_crypto_sign_keypair();
44
45        return static::from($keypair);
46    }
47
48    /**
49     * @param string $keypair
50     * @return Keypair
51     * @throws SodiumException
52     */
53    public static function from(string $keypair): Keypair
54    {
55        return new static(
56            sodium_crypto_sign_publickey($keypair),
57            sodium_crypto_sign_secretkey($keypair)
58        );
59    }
60
61    /**
62     * Create a keypair from a raw secret key byte array.
63     *
64     * This method should only be used to recreate a keypair from a previously
65     * generated secret key. Generating keypairs from a random seed should be done
66     * with the {@link Keypair.fromSeed} method.
67     *
68     * @param $secretKey
69     * @return Keypair
70     */
71    static public function fromSecretKey($secretKey, $skipValidation = null): Keypair
72    {
73        $secretKey = Buffer::from($secretKey)->toString();
74
75        $publicKey = sodium_crypto_sign_publickey_from_secretkey($secretKey);
76
77        return new static(
78            $publicKey,
79            $secretKey
80        );
81    }
82
83    /**
84     * Generate a keypair from a 32 byte seed.
85     *
86     * @param string|array $seed
87     * @return Keypair
88     * @throws SodiumException
89     */
90    static public function fromSeed($seed): Keypair
91    {
92        $seed = Buffer::from($seed)->toString();
93
94        $keypair = sodium_crypto_sign_seed_keypair($seed);
95
96        return static::from($keypair);
97    }
98
99    /**
100     * The public key for this keypair
101     *
102     * @return PublicKey
103     */
104    public function getPublicKey(): PublicKey
105    {
106        return $this->publicKey;
107    }
108
109    /**
110     * The raw secret key for this keypair
111     *
112     * @return Buffer
113     */
114    public function getSecretKey(): Buffer
115    {
116        return Buffer::from($this->secretKey);
117    }
118}