Skip to content

Commit e3caba2

Browse files
committed
Revert " update some test;fix issue1;fix pr ontio#7 (ontio#8)"
This reverts commit 098d4f4.
1 parent 098d4f4 commit e3caba2

30 files changed

Lines changed: 143 additions & 1179 deletions

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"main": "./lib/index.js",
66
"types": "./lib/types/index.d.ts",
77
"scripts": {
8-
"test": "./node_modules/.bin/jest",
9-
"build:dev": "./node_modules/.bin/webpack --display-error-details --config webpack.config.js --progress --color",
10-
"build:prod": "./node_modules/.bin/webpack --env.prod --config webpack.config.js --progress --color"
8+
"test": "jest",
9+
"build:dev": "webpack --display-error-details --config webpack.config.js --progress --color",
10+
"build:prod": "webpack --env.prod --config webpack.config.js --progress --color"
1111
},
1212
"jest": {
1313
"moduleFileExtensions": [

src/SDK/index.ts

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,6 @@ import axios from 'axios'
3232
import {BigNumber} from 'bignumber.js'
3333
import {DDO} from '../transaction/ddo';
3434
export class SDK {
35-
static SERVER_NODE : string
36-
static REST_PORT : string
37-
static SOCKET_PORT : string
38-
39-
static setServerNode(node : string) {
40-
if(node) {
41-
SDK.SERVER_NODE = node
42-
return;
43-
}
44-
throw new Error('Can not set ' + node + 'as server node')
45-
}
46-
47-
static setRestPort(port: string) {
48-
if (port) {
49-
SDK.REST_PORT = port
50-
return;
51-
}
52-
throw new Error('Can not set ' + port + ' as restful port')
53-
}
54-
55-
static setSocketPort(port: string) {
56-
if (port) {
57-
SDK.SOCKET_PORT = port
58-
return;
59-
}
60-
throw new Error('Can not set ' + port + 'as socket port')
61-
}
6235

6336
static getDecryptError(err:any) {
6437
return {
@@ -107,8 +80,7 @@ export class SDK {
10780
socket.close()
10881
}
10982
}
110-
let socket = `ws://${SDK.SERVER_NODE}:${SDK.SOCKET_PORT}`
111-
var txSender = new TxSender(socket)
83+
var txSender = new TxSender(ONT_NETWORK.TEST)
11284
txSender.sendTxWithSocket(param, socketCallback)
11385
// callback && sendBackResult2Native(JSON.stringify(obj), callback)
11486
return obj
@@ -118,8 +90,10 @@ export class SDK {
11890
password : string, callback ?: string) {
11991
let identity = new Identity()
12092
let wallet = Wallet.parseJson(walletDataStr)
93+
let privateKey = ''
12194
try {
12295
//TODO check ontid
96+
privateKey = scrypt.decrypt(encryptedPrivateKey, password)
12397
identity = Identity.importIdentity(label, encryptedPrivateKey, password)
12498
} catch (err) {
12599
let obj = this.getDecryptError(err)
@@ -135,10 +109,9 @@ export class SDK {
135109
desc : ''
136110
}
137111
//check ontid on chain
138-
let tx = buildGetDDOTx(identity.ontid)
112+
let tx = buildGetDDOTx(identity.ontid, privateKey)
139113
let param = buildRestfulParam(tx)
140-
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}/`
141-
let url = sendRawTxRestfulUrl(restUrl, true)
114+
let url = sendRawTxRestfulUrl(TEST_ONT_URL.REST_URL, true)
142115
axios.post(url, param).then((res:any) => {
143116
if (res.data.Result && res.data.Result.length > 0 && res.data.Result[0] !== '0000000000000000') {
144117

@@ -157,7 +130,9 @@ export class SDK {
157130
static importIdentity(label : string, encryptedPrivateKey : string, password : string, callback ?: string) {
158131
let identity = new Identity()
159132
let error = {}
133+
let privateKey
160134
try {
135+
privateKey = scrypt.decrypt(encryptedPrivateKey, password)
161136
identity = Identity.importIdentity(label, encryptedPrivateKey, password)
162137
let wallet = new Wallet()
163138
wallet.create(identity.label)
@@ -170,11 +145,10 @@ export class SDK {
170145
desc: ''
171146
}
172147
//check ontid on chain
173-
let tx = buildGetDDOTx(identity.ontid)
148+
let tx = buildGetDDOTx(identity.ontid, privateKey)
174149
let param = buildRestfulParam(tx)
175-
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}/`
176-
let url = sendRawTxRestfulUrl(restUrl, true)
177-
return axios.post(url, param).then((res: any) => {
150+
let url = sendRawTxRestfulUrl(TEST_ONT_URL.REST_URL, true)
151+
axios.post(url, param).then((res: any) => {
178152
if (res.data.Result && res.data.Result.length > 0 && res.data.Result[0] !== '0000000000000000') {
179153

180154
} else {
@@ -189,7 +163,7 @@ export class SDK {
189163
} catch(err) {
190164
error = this.getDecryptError(err)
191165
callback && sendBackResult2Native(JSON.stringify(error), callback)
192-
return Promise.reject(error)
166+
return error
193167
}
194168
}
195169

@@ -206,9 +180,8 @@ export class SDK {
206180
//register ontid
207181
let tx = buildRegisterOntidTx(identity.ontid, privateKey)
208182
let param = buildRestfulParam(tx)
209-
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.sendRawTx}`
210-
211-
axios.post(restUrl, param).then((res: any) => {
183+
const url = TEST_ONT_URL.sendRawTxByRestful
184+
axios.post(url, param).then((res: any) => {
212185
if(res.data.Error === 0) {
213186
callback && sendBackResult2Native(JSON.stringify(obj), callback)
214187
} else {
@@ -349,8 +322,7 @@ export class SDK {
349322
// let txId = core.getHash(tx.serialize())
350323
let param = buildTxParam(tx)
351324
//通过socket能获得推送的结果
352-
let socket = `ws://${SDK.SERVER_NODE}:${SDK.SOCKET_PORT}`
353-
var txSender = new TxSender(socket)
325+
var txSender = new TxSender(ONT_NETWORK.TEST)
354326
const socketCallback = function(res : any, socket : any) {
355327
console.log('res: '+ JSON.stringify(res))
356328
if(res.Action === 'InvokeTransaction' && res.Error === 0) {
@@ -393,7 +365,7 @@ export class SDK {
393365
if(address.length === 40) {
394366
address = core.u160ToAddress(address)
395367
}
396-
let request = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.getBalance}/${address}`
368+
let request = `http://${TEST_NODE}:${HTTP_REST_PORT}${REST_API.getBalance}/${address}`
397369
axios.get(request).then((res : any) => {
398370
if(res.data.Error === 0) {
399371
let result = res.data.Result
@@ -451,7 +423,7 @@ export class SDK {
451423

452424
let tx = makeTransferTransaction('ONT',from, to, value, privateKey)
453425
var param = buildRestfulParam(tx)
454-
let request = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.sendRawTx}`
426+
let request = `http://${TEST_NODE}:${HTTP_REST_PORT}${REST_API.sendRawTx}`
455427
axios.post(request, param).then( (res:any) => {
456428
console.log('transfer response: ' + JSON.stringify(res.data))
457429
if(res.data.Error === 0) {

src/claim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class Claim {
6262
this.Id = CryptoJS.SHA256(CryptoJS.enc.Hex.parse(JSON.stringify(body))).toString()
6363
}
6464

65-
sign( privateKey: string ) : Signature {
65+
sign( privateKey: string ) {
6666
let claimBody = {
6767
Context: this.Context,
6868
Id: this.Id,

src/common/fixed64.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/consts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Algorithm {
4242
export const DEFAULT_ALGORITHM = {
4343
algorithm : "ECDSA",
4444
parameters : {
45-
"curve": "secp256r1" // also called p256
45+
"curve": "secp256r1"
4646
}
4747
}
4848

@@ -77,7 +77,7 @@ export const TEST_ONT_URL = {
7777

7878
RPC_URL: `http://${TEST_NODE}:${HTTP_JSON_PORT}/`,
7979

80-
REST_URL: `http://${TEST_NODE}:${HTTP_REST_PORT}/`,
80+
REST_URL: `http://${TEST_NODE}:${HTTP_REST_PORT}/`,
8181

8282
sendRawTxByRestful : `http://${TEST_NODE}:${HTTP_REST_PORT}${REST_API.sendRawTx}`
8383
}

src/core.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ import * as base58 from 'bs58'
2121
import * as ecurve from 'ecurve'
2222
import * as bigInteger from 'bigi'
2323
import { ab2hexstring, hexstring2ab, StringReader, hexstr2str, num2hexstring } from './utils'
24-
import { ADDR_VERSION, TEST_ONT_URL } from './consts'
24+
import { ADDR_VERSION } from './consts'
2525
import * as scrypt from './scrypt'
2626
import {ERROR_CODE} from './error'
2727
import { VmType } from './transaction/vmcode';
28-
import { buildGetDDOTx, buildRestfulParam, sendRawTxRestfulUrl} from './transaction/transactionBuilder'
29-
import axios from 'axios'
30-
import { DDO } from './transaction/ddo'
3128

3229
var ec = require('elliptic').ec
3330
var wif = require('wif')
@@ -150,20 +147,12 @@ export function signatureData(data: string, privateKey: string): string {
150147
return signatureValue.toString('hex');
151148
}
152149

153-
154-
/*
155-
@data original value of signature
156-
@signature
157-
@publicKey the public key of the signer. is array-like or buffer
158-
*/
159-
export function verifySignature(data: string, signature: string, publicKey: any) {
150+
export function verifySignature(data : string, signature : string, publicKey : string) {
160151
let msg = cryptoJS.enc.Hex.parse(data)
161152
let msgHash = cryptoJS.SHA256(msg)
162153

163154
let elliptic = new ec('p256')
164-
let r = signature.substr(0, 64)
165-
let s = signature.substr(64, 64)
166-
const result = elliptic.verify(msgHash.toString(), { r, s }, publicKey, null)
155+
const result = elliptic.verify(data.toString(), signature, publicKey, null)
167156
return result
168157
}
169158

@@ -223,30 +212,11 @@ export function checkPrivateKey(encryptedPrivateKey : string, password : string)
223212
return true
224213
}
225214

226-
/*
227-
@claim claim json object
228-
*/
215+
229216
export function verifyOntidClaim(claim : any) {
230217
if(!claim.Metadata || !claim.Metadata.Issuer) {
231218
throw new Error('Invalid claim.')
232219
}
233-
let issuerDid = claim.Metadata.Issuer
234-
let tx = buildGetDDOTx(issuerDid)
235-
let param = buildRestfulParam(tx)
236-
let url = sendRawTxRestfulUrl(TEST_ONT_URL.REST_URL, true)
237-
return axios.post(url, param).then( (res:any) => {
238-
if (res.data.Result && res.data.Result.length > 0) {
239-
console.log('ddo hexstr: '+ res.data.Result[0])
240-
const ddo = DDO.deserialize(res.data.Result[0])
241-
console.log('ddo: ' + JSON.stringify(ddo))
242-
if(ddo.publicKeys.length > 0) {
243-
const pk = ddo.publicKeys[0].pk
244-
const signature = claim.Signature.Value
245-
claim.delete('Signature')
246-
return verifySignature(JSON.stringify(claim), signature, hexstring2ab(pk))
247-
} else {
248-
return false
249-
}
250-
}
251-
})
220+
let issuer = claim.Metadata.Issuer
221+
//TODO
252222
}

src/crypto.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,3 @@ export enum SignatureSchema {
1313

1414
SHA512withEDDSA
1515
}
16-
17-
export enum CurveLabel {
18-
P224 = 1,
19-
P256 = 2,
20-
P384 = 3,
21-
P521 = 4,
22-
23-
SM2P256V1 = 20,
24-
25-
ED25519 = 25
26-
}
27-
28-
export enum KeyType {
29-
PK_ECDSA = 0x12,
30-
PK_SM2 = 0x13,
31-
PK_EDDSA = 0x14,
32-
}
33-
34-
export class PublicKey {
35-
algorithm: number
36-
curve: number
37-
pk: string
38-
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import * as core from './core'
3131
import * as utils from './utils'
3232
import * as CONST from './consts'
3333
import { SDK } from './SDK/index'
34+
import { CompleterResult } from 'readline';
3435

3536
class ONT {
3637
Account : any
@@ -80,7 +81,7 @@ class ONT {
8081
this.CONST.HTTP_REST_PORT = port
8182
}
8283

83-
setSocketPort(port: string) {
84+
setWsPortyarn(port: string) {
8485
this.CONST.HTTP_WS_PORT = port
8586
}
8687
}

0 commit comments

Comments
 (0)