Skip to content

Commit aad9fd6

Browse files
committed
fix: add duplicate of keyToImplicitAddress to @near-js/crypto
1 parent 65a303f commit aad9fd6

File tree

6 files changed

+75
-16
lines changed

6 files changed

+75
-16
lines changed

.changeset/grumpy-numbers-find.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@near-js/crypto": patch
3+
"@near-js/utils": patch
4+
"near-api-js": patch
5+
---
6+
7+
Add duplicate of `keyToImplicitAddress` to `@near-js/crypto` to prevent cycle dependency between packages

packages/crypto/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export { KeyPair } from './key_pair';
44
export type { Signature } from './key_pair_base';
55
export { KeyPairEd25519 } from './key_pair_ed25519';
66
export { KeyPairSecp256k1 } from './key_pair_secp256k1';
7-
export { PublicKey } from './public_key';
7+
export { PublicKey, keyToImplicitAddress } from './public_key';

packages/crypto/src/public_key.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,19 @@ export class PublicKey extends Enum {
151151
return this.keyPair.data;
152152
}
153153
}
154+
155+
export function keyToImplicitAddress(publicKey: string | PublicKey): string {
156+
const pk = PublicKey.from(publicKey);
157+
158+
const publicKeyWithoutPrefix = pk.toString().replace(/^ed25519:/, '');
159+
160+
const decoded = baseDecode(publicKeyWithoutPrefix);
161+
// Converting to hex string is implemented manually
162+
// to avoid issues with environments that do not support Buffer
163+
let result = '';
164+
for (let i = 0; i < decoded.length; i++) {
165+
const hex = decoded[i].toString(16);
166+
result += hex.length === 1 ? '0' + hex : hex;
167+
}
168+
return result;
169+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect, test } from '@jest/globals';
2+
import { PublicKey, keyToImplicitAddress } from '../src';
3+
4+
test.each`
5+
publicKey | expected
6+
${'ed25519:DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847'} | ${'bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0'}
7+
${'DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847'} | ${'bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0'}
8+
${'ed25519:6E8sCci9badyRkXb3JoRpBj5p8C6Tw41ELDZoiihKEtp'} | ${'4da7e0f4096aaf2ce55e371657cd3089ba1e9f59f4d6e27bd02e472a16a61dc1'}
9+
${'6E8sCci9badyRkXb3JoRpBj5p8C6Tw41ELDZoiihKEtp'} | ${'4da7e0f4096aaf2ce55e371657cd3089ba1e9f59f4d6e27bd02e472a16a61dc1'}
10+
${'ed25519:5TdhJVkBRc9YWQdRZJXjYkfNNb8FYhPnJbGJk4zE5kDN'} | ${'424156f9ae27087a5de6b42f44d7f96db75d73ff6f3b9aa45f4cbe1259234829'}
11+
`(
12+
'keyToImplicitAddress($publicKey) returns $expected',
13+
({ publicKey, expected }) => {
14+
expect(keyToImplicitAddress(publicKey)).toEqual(expected);
15+
}
16+
);
17+
18+
test('keyToImplicitAddress accepts object with toString method', () => {
19+
const pk = PublicKey.fromString('ed25519:DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847');
20+
21+
const result = keyToImplicitAddress(pk);
22+
expect(result).toEqual(
23+
'bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0'
24+
);
25+
});
26+
27+
test('keyToImplicitAddress handles object with toString returning key without prefix', () => {
28+
const pk = PublicKey.fromString('DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847');
29+
30+
const result = keyToImplicitAddress(pk);
31+
expect(result).toEqual(
32+
'bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0'
33+
);
34+
});
35+
36+
test('keyToImplicitAddress returns hex string with proper padding', () => {
37+
const result = keyToImplicitAddress(
38+
'ed25519:DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847'
39+
);
40+
41+
expect(result.length).toEqual(64);
42+
expect(result).toMatch(/^[0-9a-f]+$/);
43+
});

packages/utils/src/utils.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { baseDecode } from './format';
2-
import type { PublicKey } from "../../crypto/src/public_key";
3-
42

53
export function sortBigIntAsc(a: bigint, b: bigint) {
64
return (a < b ? -1 : a > b ? 1 : 0)
75
}
86

9-
10-
export function keyToImplicitAddress(publicKey: string | PublicKey): string {
11-
const publicKeyStr = typeof publicKey === 'string' ? publicKey : publicKey.toString();
12-
13-
const publicKeyWithoutPrefix = publicKeyStr.replace(/^ed25519:/, '');
7+
/**
8+
* @deprecated use {@link keyToImplicitAddress} exported from `@near-js/crypto` instead
9+
*/
10+
export function keyToImplicitAddress(publicKey: string): string {
11+
const publicKeyWithoutPrefix = publicKey.toString().replace(/^ed25519:/, '');
1412

1513
const decoded = baseDecode(publicKeyWithoutPrefix);
1614
let result = '';

packages/utils/test/utils.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { expect, test } from '@jest/globals';
22
import { keyToImplicitAddress } from '../src/utils';
3-
import { PublicKey } from '../../crypto/src/public_key';
43

54
test.each`
65
publicKey | expected
@@ -13,17 +12,13 @@ test.each`
1312
expect(keyToImplicitAddress(publicKey)).toEqual(expected);
1413
});
1514

16-
test('keyToImplicitAddress accepts object with toString method', () => {
17-
const publicKeyObject = PublicKey.fromString('ed25519:DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847');
18-
19-
const result = keyToImplicitAddress(publicKeyObject);
15+
test('keyToImplicitAddress accepts object with toString method', () => {
16+
const result = keyToImplicitAddress('ed25519:DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847');
2017
expect(result).toEqual('bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0');
2118
});
2219

2320
test('keyToImplicitAddress handles object with toString returning key without prefix', () => {
24-
const publicKeyObject = PublicKey.fromString('DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847');
25-
26-
const result = keyToImplicitAddress(publicKeyObject);
21+
const result = keyToImplicitAddress('DcA2MzgpJbrUATQLLceocVckhhAqrkingax4oJ9kZ847');
2722
expect(result).toEqual('bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0');
2823
});
2924

0 commit comments

Comments
 (0)