2020-10-22 13:15:24 -07:00
|
|
|
// @flow
|
|
|
|
|
2021-02-06 10:59:00 +08:00
|
|
|
import {Buffer} from 'buffer';
|
2021-02-08 00:57:12 +08:00
|
|
|
import {keccak_256} from 'js-sha3';
|
2021-02-06 10:59:00 +08:00
|
|
|
import {privateKeyVerify, ecdsaSign, publicKeyCreate} from 'secp256k1';
|
2020-10-22 13:15:24 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
Connection,
|
|
|
|
Account,
|
|
|
|
sendAndConfirmTransaction,
|
|
|
|
LAMPORTS_PER_SOL,
|
|
|
|
Transaction,
|
2021-02-06 10:59:00 +08:00
|
|
|
Secp256k1Program,
|
2020-10-22 13:15:24 -07:00
|
|
|
} from '../src';
|
2021-02-06 10:59:00 +08:00
|
|
|
import {url} from './url';
|
2021-02-08 00:57:12 +08:00
|
|
|
import {toBuffer} from '../src/util/to-buffer';
|
2020-10-22 13:15:24 -07:00
|
|
|
|
2021-02-06 10:59:00 +08:00
|
|
|
const randomPrivateKey = () => {
|
2020-10-22 13:15:24 -07:00
|
|
|
let privateKey;
|
|
|
|
do {
|
2021-02-06 10:59:00 +08:00
|
|
|
privateKey = new Account().secretKey.slice(0, 32);
|
2020-10-22 13:15:24 -07:00
|
|
|
} while (!privateKeyVerify(privateKey));
|
2021-02-06 10:59:00 +08:00
|
|
|
return privateKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.TEST_LIVE) {
|
|
|
|
describe('secp256k1', () => {
|
|
|
|
it('create secp256k1 instruction with public key', async () => {
|
|
|
|
const privateKey = randomPrivateKey();
|
2021-03-02 09:01:31 +08:00
|
|
|
const publicKey = publicKeyCreate(privateKey, false).slice(1);
|
2021-02-06 10:59:00 +08:00
|
|
|
const message = Buffer.from('This is a message');
|
2021-02-08 00:57:12 +08:00
|
|
|
const messageHash = Buffer.from(
|
|
|
|
keccak_256.update(toBuffer(message)).digest(),
|
|
|
|
);
|
2021-02-06 10:59:00 +08:00
|
|
|
const {signature, recid: recoveryId} = ecdsaSign(messageHash, privateKey);
|
2021-02-17 16:15:09 -08:00
|
|
|
const connection = new Connection(url, 'confirmed');
|
2021-02-06 10:59:00 +08:00
|
|
|
|
|
|
|
const from = new Account();
|
|
|
|
await connection.confirmTransaction(
|
|
|
|
await connection.requestAirdrop(from.publicKey, 2 * LAMPORTS_PER_SOL),
|
2021-02-17 16:15:09 -08:00
|
|
|
'confirmed',
|
2021-02-06 10:59:00 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const transaction = new Transaction().add(
|
|
|
|
Secp256k1Program.createInstructionWithPublicKey({
|
|
|
|
publicKey,
|
|
|
|
message,
|
|
|
|
signature,
|
|
|
|
recoveryId,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
await sendAndConfirmTransaction(connection, transaction, [from], {
|
2021-02-17 16:15:09 -08:00
|
|
|
commitment: 'confirmed',
|
|
|
|
preflightCommitment: 'confirmed',
|
2021-02-06 10:59:00 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('create secp256k1 instruction with private key', async () => {
|
|
|
|
const privateKey = randomPrivateKey();
|
2021-02-17 16:15:09 -08:00
|
|
|
const connection = new Connection(url, 'confirmed');
|
2021-02-06 10:59:00 +08:00
|
|
|
|
|
|
|
const from = new Account();
|
|
|
|
await connection.confirmTransaction(
|
|
|
|
await connection.requestAirdrop(from.publicKey, 2 * LAMPORTS_PER_SOL),
|
2021-02-17 16:15:09 -08:00
|
|
|
'confirmed',
|
2021-02-06 10:59:00 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const transaction = new Transaction().add(
|
|
|
|
Secp256k1Program.createInstructionWithPrivateKey({
|
|
|
|
privateKey,
|
|
|
|
message: Buffer.from('Test 123'),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
await sendAndConfirmTransaction(connection, transaction, [from], {
|
2021-02-17 16:15:09 -08:00
|
|
|
commitment: 'confirmed',
|
|
|
|
preflightCommitment: 'confirmed',
|
2021-02-06 10:59:00 +08:00
|
|
|
});
|
|
|
|
});
|
2020-10-22 13:15:24 -07:00
|
|
|
});
|
2021-02-06 10:59:00 +08:00
|
|
|
}
|