-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
90 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,18 @@ | ||
import { SigningKey, hashMessage } from 'ethers'; | ||
import { EthSigner } from '../core/signature'; | ||
import { from_b58 } from './base58'; | ||
import { bytesToHex, hexToBytes, stringToBytes } from './serial'; | ||
import { isEthersSigner } from '../core/signature'; | ||
import { AccountId } from '../core/network'; | ||
import { hexToBytes } from './serial'; | ||
import { AccountKeyType } from '../core/enums'; | ||
|
||
// Public Keys | ||
export function isNearPubKey(pubKey: string): boolean { | ||
return pubKey.startsWith('ed25519:'); | ||
} | ||
|
||
function trimNearPrefix(str: string): string { | ||
return str.replace('ed25519:', ''); | ||
} | ||
|
||
export function nearB58ToHex(b58: string): string { | ||
b58 = trimNearPrefix(b58); | ||
const b58Bytes = from_b58(b58); | ||
if (!b58Bytes) { | ||
throw new Error(`invalid base58 string: ${b58}`); | ||
} | ||
return bytesToHex(b58Bytes); | ||
} | ||
|
||
export function getAccountId(owner: string | Uint8Array): AccountId { | ||
export function inferKeyType(owner: string | Uint8Array): AccountKeyType { | ||
if (typeof owner === 'string') { | ||
owner = hexToBytes(owner); | ||
} | ||
|
||
if (owner.length === 32) { | ||
return { | ||
key_type: AccountKeyType.ED25519, | ||
identifier: bytesToHex(owner), | ||
} | ||
return AccountKeyType.ED25519; | ||
} | ||
|
||
if (owner.length === 20) { | ||
return { | ||
key_type: AccountKeyType.SECP256K1, | ||
identifier: bytesToHex(owner), | ||
} | ||
return AccountKeyType.SECP256K1; | ||
} | ||
|
||
throw new Error('Invalid owner length'); | ||
} | ||
|
||
/** | ||
* Recover a secp256k1 public key from a signer. This will force the user to sign a message. | ||
* | ||
* @param signer - An ethereum signer from Ethers v5 or Ethers v6. | ||
* @param message - The message to sign. Defaults to 'Sign this message to recover your public key.'. | ||
* @returns A promise that resolves to the recovered public key. | ||
* @deprecated No longer supported. Ethereum accounts are now identified by their address. (will be removed in v0.6.0) | ||
*/ | ||
|
||
export async function recoverSecp256k1PubKey( | ||
signer: EthSigner, | ||
message: string = 'Sign this message to recover your public key.' | ||
): Promise<string> { | ||
if (isEthersSigner(signer)) { | ||
const signature = await signer.signMessage(message); | ||
return ecrRecoverPubKey(stringToBytes(message), signature); | ||
} | ||
|
||
throw new Error('Signer must be an ethereum signer from Ethers v5 or Ethers v6.'); | ||
} | ||
|
||
export function ecrRecoverPubKey(unsignedMessage: Uint8Array, signature: string): string { | ||
const msgHash = hashMessage(unsignedMessage); | ||
return SigningKey.recoverPublicKey(msgHash, signature); | ||
} | ||
throw new Error('Cannot determine key type from owner.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { TransferBody } from '../../src/funder/funding_types'; | ||
import { bytesToHex } from '../../src/utils/serial'; | ||
import { kwil, isGasOn, kwilSigner, deriveKeyPair64 } from './setup'; | ||
|
||
// testing balance transfer / management | ||
(isGasOn ? describe : describe.skip)('Funder', () => { | ||
it('should transfer funds to Eth Accounts', async () => { | ||
const transferBody: TransferBody = { | ||
to: '0x6E2fA2aF9B4eF5c8A3BcF9A9B9A4F1a1a2c1c1c1', | ||
amount: BigInt(1), | ||
} | ||
|
||
const result = await kwil.funder.transfer(transferBody, kwilSigner, true); | ||
|
||
expect(result.data).toMatchObject({ | ||
tx_hash: expect.any(String), | ||
}) | ||
}) | ||
|
||
it('should transfer funds to ED25119 accounts', async () => { | ||
const edKeys = await deriveKeyPair64('69420', '69420'); | ||
const edPk = bytesToHex(edKeys.publicKey); | ||
|
||
const transferBody: TransferBody = { | ||
to: edPk, | ||
amount: BigInt(1), | ||
} | ||
|
||
const result = await kwil.funder.transfer(transferBody, kwilSigner, true); | ||
|
||
expect(result.data).toMatchObject({ | ||
tx_hash: expect.any(String), | ||
}) | ||
}) | ||
|
||
describe('Error Cases', () => { | ||
it('should fail when using a custom key type without specification', async () => { | ||
const transferBody: TransferBody = { | ||
to: 'zzzzz', | ||
amount: BigInt(1), | ||
} | ||
|
||
await expect(kwil.funder.transfer(transferBody, kwilSigner, true)).rejects.toThrow(); | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters