|
1 | 1 | import { BaseUtils } from '@bitgo/sdk-core'; |
| 2 | +import elliptic from 'elliptic'; |
| 3 | +import { Principal as DfinityPrincipal } from '@dfinity/principal'; |
| 4 | +import * as agent from '@dfinity/agent'; |
| 5 | +import crypto from 'crypto'; |
| 6 | +import crc32 from 'crc-32'; |
| 7 | + |
| 8 | +const Secp256k1Curve = new elliptic.ec('secp256k1'); |
2 | 9 |
|
3 | 10 | export class Utils implements BaseUtils { |
4 | 11 | isValidAddress(address: string): boolean { |
5 | 12 | throw new Error('Method not implemented.'); |
6 | 13 | } |
| 14 | + |
7 | 15 | isValidTransactionId(txId: string): boolean { |
8 | 16 | throw new Error('Method not implemented.'); |
9 | 17 | } |
| 18 | + |
10 | 19 | isValidPublicKey(key: string): boolean { |
11 | | - throw new Error('Method not implemented.'); |
| 20 | + const hexRegex = /^[0-9a-fA-F]+$/; |
| 21 | + if (!hexRegex.test(key)) return false; |
| 22 | + |
| 23 | + const length = key.length; |
| 24 | + if (length !== 130) return false; |
| 25 | + |
| 26 | + return true; |
12 | 27 | } |
| 28 | + |
13 | 29 | isValidPrivateKey(key: string): boolean { |
14 | 30 | throw new Error('Method not implemented.'); |
15 | 31 | } |
| 32 | + |
16 | 33 | isValidSignature(signature: string): boolean { |
17 | 34 | throw new Error('Method not implemented.'); |
18 | 35 | } |
| 36 | + |
19 | 37 | isValidBlockId(hash: string): boolean { |
20 | 38 | throw new Error('Method not implemented.'); |
21 | 39 | } |
| 40 | + |
| 41 | + getHeaders(): Record<string, string> { |
| 42 | + return { |
| 43 | + 'Content-Type': 'application/json', |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + getNetworkIdentifier(): Record<string, string> { |
| 48 | + return { |
| 49 | + blockchain: 'Internet Computer', |
| 50 | + network: '00000000000000020101', |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + compressPublicKey(uncompressedKey: string): string { |
| 55 | + if (!uncompressedKey.startsWith('04')) { |
| 56 | + throw new Error('Invalid uncompressed public key format'); |
| 57 | + } |
| 58 | + const xHex = uncompressedKey.slice(2, 66); |
| 59 | + const yHex = uncompressedKey.slice(66); |
| 60 | + const y = BigInt(`0x${yHex}`); |
| 61 | + const prefix = y % 2n === 0n ? '02' : '03'; |
| 62 | + return prefix + xHex; |
| 63 | + } |
| 64 | + |
| 65 | + getCurveType(): string { |
| 66 | + return 'secp256k1'; |
| 67 | + } |
| 68 | + |
| 69 | + derivePrincipalFromPublicKey(publicKeyHex: string): DfinityPrincipal { |
| 70 | + const publicKeyBuffer = Buffer.from(publicKeyHex, 'hex'); |
| 71 | + |
| 72 | + try { |
| 73 | + const ellipticKey = Secp256k1Curve.keyFromPublic(publicKeyBuffer); |
| 74 | + const uncompressedPublicKeyHex = ellipticKey.getPublic(false, 'hex'); |
| 75 | + const derEncodedKey = agent.wrapDER(Buffer.from(uncompressedPublicKeyHex, 'hex'), agent.SECP256K1_OID); |
| 76 | + const principalId = DfinityPrincipal.selfAuthenticating(Buffer.from(derEncodedKey)); |
| 77 | + const principal = DfinityPrincipal.fromUint8Array(principalId.toUint8Array()); |
| 78 | + return principal; |
| 79 | + } catch (error) { |
| 80 | + throw new Error(`Failed to process the public key: ${error.message}`); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fromPrincipal(principal: DfinityPrincipal, subAccount: Uint8Array = new Uint8Array(32)): string { |
| 85 | + const ACCOUNT_ID_PREFIX = new Uint8Array([0x0a, ...Buffer.from('account-id')]); |
| 86 | + const principalBytes = principal.toUint8Array(); |
| 87 | + const combinedBytes = new Uint8Array(ACCOUNT_ID_PREFIX.length + principalBytes.length + subAccount.length); |
| 88 | + |
| 89 | + combinedBytes.set(ACCOUNT_ID_PREFIX, 0); |
| 90 | + combinedBytes.set(principalBytes, ACCOUNT_ID_PREFIX.length); |
| 91 | + combinedBytes.set(subAccount, ACCOUNT_ID_PREFIX.length + principalBytes.length); |
| 92 | + |
| 93 | + const sha224Hash = crypto.createHash('sha224').update(combinedBytes).digest(); |
| 94 | + const checksum = Buffer.alloc(4); |
| 95 | + checksum.writeUInt32BE(crc32.buf(sha224Hash) >>> 0, 0); |
| 96 | + |
| 97 | + const accountIdBytes = Buffer.concat([checksum, sha224Hash]); |
| 98 | + return accountIdBytes.toString('hex'); |
| 99 | + } |
22 | 100 | } |
| 101 | + |
| 102 | +const utils = new Utils(); |
| 103 | +export default utils; |
0 commit comments