Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #167 from ripple/freeze-trustline
Browse files Browse the repository at this point in the history
Add trustline freeze flag support
  • Loading branch information
geertweening committed Oct 2, 2014
2 parents c912b9a + c1639df commit 84cd72b
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 70 deletions.
34 changes: 25 additions & 9 deletions api/trustlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ var respond = require('./../lib/response-handler.js');
var errors = require('./../lib/errors.js');

const TrustSetFlags = {
SetAuth: { name: 'authorized', value: 0x00010000 },
NoRipple: { name: 'prevent_rippling', value: 0x00020000 },
ClearNoRipple: { name: 'allow_rippling', value: 0x00040000 }
SetAuth: { name: 'authorized', value: 0x00010000 },
NoRipple: { name: 'prevent_rippling', value: 0x00020000 },
ClearNoRipple: { name: 'allow_rippling', value: 0x00040000 },
SetFreeze: { name: 'freeze', value: 0x00100000 },
ClearFreeze: { name: 'unfreeze', value: 0x00200000 }
};

exports.get = getTrustLines;
exports.add = addTrustLine;

function getTrustLines(request, response, next) {

var steps = [
validateOptions,
getAccountLines
Expand All @@ -29,6 +30,7 @@ function getTrustLines(request, response, next) {
});

var options = request.params;

Object.keys(request.query).forEach(function(param) {
options[param] = request.query[param];
});
Expand Down Expand Up @@ -71,6 +73,8 @@ function getTrustLines(request, response, next) {
reciprocated_limit: line.limit_peer,
account_allows_rippling: line.no_ripple ? !line.no_ripple : true,
counterparty_allows_rippling: line.no_ripple_peer ? !line.no_ripple_peer : true,
account_froze_line: line.freeze ? line.freeze : false,
counterparty_froze_line: line.freeze_peer ? line.freeze_peer : false
});
});

Expand All @@ -81,7 +85,6 @@ function getTrustLines(request, response, next) {
};
};


function addTrustLine(request, response, next) {
var options = request.params;

Expand Down Expand Up @@ -139,11 +142,11 @@ function addTrustLine(request, response, next) {
if (!/^(undefined|boolean)$/.test(typeof options.trustline.account_allows_rippling)) {
return callback(new errors.InvalidRequestError('Parameter must be a boolean: trustline.allow_rippling'));
}

callback();
};

function addLine(callback) {

var limit = [
options.trustline.limit,
options.trustline.currency,
Expand All @@ -153,6 +156,7 @@ function addTrustLine(request, response, next) {
var transaction = remote.transaction();
var complete = false;
var allows_rippling = false;
var froze_trustline = false;

function transactionSent(m) {
complete = true;
Expand All @@ -165,13 +169,15 @@ function addTrustLine(request, response, next) {
limit: line.value,
currency: line.currency,
counterparty: line.issuer,
account_allows_rippling: allows_rippling
account_allows_rippling: allows_rippling,
account_froze_trustline: froze_trustline
}
};

if (m.tx_json.Flags & TrustSetFlags.SetAuth) {
result.trustline.authorized = true;
}

result.ledger = String(summary.submitIndex);
result.hash = m.tx_json.hash;
callback(null, result);
Expand All @@ -187,24 +193,34 @@ function addTrustLine(request, response, next) {
})

try {

transaction.trustSet(options.account, limit);
transaction.secret(options.secret);

if (typeof options.trustline.quality_in === 'number') {
transaction.tx_json.QualityIn = options.trustline.quality_in;
}
if (typeof options.trustline.quality_out === 'number') {
transaction.tx_json.QualityOut = options.trustline.quality_out;
}

if (typeof options.trustline.account_allows_rippling === 'boolean') {
if (options.trustline.account_allows_rippling) {
transaction.setFlags('ClearNoRipple');
} else {
transaction.setFlags('NoRipple');
}
}
allows_rippling = !Boolean(transaction.tx_json.Flags & 0x00020000);

if (typeof options.trustline.account_froze_trustline === 'boolean') {
if (options.trustline.account_froze_trustline) {
transaction.setFlags('SetFreeze');
} else {
transaction.setFlags('ClearFreeze');
}
}

allows_rippling = !Boolean(transaction.tx_json.Flags & 0x00020000);
froze_trustline = Boolean(transaction.tx_json.Flags & 0x00100000);
} catch (exception) {
return callback(new errors.ApiError(exception));
}
Expand Down
78 changes: 43 additions & 35 deletions test/_payment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe('payments', function() {

route.once('ripple_path_find',_ripple_path_find);
app.get('/v1/accounts/'+fixtures.accounts.alice.address+'/payments/paths/'+fixtures.accounts.bob.address+'/10+USD+'+fixtures.accounts.alice.address)
.end(function(err, resp) {
.expect(function(resp) {
assert.deepEqual(resp.body, {
success: false,
error_type: 'invalid_request',
Expand All @@ -679,8 +679,8 @@ describe('payments', function() {
});
assert.equal(orderlist.test(),true);
orderlist.reset();
done();
});
})
.end(done);
});


Expand Down Expand Up @@ -744,22 +744,24 @@ describe('payments', function() {
"allows_rippling": false
}
})
.end(function(err, resp) {
.expect(function(resp) {
delete resp.body.ledger;
delete resp.body.hash;
assert.deepEqual(resp.body,{ success: true,
trustline: {
account: 'rwmityd4Ss34DBUsRy7Pacv6UA5n7yjfe5',
limit: '10',
currency: 'USD',
counterparty: 'rJRLoJSErtNRFnbCyHEUYnRUKNwkVYDM7U',
account_allows_rippling: true
assert.deepEqual(resp.body,{
"success": true,
"trustline": {
"account": "rwmityd4Ss34DBUsRy7Pacv6UA5n7yjfe5",
"limit": "10",
"currency": "USD",
"counterparty": "rJRLoJSErtNRFnbCyHEUYnRUKNwkVYDM7U",
"account_allows_rippling": true,
"account_froze_trustline": false
}
});
assert.equal(orderlist.test(),true);
orderlist.reset();
done();
});
})
.end(done);
});


Expand Down Expand Up @@ -996,13 +998,13 @@ describe('payments', function() {
"allows_rippling": false
}
})
.end(function(err, resp) {
.expect(function(resp) {
assert.equal(resp.status, 400)
assert.deepEqual(resp.body,{ success: false,
error_type: 'invalid_request',
error: 'Parameter missing: secret' })
done();
})
.end(done);
});

// TODO: PROBLEM no response, secret approximation takes very long, no response for over 10 seconds
Expand Down Expand Up @@ -1033,20 +1035,23 @@ describe('payments', function() {
"allows_rippling": false
}
})
.end(function(err, resp) {
.expect(function(resp) {
assert.equal(resp.status,201);
delete resp.body.hash;
delete resp.body.ledger;
assert.deepEqual(resp.body,{ success: true,
trustline:
{ account: 'rsE6ZLDkXhSvfJHvSqFPhdazsoMgCEC52V',
limit: '10',
currency: 'USD',
counterparty: 'r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ',
account_allows_rippling: true }
assert.deepEqual(resp.body,{
"success": true,
"trustline": {
"account": "rsE6ZLDkXhSvfJHvSqFPhdazsoMgCEC52V",
"limit": "10",
"currency": "USD",
"counterparty": "r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ",
"account_allows_rippling": true,
"account_froze_trustline": false
}
});
done();
})
.end(done);
});

it('dan grants an additional trustline of 10 usd towards carol and uses correct address in the accounts setting', function(done) {
Expand All @@ -1060,17 +1065,20 @@ describe('payments', function() {
"allows_rippling": false
}
})
.expect(function(resp, err) {
assert.equal(resp.status,201)
delete resp.body.hash
delete resp.body.ledger
assert.deepEqual(resp.body,{ success: true,
trustline:
{ account: 'rsE6ZLDkXhSvfJHvSqFPhdazsoMgCEC52V',
limit: '10',
currency: 'USD',
counterparty: 'r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ',
account_allows_rippling: true }
.expect(function(resp) {
assert.equal(resp.status,201);
delete resp.body.hash;
delete resp.body.ledger;
assert.deepEqual(resp.body, {
"success": true,
"trustline": {
"account": "rsE6ZLDkXhSvfJHvSqFPhdazsoMgCEC52V",
"limit": "10",
"currency": "USD",
"counterparty": "r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ",
"account_allows_rippling": true,
"account_froze_trustline": false
}
});
})
.end(done);
Expand Down
Loading

0 comments on commit 84cd72b

Please sign in to comment.