Skip to content

Commit

Permalink
update sdk.ts notice (#15)
Browse files Browse the repository at this point in the history
* update some test;fix issue1;fix pr #7

* update tsconfig.json for build amd file

* add verify ontid claim; add ontidContract tx

* update ontid contract

* update tsconfig.json compile option

* add restful,rpc,websocket api
  • Loading branch information
MickWang authored Apr 16, 2018
1 parent 5fbcba2 commit 327b595
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
53 changes: 32 additions & 21 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

/*
********************************* Notice ********************************************
*************************************************************************************
* All the methods in this file is only for our native app development!!!
* We do not recommend you to use these methods directly.
* You can refer to these methods or the test cases to implement the same methods.
*************************************************************************************
*************************************************************************************
*/

import {Wallet} from '../wallet'
import {Identity} from '../identity'
import {Account} from '../account'
Expand Down Expand Up @@ -140,7 +150,7 @@ export class SDK {
let param = buildRestfulParam(tx)
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}/`
let url = sendRawTxRestfulUrl(restUrl, true)
axios.post(url, param).then((res:any) => {
return axios.post(url, param).then((res:any) => {
if (res.data.Result && res.data.Result.length > 0 && res.data.Result[0] !== '0000000000000000') {

} else {
Expand All @@ -150,8 +160,6 @@ export class SDK {
callback && sendBackResult2Native(JSON.stringify(obj), callback)
return obj
})
// callback && sendBackResult2Native(JSON.stringify(obj), callback)
// return obj
}

//send http post to check
Expand Down Expand Up @@ -209,7 +217,7 @@ export class SDK {
let param = buildRestfulParam(tx)
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.sendRawTx}`

axios.post(restUrl, param).then((res: any) => {
return axios.post(restUrl, param).then((res: any) => {
if(res.data.Error === 0) {
callback && sendBackResult2Native(JSON.stringify(obj), callback)
} else {
Expand All @@ -219,27 +227,25 @@ export class SDK {
}
callback && sendBackResult2Native(JSON.stringify(obj), callback)
}
return obj
})
return result
}

static createAccount(label: string, password: string, callback?: string): string {
static createAccount(label: string, password: string, callback?: string) {
let account = new Account()
let privateKey = core.generatePrivateKeyStr()
account.create(privateKey, password, label)
let result = account.toJson()
if (callback) {
let obj = {
error : ERROR_CODE.SUCCESS,
result : result,
desc : ''
}
sendBackResult2Native(JSON.stringify(obj), callback)
let obj = {
error : ERROR_CODE.SUCCESS,
result : result,
desc : ''
}
return result
callback && sendBackResult2Native(JSON.stringify(obj), callback)
return obj
}

static importAccountWithWallet(walletDataStr:string, label : string, encryptedPrivateKey:string, password:string, callback: string) {
static importAccountWithWallet(walletDataStr:string, label : string, encryptedPrivateKey:string, password:string, callback ?: string) {
let wallet = Wallet.parseJson(walletDataStr)
let account = new Account()
try {
Expand All @@ -263,7 +269,7 @@ export class SDK {
}

static signSelfClaim(context: string, claimData : string, ontid : string,
encryptedPrivateKey : string, password : string, callback :string) {
encryptedPrivateKey : string, password : string, callback ?:string) {
let privateKey = ''
try {
privateKey = scrypt.decrypt(encryptedPrivateKey, password);
Expand Down Expand Up @@ -326,7 +332,7 @@ export class SDK {


static getClaim(claimId : string, context: string, issuer : string, subject : string, encryptedPrivateKey : string,
password : string, callback : string ) {
password : string, callback ?: string ) {
let privateKey = ''
try {
privateKey = scrypt.decrypt(encryptedPrivateKey, password);
Expand Down Expand Up @@ -397,33 +403,35 @@ export class SDK {
}


static getBalance(address : string, callback : string) {
static getBalance(address : string, callback ?: string) {
if(address.length === 40) {
address = core.u160ToAddress(address)
}
let request = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.getBalance}/${address}`
axios.get(request).then((res : any) => {
return axios.get(request).then((res : any) => {
if(res.data.Error === 0) {
let result = res.data.Result
let obj = {
error : 0,
result : result
}
callback && sendBackResult2Native(JSON.stringify(obj), callback)
return obj
} else {
let obj = {
error: res.data.Error,
result : ''
}
callback && sendBackResult2Native(JSON.stringify(obj), callback)

return obj
}
}).catch( (err:any) => {
let obj = {
error: JSON.stringify(err),
result: ''
}
callback && sendBackResult2Native(JSON.stringify(obj), callback)
return Promise.reject(obj)
})
}

Expand Down Expand Up @@ -460,7 +468,7 @@ export class SDK {
let tx = makeTransferTransaction('ONT',from, to, value, privateKey)
var param = buildRestfulParam(tx)
let request = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.sendRawTx}`
axios.post(request, param).then( (res:any) => {
return axios.post(request, param).then( (res:any) => {
console.log('transfer response: ' + JSON.stringify(res.data))
if(res.data.Error === 0) {
let obj = {
Expand All @@ -469,16 +477,19 @@ export class SDK {
desc : 'Send transfer success.'
}
callback && sendBackResult2Native(JSON.stringify(obj), callback)
return obj
} else {
let obj = {
error: res.data.Error,
result: '',
desc: 'Send transfer failed.'
}
callback && sendBackResult2Native(JSON.stringify(obj), callback)
return obj
}
}).catch( (err:any) => {
console.log(err)
return Promise.reject(err)
})
}

Expand Down
3 changes: 1 addition & 2 deletions test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ describe('test core', ()=>{

test('encrypt private key', () => {
let privateKey = 'b02304dcb35bc9a055147f07b2a3291db4ac52f664ec38b436470c98db4200d9'
let wif = core.getWIFFromPrivateKey(privateKey)
let encrypt = scrypt.encrypt(wif, '123456')
let encrypt = scrypt.encrypt(privateKey, '123456')
console.log('encrypt: '+ encrypt)
})

Expand Down

0 comments on commit 327b595

Please sign in to comment.