2021-02-05 18:59:00 -08:00
|
|
|
import {Buffer} from 'buffer';
|
2022-09-06 22:38:11 -07:00
|
|
|
import {keccak_256} from '@noble/hashes/sha3';
|
2020-10-22 13:15:24 -07:00
|
|
|
|
2022-08-25 13:34:11 -07:00
|
|
|
import {
|
|
|
|
ecdsaSign,
|
|
|
|
isValidPrivateKey,
|
|
|
|
publicKeyCreate,
|
|
|
|
} from '../../src/utils/secp256k1';
|
2020-10-22 13:15:24 -07:00
|
|
|
import {
|
|
|
|
Connection,
|
2021-05-07 01:59:51 -07:00
|
|
|
Keypair,
|
2020-10-22 13:15:24 -07:00
|
|
|
sendAndConfirmTransaction,
|
|
|
|
LAMPORTS_PER_SOL,
|
|
|
|
Transaction,
|
2021-02-05 18:59:00 -08:00
|
|
|
Secp256k1Program,
|
2022-08-11 01:57:19 -07:00
|
|
|
} from '../../src';
|
|
|
|
import {url} from '../url';
|
2020-10-22 13:15:24 -07:00
|
|
|
|
2021-02-05 18:59:00 -08:00
|
|
|
const randomPrivateKey = () => {
|
2020-10-22 13:15:24 -07:00
|
|
|
let privateKey;
|
|
|
|
do {
|
2021-05-07 01:59:51 -07:00
|
|
|
privateKey = Keypair.generate().secretKey.slice(0, 32);
|
2022-08-25 13:34:11 -07:00
|
|
|
} while (!isValidPrivateKey(privateKey));
|
2021-02-05 18:59:00 -08:00
|
|
|
return privateKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.TEST_LIVE) {
|
|
|
|
describe('secp256k1', () => {
|
2021-03-02 10:16:36 -08:00
|
|
|
const privateKey = randomPrivateKey();
|
2022-08-25 13:34:11 -07:00
|
|
|
const publicKey = publicKeyCreate(
|
|
|
|
privateKey,
|
|
|
|
false /* isCompressed */,
|
|
|
|
).slice(1);
|
2021-03-02 10:16:36 -08:00
|
|
|
const ethAddress = Secp256k1Program.publicKeyToEthAddress(publicKey);
|
2021-05-07 01:59:51 -07:00
|
|
|
const from = Keypair.generate();
|
2021-03-02 10:16:36 -08:00
|
|
|
const connection = new Connection(url, 'confirmed');
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
await connection.confirmTransaction(
|
|
|
|
await connection.requestAirdrop(from.publicKey, 10 * LAMPORTS_PER_SOL),
|
2021-02-07 08:57:12 -08:00
|
|
|
);
|
2021-03-02 10:16:36 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('create secp256k1 instruction with string address', async () => {
|
|
|
|
const message = Buffer.from('string address');
|
2022-09-06 22:38:11 -07:00
|
|
|
const messageHash = Buffer.from(keccak_256(message));
|
2022-08-25 13:34:11 -07:00
|
|
|
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
|
2021-03-02 10:16:36 -08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
Secp256k1Program.createInstructionWithEthAddress({
|
|
|
|
ethAddress: ethAddress.toString('hex'),
|
|
|
|
message,
|
|
|
|
signature,
|
|
|
|
recoveryId,
|
|
|
|
}),
|
|
|
|
);
|
2021-02-05 18:59:00 -08:00
|
|
|
|
2021-03-02 10:16:36 -08:00
|
|
|
await sendAndConfirmTransaction(connection, transaction, [from]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('create secp256k1 instruction with 0x prefix string address', async () => {
|
|
|
|
const message = Buffer.from('0x string address');
|
2022-09-06 22:38:11 -07:00
|
|
|
const messageHash = Buffer.from(keccak_256(message));
|
2022-08-25 13:34:11 -07:00
|
|
|
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
|
2021-03-02 10:16:36 -08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
Secp256k1Program.createInstructionWithEthAddress({
|
|
|
|
ethAddress: '0x' + ethAddress.toString('hex'),
|
|
|
|
message,
|
|
|
|
signature,
|
|
|
|
recoveryId,
|
|
|
|
}),
|
2021-02-05 18:59:00 -08:00
|
|
|
);
|
|
|
|
|
2021-03-02 10:16:36 -08:00
|
|
|
await sendAndConfirmTransaction(connection, transaction, [from]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('create secp256k1 instruction with buffer address', async () => {
|
|
|
|
const message = Buffer.from('buffer address');
|
2022-09-06 22:38:11 -07:00
|
|
|
const messageHash = Buffer.from(keccak_256(message));
|
2022-08-25 13:34:11 -07:00
|
|
|
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
|
2021-03-02 10:16:36 -08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
Secp256k1Program.createInstructionWithEthAddress({
|
|
|
|
ethAddress,
|
|
|
|
message,
|
|
|
|
signature,
|
|
|
|
recoveryId,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
await sendAndConfirmTransaction(connection, transaction, [from]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('create secp256k1 instruction with public key', async () => {
|
|
|
|
const message = Buffer.from('public key');
|
2022-09-06 22:38:11 -07:00
|
|
|
const messageHash = Buffer.from(keccak_256(message));
|
2022-08-25 13:34:11 -07:00
|
|
|
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
|
2021-02-05 18:59:00 -08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
Secp256k1Program.createInstructionWithPublicKey({
|
|
|
|
publicKey,
|
|
|
|
message,
|
|
|
|
signature,
|
|
|
|
recoveryId,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-03-02 10:16:36 -08:00
|
|
|
await sendAndConfirmTransaction(connection, transaction, [from]);
|
2021-02-05 18:59:00 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('create secp256k1 instruction with private key', async () => {
|
2021-03-02 10:16:36 -08:00
|
|
|
const message = Buffer.from('private key');
|
2021-02-05 18:59:00 -08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
Secp256k1Program.createInstructionWithPrivateKey({
|
|
|
|
privateKey,
|
2021-03-02 10:16:36 -08:00
|
|
|
message,
|
2021-02-05 18:59:00 -08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-03-02 10:16:36 -08:00
|
|
|
await sendAndConfirmTransaction(connection, transaction, [from]);
|
2021-02-05 18:59:00 -08:00
|
|
|
});
|
2020-10-22 13:15:24 -07:00
|
|
|
});
|
2021-02-05 18:59:00 -08:00
|
|
|
}
|