Skip to content

Commit 097e425

Browse files
authored
Merge pull request #6218 from BitGo/vechain-ob
feat(sdk-coin-vet): address and wallet creation
2 parents 225cf27 + 7964875 commit 097e425

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

modules/sdk-coin-vet/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
},
4242
"dependencies": {
4343
"@bitgo/sdk-core": "^35.1.0",
44-
"@bitgo/statics": "^54.1.0"
44+
"@bitgo/statics": "^54.1.0",
45+
"@bitgo/abstract-eth": "^24.4.0",
46+
"@bitgo/secp256k1": "^1.3.3"
4547
},
4648
"devDependencies": {
4749
"@bitgo/sdk-api": "^1.63.3",

modules/sdk-coin-vet/src/lib/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BaseUtils } from '@bitgo/sdk-core';
22

33
export class Utils implements BaseUtils {
44
isValidAddress(address: string): boolean {
5-
throw new Error('Method not implemented');
5+
return /^0x[0-9a-fA-F]{40}$/.test(address);
66
}
77

88
isValidBlockId(hash: string): boolean {

modules/sdk-coin-vet/src/vet.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
BaseCoin,
44
BaseTransaction,
55
BitGoBase,
6+
InvalidAddressError,
67
KeyPair,
78
MPCAlgorithm,
89
MultisigType,
@@ -15,6 +16,9 @@ import {
1516
} from '@bitgo/sdk-core';
1617
import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
1718
import utils from './lib/utils';
19+
import { bip32 } from '@bitgo/secp256k1';
20+
import { randomBytes } from 'crypto';
21+
import { KeyPair as EthKeyPair } from '@bitgo/abstract-eth';
1822

1923
/**
2024
* Full Name: Vechain
@@ -79,7 +83,12 @@ export class Vet extends BaseCoin {
7983
}
8084

8185
async isWalletAddress(params: VerifyAddressOptions): Promise<boolean> {
82-
throw new Error('Method not implemented');
86+
const { address: newAddress } = params;
87+
88+
if (!this.isValidAddress(newAddress)) {
89+
throw new InvalidAddressError(`invalid address: ${newAddress}`);
90+
}
91+
return true;
8392
}
8493

8594
async parseTransaction(): Promise<ParsedTransaction> {
@@ -95,11 +104,28 @@ export class Vet extends BaseCoin {
95104
}
96105

97106
generateKeyPair(seed?: Buffer): KeyPair {
98-
throw new Error('Method not implemented');
107+
if (!seed) {
108+
// An extended private key has both a normal 256 bit private key and a 256
109+
// bit chain code, both of which must be random. 512 bits is therefore the
110+
// maximum entropy and gives us maximum security against cracking.
111+
seed = randomBytes(512 / 8);
112+
}
113+
const extendedKey = bip32.fromSeed(seed);
114+
const xpub = extendedKey.neutered().toBase58();
115+
return {
116+
pub: xpub,
117+
prv: extendedKey.toBase58(),
118+
};
99119
}
100120

101121
isValidPub(pub: string): boolean {
102-
throw new Error('Method not implemented.');
122+
let valid = true;
123+
try {
124+
new EthKeyPair({ pub });
125+
} catch (e) {
126+
valid = false;
127+
}
128+
return valid;
103129
}
104130

105131
isValidAddress(address: string): boolean {

modules/sdk-coin-vet/test/unit/vet.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ describe('Vechain', function () {
2525
tvet.getFullName().should.equal('Testnet VeChain');
2626
tvet.getBaseFactor().should.equal(1e18);
2727
});
28+
29+
it('should validate address', function () {
30+
const vet = bitgo.coin('vet');
31+
vet.isValidAddress('wrongaddress').should.false();
32+
vet.isValidAddress('25bcb8855effa9f12a23c2f7f34f2d92b5841f19').should.false();
33+
vet.isValidAddress('0x7Ca00e3bC8a836026C2917C6c7c6D049E52099dd').should.true();
34+
vet.isValidAddress('0x690fFcefa92876C772E85d4B5963807C2152e08d').should.true();
35+
vet.isValidAddress('0xe59F1cea4e0FEf511e3d0f4EEc44ADf19C4cbeEC').should.true();
36+
});
2837
});

0 commit comments

Comments
 (0)