Skip to content

Commit a2e8159

Browse files
author
Kok Kek
committed
feat: Generate a random contract key pair if one is not set
1 parent 19ed16c commit a2e8159

File tree

11 files changed

+50
-54
lines changed

11 files changed

+50
-54
lines changed

src/cli/actions/compile/generator/generateTypeScript.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ${payloadClass}
2828
}
2929

3030
function getImports (name: string): string {
31-
return `import { type CompiledContractConfig, Contract, type ContractOptions, type ResultOfCall, ZERO } from 'vendee'
31+
return `import { type CompiledContractConfig, Contract, type ContractOptions, type ResultOfCall } from 'vendee'
3232
import { type KeyPair, type ResultOfProcessMessage } from '@eversdk/core'
3333
import ${name}Content from './${name}Content'`
3434
}
@@ -66,12 +66,12 @@ function getContractClass (name: string, abi: AbiContract): string {
6666
private readonly _call: ${name}Calls
6767
private readonly _run: ${name}Runs
6868
private readonly _payload: ${name}Payload
69-
constructor (config: CompiledContractConfig, options: ContractOptions = {}) {
69+
constructor (config: CompiledContractConfig = {}, options: ContractOptions = {}) {
7070
if (config.address === undefined)
7171
super({
7272
abi: ${name}Content.abi,
7373
initialData: config.initialData ?? {},
74-
keys: config.keys ?? ZERO.keys,
74+
keys: config.keys,
7575
tvc: ${name}Content.tvc
7676
}, options)
7777
else

src/cli/actions/giver/printContract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export async function printContract (contract: Contract): Promise<void> {
4747
* '1,234,567,890.123,456,789'
4848
*/
4949
export function prettifyBalance (balance: bigint): string {
50-
const integerPartOfNumber = balance / B
50+
const integerPartOfNumber = balance / BigInt(B)
5151
const integerPartOfNumberText = integerPartOfNumber.toLocaleString()
52-
const fractionalPartOfNumber = balance % B + B
52+
const fractionalPartOfNumber = balance % BigInt(B) + BigInt(B)
5353
const fractionalPartOfNumberText = fractionalPartOfNumber.toLocaleString(
5454
'en',
5555
{ maximumFractionDigits: 10 }

src/cli/actions/giver/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function validate (options: SendOptions): {
1717

1818
return {
1919
to: options.to,
20-
value: BigInt(parseFloat(options.value) * parseFloat(B.toString()))
20+
value: BigInt(parseFloat(options.value) * B)
2121
}
2222
}
2323

src/contract/constants.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { x0 } from '../utils/hex'
22

33
export const error = {
4-
noKeys: new Error('Contract key pair is undefined'),
54
noClient: new Error('SDK client is undefined'),
65
noTvc: new Error('Contract tvc is undefined'),
76
noGiver: new Error('Contract giver is undefined'),
87
contractAlreadyDeployed: new Error('Contract already deployed')
98
}
109
export const ZERO256 = '0000000000000000000000000000000000000000000000000000000000000000'
11-
export const ZERO = {
12-
keys: {
13-
public: ZERO256,
14-
secret: ZERO256
15-
},
16-
address: `0:${ZERO256}`,
17-
uint256: x0(ZERO256)
18-
}
10+
export const ZERO_ADDRESS = `0:${ZERO256}`
11+
export const ZERO_UINT256 = x0(ZERO256)

src/contract/index.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { type Giver } from '../giver'
88
import { Global } from '../global'
99
import { error } from './constants'
1010
import { createPayload } from './payload'
11+
import { generateRandomKeyPair } from '../keys'
1112

1213
export enum AccountType {
1314
notFound = '-1',
@@ -18,13 +19,9 @@ export enum AccountType {
1819
}
1920

2021
export type CompiledContractConfig = {
21-
address: string
22-
initialData?: Record<string, any>
23-
keys?: KeyPair
24-
} | {
2522
address?: string
2623
initialData?: Record<string, any>
27-
keys: KeyPair
24+
keys?: KeyPair
2825
}
2926

3027
export type ContractOptions = {
@@ -44,42 +41,42 @@ export class Contract {
4441
}
4542

4643
private readonly initialData?: Record<string, any>
47-
private readonly keys?: KeyPair
4844
private readonly tvc?: string
4945

5046
private readonly client?: TonClient
5147
private readonly giver?: Giver
5248

5349
private _address?: string
50+
private _keys?: KeyPair
5451
private _lastTransactionLogicTime: string
5552

5653
constructor (
5754
config: {
5855
address: string
56+
keys?: KeyPair
5957
abi?: AbiContract
6058
initialData?: Record<string, any>
6159
tvc?: string
62-
keys?: KeyPair
6360
} | {
6461
address?: string
62+
keys?: KeyPair
6563
abi: AbiContract
6664
initialData: Record<string, any>
6765
tvc: string
68-
keys: KeyPair
6966
},
7067
options: {
7168
client?: TonClient
7269
giver?: Giver
7370
} = {}
7471
) {
7572
this._address = config.address
73+
this._keys = config.keys
7674
this._lastTransactionLogicTime = '0'
7775
this.abi = {
7876
type: 'Contract',
7977
value: config.abi ?? {}
8078
}
8179
this.initialData = config.initialData ?? {}
82-
this.keys = config.keys
8380
this.tvc = config.tvc
8481

8582
this.client = options.client ?? Global.client
@@ -102,17 +99,14 @@ export class Contract {
10299
if (this.client === undefined)
103100
throw error.noClient
104101

105-
if (this.keys === undefined)
106-
throw error.noKeys
107-
108102
if (this.tvc === undefined)
109103
throw new Error('Contract tvc is undefined')
110104

111105
this._address = (await this.client.abi.encode_message({
112106
abi: this.abi,
113107
signer: {
114108
type: 'Keys',
115-
keys: this.keys
109+
keys: await this.keys()
116110
},
117111
deploy_set: {
118112
tvc: this.tvc,
@@ -122,6 +116,27 @@ export class Contract {
122116
return this._address
123117
}
124118

119+
/**
120+
* Generates a random key pair if no keys are given. Next time returns the already generated key pair
121+
* @example
122+
* const contract = new Contract(...)
123+
* const address = await contract.keyPair()
124+
* @return
125+
* '0:97b53be2604579e89bd0077a5456456857792eb2ff09849d14321fc2c167f29e'
126+
*/
127+
public async keys (): Promise<KeyPair> {
128+
if (this._keys !== undefined)
129+
return this._keys
130+
131+
if (this.client === undefined)
132+
throw error.noClient
133+
134+
if (this.tvc === undefined)
135+
throw new Error('Contract tvc is undefined')
136+
137+
return this._keys = await generateRandomKeyPair(this.client)
138+
}
139+
125140
/**
126141
* Return contract balance
127142
* @example
@@ -250,13 +265,6 @@ export class Contract {
250265
if (this.client === undefined)
251266
throw error.noClient
252267

253-
////////////////
254-
// Check keys //
255-
////////////////
256-
const keysPair = keys ?? this.keys
257-
if (keysPair === undefined)
258-
throw error.noKeys
259-
260268
///////////////////////////////
261269
// Generate external message //
262270
///////////////////////////////
@@ -265,7 +273,7 @@ export class Contract {
265273
abi: this.abi,
266274
signer: {
267275
type: 'Keys',
268-
keys: keysPair
276+
keys: keys ?? await this.keys()
269277
},
270278
address: await this.address(),
271279
call_set: {
@@ -321,9 +329,6 @@ export class Contract {
321329
if (this.client === undefined)
322330
throw error.noClient
323331

324-
if (this.keys === undefined)
325-
throw error.noKeys
326-
327332
if (this.tvc === undefined)
328333
throw error.noTvc
329334

@@ -368,7 +373,7 @@ export class Contract {
368373
abi: this.abi,
369374
signer: {
370375
type: 'Keys',
371-
keys: this.keys
376+
keys: await this.keys()
372377
},
373378
deploy_set: {
374379
tvc: this.tvc,

src/contract/samples/GiverV2/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { GiverV2Content } from './GiverV2Content'
22
import { type CompiledContractConfig, Contract, type ContractOptions, type ResultOfCall } from '../../index'
33
import { type KeyPair } from '@eversdk/core'
4-
import { ZERO } from '../../constants'
54

65
type SendTransactionIn = {
76
dest: string
@@ -30,7 +29,7 @@ export class GiverV2 extends Contract {
3029
super({
3130
abi: GiverV2Content.abi,
3231
initialData: config.initialData ?? {},
33-
keys: config.keys ?? ZERO.keys,
32+
keys: config.keys,
3433
tvc: GiverV2Content.tvc
3534
}, options)
3635
else

src/contract/samples/GiverV3/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { GiverV3Content } from './GiverV3Content'
22
import { type CompiledContractConfig, Contract, type ContractOptions, type ResultOfCall } from '../../index'
33
import { type KeyPair } from '@eversdk/core'
4-
import { ZERO } from '../../constants'
54

65
type SendTransactionIn = {
76
dest: string
@@ -30,7 +29,7 @@ export class GiverV3 extends Contract {
3029
super({
3130
abi: GiverV3Content.abi,
3231
initialData: config.initialData ?? {},
33-
keys: config.keys ?? ZERO.keys,
32+
keys: config.keys,
3433
tvc: GiverV3Content.tvc
3534
}, options)
3635
else

src/contract/samples/SafeMultisigWallet/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { SafeMultisigWalletContent } from './SafeMultisigWalletContent'
22
import { type CompiledContractConfig, Contract, type ContractOptions, type ResultOfCall } from '../../index'
33
import { type KeyPair, type ResultOfProcessMessage } from '@eversdk/core'
4-
import { ZERO } from '../../constants'
54

65
type DeployIn = {
76
owners: string[] | number[] | bigint[]
@@ -108,7 +107,7 @@ export class SafeMultisigWallet extends Contract {
108107
super({
109108
abi: SafeMultisigWalletContent.abi,
110109
initialData: config.initialData ?? {},
111-
keys: config.keys ?? ZERO.keys,
110+
keys: config.keys,
112111
tvc: SafeMultisigWalletContent.tvc
113112
}, options)
114113
else

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { type Config } from './cli/config/types'
77
// Contract //
88
//////////////
99
import { AccountType, type CompiledContractConfig, type ContractOptions, type ResultOfCall, Contract } from './contract'
10-
import { ZERO256, ZERO } from './contract/constants'
10+
import { ZERO256, ZERO_ADDRESS, ZERO_UINT256 } from './contract/constants'
1111
import { createPayload, createTransferPayload } from './contract/payload'
1212
import { GiverV2 } from './contract/samples/GiverV2'
1313
import { GiverV2Content } from './contract/samples/GiverV2/GiverV2Content'
@@ -56,7 +56,8 @@ export {
5656
type ResultOfCall,
5757
Contract,
5858
ZERO256,
59-
ZERO,
59+
ZERO_ADDRESS,
60+
ZERO_UINT256,
6061
createPayload,
6162
createTransferPayload,
6263
GiverV2,

src/keys/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { type KeyPair, type TonClient } from '@eversdk/core'
77
/**
88
* Wrapper for crypto.generate_random_sign_keys()
99
* @example
10-
* await createRandomKeyPair('/home/user/keys/GiverV2.keys.json')
10+
* await createRandomKeyPair()
1111
* @return
1212
* {
1313
* public: '61d08f0f1aa152095b6b2e19f31bf09d84a5ae6037be9247d0e54a3926d46593',
@@ -76,7 +76,7 @@ export async function generateOrReadKeys (file: string, client?: TonClient): Pro
7676
* @param directory Keys relative path e.g. `./keys`
7777
* @param client SDK client
7878
* @example
79-
* namedKeys('/home/user/keys/GiverV2.keys.json')
79+
* namedKeys('root')
8080
* @return
8181
* {
8282
* public: '61d08f0f1aa152095b6b2e19f31bf09d84a5ae6037be9247d0e54a3926d46593',

src/utils/suffixes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const Q = BigInt(1_000_000_000_000_000)
2-
export const T = BigInt(1_000_000_000_000)
3-
export const B = BigInt(1_000_000_000)
4-
export const M = BigInt(1_000_000)
5-
export const K = BigInt(1_000)
2+
export const T = 1_000_000_000_000
3+
export const B = 1_000_000_000
4+
export const M = 1_000_000
5+
export const K = 1_000

0 commit comments

Comments
 (0)